-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raindrops approaches: data-driven programming approach
- Loading branch information
1 parent
13ff1d8
commit 6276b74
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
exercises/practice/raindrops/.approaches/data-driven/content.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Data-Driven Programming | ||
|
||
**raindrops.h** | ||
|
||
```c | ||
#ifndef RAINDROPS_H | ||
#define RAINDROPS_H | ||
|
||
char *convert(char result[], int drops); | ||
|
||
#endif | ||
``` | ||
|
||
**raindrops.c** | ||
|
||
```c | ||
#include "raindrops.h" | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
typedef struct { | ||
int factor; | ||
const char *sound; | ||
} sound_t; | ||
|
||
static const sound_t SOUNDS[] = { | ||
{ 3, "Pling" }, | ||
{ 5, "Plang" }, | ||
{ 7, "Plong" }, | ||
}; | ||
|
||
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) | ||
|
||
char *convert(char result[], int drops) | ||
{ | ||
for (size_t i = 0; i < ARRAY_SIZE(SOUNDS); i++) { | ||
if (drops % SOUNDS[i].factor == 0) { | ||
strcat(result, SOUNDS[i].sound); | ||
} | ||
} | ||
|
||
if (strlen(result) == 0) { | ||
sprintf(result, "%d", drops); | ||
} | ||
|
||
return result; | ||
} | ||
``` | ||
First, the program defines a [structure][struct] [data type][typedef] to hold together two pieces of information: a factor and its corresponding sound. | ||
Then, an array is created to hold all the necessary data, which is used to drive the logc of the program. | ||
The body of the function does not have any knowledge of the actual data, becoming simple and flexible: | ||
- For each element of the `SOUNDS` array | ||
-- If the given number is a multiple of the current `factor` | ||
--- Then concatenate the current `sound` to the result string using the [`strcat` function][strcat]. | ||
This approach allows for extensible code: for example, new sounds could be added (or removed) without modifying the `convert` function. | ||
Finally, the [`strlen` function] checks if `result` is empty: if so, the [`sprintf` function] formats `drops` as string into `result`. | ||
[struct]: https://www.geeksforgeeks.org/structures-c/ | ||
[typedef]: https://www.geeksforgeeks.org/typedef-in-c/ | ||
[strcat]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/strcat.html | ||
[strlen]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/strlen.html | ||
[sprintf]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/sprintf.html |
5 changes: 5 additions & 0 deletions
5
exercises/practice/raindrops/.approaches/data-driven/snippet.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
for (size_t i = 0; i < ARRAY_SIZE(SOUNDS); i++) { | ||
if (drops % SOUNDS[i].factor == 0) { | ||
strcat(result, SOUNDS[i].sound); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters