food_chain: X macro

This commit is contained in:
Dmitry Kokorin 2016-02-29 14:37:07 +03:00
parent 70581bd363
commit 940f2c971a

View file

@ -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 += "She swallowed the bird to catch the spider "
"that wriggled and jiggled and tickled inside her.\n";
}
else {
result += string("She swallowed the ") result += string("She swallowed the ")
+ animals[i] + animal_name[i]
+ " to catch the " + " to catch the "
+ animals[i-1] + animal_name[i-1];
+ ".\n";
} if (i == Animal::Bird)
result += string(" that wriggled and jiggled and "
"tickled inside her");
result += string(".\n");
} }
result += "I don't know why she swallowed the fly. " result += "I don't know why she swallowed the fly. "