For the new programmer, some of the terms in this book will probably be unfamiliar. You should have a better idea of them by the time you finish reading this book. However, you may also be unfamiliar with the various programming languages that exist. This chapter will display some of the more popular languages currently used by programmers. These programs are designed to show how each language can be used to create the same output. You’ll notice that the Python program is significantly simpler than the others.

The following code examples all display the song, “99 Bottles of Beer on the Wall”. You can find more at the official 99 Bottles website: http://99-bottles-of-beer.net/. I can’t vouch that each of these programs is valid and will actually run correctly, but at least you get an idea of how each one looks.

C

/*

* 99 bottles of beer in ansi c

*

* by Bill Wein: bearheart@bearnet.com

*

*/

#define MAXBEER (99)

void chug(int beers);


main()

{

register beers;

for(beers = MAXBEER; beers; chug(beers--))

puts("");

puts("\nTime to buy more beer!\n");

exit(0);

}


void chug(register beers)

{

char howmany[8], *s;

s = beers != 1 ? "s" : "";

printf("%d bottle%s of beer on the wall,\n", beers, s);

printf("%d bottle%s of beeeeer . . . ,\n", beers, s);

printf("Take one down, pass it around,\n");


if(--beers) sprintf(howmany, "%d", beers); else strcpy(howmany, "No more");

s = beers != 1 ? "s" : "";

printf("%s bottle%s of beer on the wall.\n", howmany, s);

}


C++ (Object-Oriented Version)

// C++ version of 99 Bottles of Beer, object oriented paradigm

// programmer: Tim Robinson timtroyr@ionet.net

#include <fstream.h>


enum Bottle { BeerBottle };


class Shelf {

unsigned BottlesLeft;

public:

Shelf( unsigned bottlesbought )

: BottlesLeft( bottlesbought )

{}

void TakeOneDown()

{

if (!BottlesLeft)

throw BeerBottle;

BottlesLeft--;

}

operator int () { return BottlesLeft; }

};


int main( int, char ** )

{

Shelf Beer(99);

try {

for (;;) {

char *plural = (int)Beer !=1 ? "s" : "";

cout << (int)Beer << " bottle" << plural

<< " of beer on the wall," << endl;

cout << (int)Beer << " bottle" << plural

<< " of beer," << endl;

Beer.TakeOneDown();

cout << "Take one down, pass it around," << endl;

plural = (int)Beer !=1 ? "s":"";

cout << (int)Beer << " bottle" << plural

<< " of beer on the wall." << endl;

}

}

catch ( Bottle ) {

cout << "Go to the store and buy some more," << endl;

cout << "99 bottles of beer on the wall." << endl;

}

return 0;

}


Java (Java 5.0 version)

/**

* Java 5.0 version of the famous "99 bottles of beer on the wall".

* Note the use of specific Java 5.0 features and the strictly correct output.

*

* @author kvols

*/


import java.util.*;


class Verse {

private final int count;


Verse(int verse) {

count= 100-verse;

}


public String toString() {

String c=

"{0,choice,0#no more bottles|1#1 bottle|1<{0} bottles} of beer";

return java.text.MessageFormat.format(

c.replace("n","N")+" on the wall, "+c+".\n"+

"{0,choice,0#Go to the store and buy some more"+

"|0<Take one down and pass it around}, "+c.replace("{0","{1")+

" on the wall.\n", count, (count+99)%100);

}

}


class Song implements Iterator<Verse> {

private int verse=1;


public boolean hasNext() {

return verse <= 100;

}


public Verse next() {

if(!hasNext())

throw new NoSuchElementException("End of song!");

return new Verse(verse++);

}


public void remove() {

throw new UnsupportedOperationException("Cannot remove verses!");

}

}


public class Beer {

public static void main(String[] args ) {


Iterable<Verse> song= new Iterable<Verse>() {

public Iterator<Verse> iterator() {

return new Song();

}

};


// All this work to utilize this feature:

// "For each verse in the song..."


for(Verse verse : song) {

System.out.println(verse);

}

}

}


C# (Removed unnecessary comments)

/// Implementation of Ninety-Nine Bottles of Beer Song in C#.

/// What's neat is that .NET makes the Binge class a

/// full-fledged component that may be called from any other

/// .NET component.

///

/// Paul M. Parks

/// http://www.parkscomputing.com/

/// February 8, 2002

///


using System;


namespace NinetyNineBottles

{

/// References the method of output.

public delegate void Writer(string format, params object[] arg);


/// References the corrective action to take when we run out.

public delegate int MakeRun();


public class Binge

{

private string beverage;


private int count = 0;


private Writer Sing;


private MakeRun RiskDUI;


public event MakeRun OutOfBottles;


public Binge(string beverage, int count, Writer writer)

{

this.beverage = beverage;

this.count = count;

this.Sing = writer;

}


public void Start()

{

while (count > 0)

{

Sing(

@"

{0} bottle{1} of {2} on the wall,

{0} bottle{1} of {2}.

Take one down, pass it around,",

count, (count == 1) ? "" : "s", beverage);


count--;


if (count > 0)

{

Sing("{0} bottle{1} of {2} on the wall.",

count, (count == 1) ? "" : "s", beverage);

}

else

{

Sing("No more bottles of {0} on the wall.", beverage, null);

}


}


Sing(

@"

No more bottles of {0} on the wall,

No more bottles of {0}.", beverage, null);


if (this.OutOfBottles != null)

{

count = this.OutOfBottles();

Sing("{0} bottles of {1} on the wall.", count, beverage);

}

else

{

Sing("First we weep, then we sleep.");

Sing("No more bottles of {0} on the wall.", beverage, null);

}

}

}


class SingTheSong

{

const int bottleCount = 99;


static void Main(string[] args)

{

Binge binge =

new Binge("beer", bottleCount, new Writer(Console.WriteLine));

binge.OutOfBottles += new MakeRun(SevenEleven);

binge.Start();

}


static int SevenEleven()

{

Console.WriteLine("Go to the store, get some more...");

return bottleCount;

}

}

}


Python (This is why Python rocks)

#!/usr/bin/env python

# -*- coding: iso-8859-1 -*-

"""

99 Bottles of Beer (by Gerold Penz)

Python can be simple, too :-)

"""


for quant in range(99, 0, -1):

if quant > 1:

print quant, "bottles of beer on the wall,", quant, "bottles of beer."

if quant > 2:

suffix = str(quant - 1) + " bottles of beer on the wall."

else:

suffix = "1 bottle of beer on the wall."

elif quant == 1:

print "1 bottle of beer on the wall, 1 bottle of beer."

suffix = "no more beer on the wall!"

print "Take one down, pass it around,", suffix

print "--"