C: raindrops

This commit is contained in:
2021-08-12 12:24:38 +02:00
parent 6e9a77168e
commit 58d81e96a6
6 changed files with 190 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
#include "raindrops.h"
#include <stdio.h>
char *convert(char result[], int drops)
{
char *p=result;
if (!(drops%3)) p+=sprintf(p, "%s", "Pling");
if (!(drops%5)) p+=sprintf(p, "%s", "Plang");
if (!(drops%7)) p+=sprintf(p, "%s", "Plong");
if (p==result) sprintf(p, "%d", drops);
return result;
}
/* See GNUmakefile below for explanation
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#ifdef UNIT_TEST
int main(int ac, char **av)
{
int arg=1, i;
char buffer[128];
for (; arg<ac; ++arg) {
i=atoi(av[arg]);
printf("raindrops(%d)=%s\n", i, convert(buffer, i));
}
}
#endif

View File

@@ -0,0 +1,18 @@
#ifndef RAINDROPS_H
#define RAINDROPS_H
char *convert(char result[], int drops);
/* See GNUmakefile below for explanation
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif