beer_song: iteration 2
This commit is contained in:
parent
c8992eb872
commit
0658b6a066
1 changed files with 31 additions and 25 deletions
|
|
@ -1,51 +1,57 @@
|
||||||
#include "beer_song.h"
|
#include "beer_song.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
namespace beer {
|
namespace beer {
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
string verse(int index)
|
namespace {
|
||||||
{
|
|
||||||
auto count_str = [](int count) -> string {
|
|
||||||
|
|
||||||
|
string count_bottles(int count)
|
||||||
|
{
|
||||||
if (0 == count)
|
if (0 == count)
|
||||||
return "no more bottles";
|
return "no more bottles";
|
||||||
|
|
||||||
if (1 == count)
|
if (1 == count)
|
||||||
return "1 bottle";
|
return "1 bottle";
|
||||||
|
|
||||||
ostringstream oss;
|
return to_string(count) + " bottles";
|
||||||
oss << count << " bottles";
|
};
|
||||||
|
|
||||||
return oss.str();
|
void capitalize_first(string &str)
|
||||||
};
|
{
|
||||||
|
assert(!str.empty());
|
||||||
|
str.front() = toupper(str.front());
|
||||||
|
};
|
||||||
|
|
||||||
auto capitalize_first = [](const string &str) -> string {
|
}
|
||||||
|
|
||||||
string result = str;
|
string verse(int index)
|
||||||
result.front() = toupper(result.front());
|
{
|
||||||
return result;
|
string result = count_bottles(index)
|
||||||
};
|
|
||||||
|
|
||||||
return capitalize_first(count_str(index))
|
|
||||||
+ " of beer on the wall, "
|
+ " of beer on the wall, "
|
||||||
+ count_str(index)
|
+ count_bottles(index)
|
||||||
+ " of beer.\n"
|
+ " of beer.\n"
|
||||||
+ (index ? string("Take ")
|
+ (index ? string("Take ")
|
||||||
+ (index == 1 ? "it" : "one")
|
+ (index == 1 ? "it" : "one")
|
||||||
+ " down and pass it around, "
|
+ " down and pass it around, "
|
||||||
: string("Go to the store and buy some more, "))
|
: string("Go to the store and buy some more, "))
|
||||||
+ count_str(index ? index - 1 : 99)
|
+ count_bottles(index ? index - 1 : 99)
|
||||||
+ " of beer on the wall.\n";
|
+ " of beer on the wall.\n";
|
||||||
|
|
||||||
|
capitalize_first(result);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
string sing(int from_index, int to_index)
|
string sing(int from_index, int to_index)
|
||||||
{
|
{
|
||||||
string result;
|
string result;
|
||||||
|
|
||||||
|
assert(from_index >= to_index);
|
||||||
|
|
||||||
for (int i = from_index; i >= to_index; --i) {
|
for (int i = from_index; i >= to_index; --i) {
|
||||||
|
|
||||||
result += (i == from_index ? "" : "\n");
|
result += (i == from_index ? "" : "\n");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue