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;
|
||||
|
||||
#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[] = {
|
||||
"",
|
||||
|
|
@ -21,8 +40,6 @@ static const char *verse_phrase[] = {
|
|||
"She's dead, of course!\n"
|
||||
};
|
||||
|
||||
static const int size_of_animals = sizeof(animals) / sizeof(*animals);
|
||||
|
||||
|
||||
string verse(int index)
|
||||
{
|
||||
|
|
@ -31,27 +48,24 @@ string verse(int index)
|
|||
--index;
|
||||
|
||||
result += string("I know an old lady who swallowed a ")
|
||||
+ animals[index]
|
||||
+ animal_name[index]
|
||||
+ ".\n"
|
||||
+ verse_phrase[index];
|
||||
|
||||
if (index != size_of_animals - 1) {
|
||||
if (index != Animal::Horse) {
|
||||
|
||||
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 ")
|
||||
+ animals[i]
|
||||
+ animal_name[i]
|
||||
+ " to catch the "
|
||||
+ animals[i-1]
|
||||
+ ".\n";
|
||||
}
|
||||
+ animal_name[i-1];
|
||||
|
||||
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. "
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue