Sha256: b384199811bf8fff893ace4f57f642372261cd37201c01fcd26b1814ce0828a9

Contents?: true

Size: 1.69 KB

Versions: 396

Compression:

Stored size: 1.69 KB

Contents

#include "beer_song.h"
#include <sstream>

using namespace std;

namespace beer
{

namespace
{

struct bottles
{
    bottles(unsigned num)
        : count(num),
        suffix(num > 1 ? "s" : "")
    {}
    unsigned const count;
    string const suffix;
};

ostream& operator<<(ostream& stream, bottles const& b)
{
    if (b.count) {
        stream << b.count << " bottle" << b.suffix;
    } else {
        stream << "no more bottles";
    }
    return stream << " of beer";
}

void bottles_of_beer_on_the_wall(ostream& stream, unsigned count)
{
    if (count) {
        stream << bottles(count) << " on the wall, " << bottles(count) << ".\n";
    } else {
        stream << "No more bottles of beer on the wall, " << bottles(count) << ".\n";
    }
}

void take_one_down_and_pass_it_around(ostream& stream, unsigned count)
{
    if (count > 1) {
        stream << "Take one down and pass it around, " << bottles(count - 1) << " on the wall.\n";
    } else if (count) {
        stream << "Take it down and pass it around, " << bottles(count - 1) << " on the wall.\n";
    } else {
        stream << "Go to the store and buy some more, 99 bottles of beer on the wall.\n";
    }
}

void verse(ostream& stream, unsigned count)
{
    bottles_of_beer_on_the_wall(stream, count);
    take_one_down_and_pass_it_around(stream, count);
}

}

string verse(unsigned num)
{
    ostringstream stream;
    verse(stream, num);
    return stream.str();
}

string sing(unsigned begin, unsigned end)
{
    ostringstream stream;
    for (unsigned num = begin; num > end; --num) {
        verse(stream, num);
        stream << '\n';
    }
    verse(stream, end);
    return stream.str();
}

string sing(unsigned num)
{
    return sing(num, 0);
}

}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.179 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.178 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.177 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.176 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.175 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.174 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.173 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.172 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.171 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.170 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.169 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.167 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.166 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.165 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.164 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.163 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.162 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.161 tracks/cpp/exercises/beer-song/example.cpp
trackler-2.2.1.160 tracks/cpp/exercises/beer-song/example.cpp