food chain: initial commit
This commit is contained in:
parent
e607f45d38
commit
70581bd363
2 changed files with 92 additions and 0 deletions
79
cpp/food-chain/food_chain.cpp
Normal file
79
cpp/food-chain/food_chain.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
#include "food_chain.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace food_chain {
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
static const char *animals[] =
|
||||||
|
{"fly", "spider", "bird", "cat", "dog", "goat", "cow", "horse"};
|
||||||
|
|
||||||
|
static const char *verse_phrase[] = {
|
||||||
|
"",
|
||||||
|
"It wriggled and jiggled and tickled inside her.\n",
|
||||||
|
"How absurd to swallow a bird!\n",
|
||||||
|
"Imagine that, to swallow a cat!\n",
|
||||||
|
"What a hog, to swallow a dog!\n",
|
||||||
|
"Just opened her throat and swallowed a goat!\n",
|
||||||
|
"I don't know how she swallowed a cow!\n",
|
||||||
|
"She's dead, of course!\n"
|
||||||
|
};
|
||||||
|
|
||||||
|
static const int size_of_animals = sizeof(animals) / sizeof(*animals);
|
||||||
|
|
||||||
|
|
||||||
|
string verse(int index)
|
||||||
|
{
|
||||||
|
string result;
|
||||||
|
|
||||||
|
--index;
|
||||||
|
|
||||||
|
result += string("I know an old lady who swallowed a ")
|
||||||
|
+ animals[index]
|
||||||
|
+ ".\n"
|
||||||
|
+ verse_phrase[index];
|
||||||
|
|
||||||
|
if (index != size_of_animals - 1) {
|
||||||
|
|
||||||
|
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]
|
||||||
|
+ " to catch the "
|
||||||
|
+ animals[i-1]
|
||||||
|
+ ".\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result += "I don't know why she swallowed the fly. "
|
||||||
|
"Perhaps she'll die.\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
string verses(int from_index, int to_index)
|
||||||
|
{
|
||||||
|
string result;
|
||||||
|
|
||||||
|
for (int i = from_index; i <= to_index; ++i)
|
||||||
|
result += verse(i) + '\n';
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
string sing()
|
||||||
|
{
|
||||||
|
return verses(1, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
13
cpp/food-chain/food_chain.h
Normal file
13
cpp/food-chain/food_chain.h
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define EXERCISM_RUN_ALL_TESTS
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace food_chain {
|
||||||
|
|
||||||
|
std::string verse(int index);
|
||||||
|
std::string verses(int from_index, int to_index);
|
||||||
|
std::string sing();
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue