Again.. Ninety Nine Bottles of Beers

Here is again.. Ninety Nine Bottles of Beer, C++ style…

#include <iostream>

using namespace std;

const int NINE9=99;
const string n[] = {"bottle", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
const string o[] = {"Bottle", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
const string p[] = {"No more", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};

inline string s(int i) {return (i>1?"s":"");}
string num(int i) {
        int a = i/10;
        int b = i%10;
        string r="";
        r=(a>1||(a==1&&b==0))?n[a]:(a==1&&b>0?o[b]:"");
        r+=(a==0||(a>1&&b>0))?((r!=""?" ":"")+p[b]):"";
        return r;
}

int main() {
        for (int i=NINE9;i>0;i–)
        {
                cout << num(i) << " " << o[0] << s(i) <<
                        " of beer on the wall, " << num(i) <<
                        " " << n[0] << s(i) << " of beer." << endl;
                cout << "Take one down and pass it around, ";
                cout << num(i-1);
                cout << " " << n[0] << s(i-1) << " of beer on the wall." <<
                        endl << endl;
        }
        cout << p[0] << " " << n[0] << "s of beer on the wall, no more " <<
                n[0] << "s of beer." << endl;
        cout << "Go to the store and buy some more, " <<
                num(NINE9) << " " << n[0] << "s of beer on the wall." <<
                endl;
        return 0;
}

cheers… ^^

Leave a Reply