C: raindrops
This commit is contained in:
29
c/raindrops/src/raindrops.c
Normal file
29
c/raindrops/src/raindrops.c
Normal 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
|
18
c/raindrops/src/raindrops.h
Normal file
18
c/raindrops/src/raindrops.h
Normal 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
|
Reference in New Issue
Block a user