food_chain: X macro
This commit is contained in:
parent
70581bd363
commit
940f2c971a
1 changed files with 31 additions and 17 deletions
|
|
@ -6,9 +6,28 @@ namespace food_chain {
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
#define ANIMAL_TABLE \
|
||||||
|
X(Fly, "fly") \
|
||||||
|
X(Spider, "spider") \
|
||||||
|
X(Bird, "bird") \
|
||||||
|
X(Cat, "cat") \
|
||||||
|
X(Dog, "dog") \
|
||||||
|
X(Goat, "goat") \
|
||||||
|
X(Cow, "cow") \
|
||||||
|
X(Horse, "horse")
|
||||||
|
|
||||||
|
#define X(a, b) a,
|
||||||
|
enum Animal {
|
||||||
|
ANIMAL_TABLE
|
||||||
|
};
|
||||||
|
#undef X
|
||||||
|
|
||||||
|
#define X(a, b) b,
|
||||||
|
static const char *animal_name[] = {
|
||||||
|
ANIMAL_TABLE
|
||||||
|
};
|
||||||
|
#undef X
|
||||||
|
|
||||||
static const char *animals[] =
|
|
||||||
{"fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"};
|
|
||||||
|
|
||||||
static const char *verse_phrase[] = {
|
static const char *verse_phrase[] = {
|
||||||
"",
|
"",
|
||||||
|
|
@ -21,8 +40,6 @@ static const char *verse_phrase[] = {
|
||||||
"She's dead, of course!\n"
|
"She's dead, of course!\n"
|
||||||
};
|
};
|
||||||
|
|
||||||
static const int size_of_animals = sizeof(animals) / sizeof(*animals);
|
|
||||||
|
|
||||||
|
|
||||||
string verse(int index)
|
string verse(int index)
|
||||||
{
|
{
|
||||||
|
|
@ -31,27 +48,24 @@ string verse(int index)
|
||||||
--index;
|
--index;
|
||||||
|
|
||||||
result += string("I know an old lady who swallowed a ")
|
result += string("I know an old lady who swallowed a ")
|
||||||
+ animals[index]
|
+ animal_name[index]
|
||||||
+ ".\n"
|
+ ".\n"
|
||||||
+ verse_phrase[index];
|
+ verse_phrase[index];
|
||||||
|
|
||||||
if (index != size_of_animals - 1) {
|
if (index != Animal::Horse) {
|
||||||
|
|
||||||
for (int i = index; i > 0; --i) {
|
for (int i = index; i > 0; --i) {
|
||||||
|
|
||||||
if (i == 2) {
|
result += string("She swallowed the ")
|
||||||
|
+ animal_name[i]
|
||||||
|
+ " to catch the "
|
||||||
|
+ animal_name[i-1];
|
||||||
|
|
||||||
result += "She swallowed the bird to catch the spider "
|
if (i == Animal::Bird)
|
||||||
"that wriggled and jiggled and tickled inside her.\n";
|
result += string(" that wriggled and jiggled and "
|
||||||
}
|
"tickled inside her");
|
||||||
else {
|
|
||||||
|
|
||||||
result += string("She swallowed the ")
|
result += string(".\n");
|
||||||
+ animals[i]
|
|
||||||
+ " to catch the "
|
|
||||||
+ animals[i-1]
|
|
||||||
+ ".\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result += "I don't know why she swallowed the fly. "
|
result += "I don't know why she swallowed the fly. "
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue