See http://www.cs.trinity.edu/About/The_Courses/cs2322/
Date: Thu, 8 Mar 2001 09:23:02 -0500
From: Roger Hui
Reply-To: forum@jsoftware.com
To: APL Mailing List , J Forum , TimTroyR@ionet.net
Subject: Jforum: Re: New Scientist Puzzle and Oddball Languages
NB. a solution in J (http://www.jsoftware.com) to the 99 Bottles of Beer problem.
bob =: ": , ' bottle'"_ , (1: = ]) }. 's of beer'"_
bobw=: bob , ' on the wall'"_
beer=: bobw , ', '"_ , bob , '; take one down and pass it around, '"_ , bobw@<:
NB. For example:
beer"0 >:i.-5
5 bottles of beer on the wall, 5 bottles of beer; take one down and pass it around, 4 bottles of beer on the wall
4 bottles of beer on the wall, 4 bottles of beer; take one down and pass it around, 3 bottles of beer on the wall
3 bottles of beer on the wall, 3 bottles of beer; take one down and pass it around, 2 bottles of beer on the wall
2 bottles of beer on the wall, 2 bottles of beer; take one down and pass it around, 1 bottle of beer on the wall
1 bottle of beer on the wall, 1 bottle of beer; take one down and pass it around, 0 bottles of beer on the wall
# Jamfile for 99 Bottles of beer on the wall.
# David Brandon (brandon@aspentech.com)
ALWAYS beer ;
rule Drink {
for tenbeers in 9 8 7 6 5 4 3 2 1 "" {
for beers in 9 8 7 6 5 4 3 2 1 0 {
b = $(tenbeers)$(beers) ;
if ( $(b) != 99 ) {
ECHO "$(b) bottles of $(<) on the wall!" ;
}
if ( $(b) = 0 ) {
EXIT "No more $(<)!" ;
}
ECHO "$(b) bottles of $(<) on the wall. $(b) bottles of $(<). Take one down," ;
ECHO "pass it around..." ;
}
}
}
Drink beer ;
<A HREF=http://java.sun.com>Java</A> is a machine independent
compiler based on C++ which targets to pseudo-code.
// java version of 99 bottles of beer on the wall
// 1995 Sean Russell (ser@cs.uoregon.edu)
class bottles
{
public static void main(String args[])
{
String s = "s";
for (int beers=99; beers>-1;)
{
System.out.print(beers + " bottle" + s + " of beer on the wall, ");
System.out.println(beers + " bottle" + s + " of beer, ");
if (beers==0)
{
System.out.print("Go to the store, buy some more, ");
System.out.println("99 bottles of beer on the wall.\n");
System.exit(0);
}
else
System.out.print("Take one down, pass it around, ");
s = (--beers == 1)?"":"s";
System.out.println(beers + " bottle" + s + " of beer on the wall.\n");
}
}
}
Interpretive <A href=#java>Java</a>.
/**
* 99 Bottles of Beer on the Wall in JavaScript
* This program prints out the lyrics of an old pub song.
* Copyright (C) 1996, Brian Patrick Lee (blee@media-lab.mit.edu)
*/
if (confirm("Are you old enough to read about beer\n" +
"according to your local community standards?")) {
for (i = 99 ; i > 0 ; i--) {
j = i - 1;
if (i != 1) {
icase = "bottles";
} else {
icase = "bottle";
}
if (j != 1) {
jcase = "bottles";
} else {
jcase = "bottle";
}
document.writeln(i + " " + icase + " of beer on the wall,");
document.writeln(i + " " + icase + " of beer,");
document.writeln("Take 1 down, pass it around,");
if (j != 0) {
document.writeln(j + " " + jcase + " of beer on the wall.");
} else {
document.writeln("No more bottles of beer on the wall!");
}
document.writeln()
}
} else {
document.write("You might want think about moving to another community.")
}
Form 1: " JOSS version of 99 bottles of beer "
Form 1: " Laurent Vogel, http://lvogel.free.fr "
Form 1: "%.0f "
Form 2: "No more bottles of beer"
Form 3: "bottle of beer"
Form 4: "bottles of beer"
Form 5: " on the wall,\n"
Form 6: ".\nTake one down, pass it around,\n"
Form 7: " on the wall.\n\n"
3.1 Type a in form 1 if a>0.
3.2 Type in form (a<2: a+2; 4).
2.1 Do part 3.
2.2 Type in form 5.
2.3 Do part 3.
2.4 Type in form 6.
2.5 Set a=a-1.
2.6 Do part 3.
2.7 Type in form 7.
Set a=99.
Do part 2 while a>0.
Stop.
/*
* NinetyNine.js
*
* The "99 Bottles of Beer" song rendered in JScript.NET.
*
* To compile:
*
* jsc /target:exe NinetyNine.js
*
* written by Chris R. Timmons (chris@crtimmonsinc.com)
* March 25, 2003
*/
import System;
new Sing();
public final class Sing
{
const verse : String =
"{0} bottle{2} of beer on the wall,\n" +
"{0} bottle{2} of beer.\n" +
"{4}" +
"{1} bottle{3} of beer on the wall.\n";
const beerLeft : String = "Take one down, pass it around.\n";
const noBeerLeft : String = "Go to the store, get some more!\n";
public function Sing()
{
var bottles : int = this.GetNumberOfBottles();
for (var x = bottles; x >= 0; x--)
{
if (x == 2)
Console.WriteLine(verse, x, x - 1, "s", "", beerLeft);
else if (x == 1)
Console.WriteLine(verse, x, "No more", "", "s", beerLeft);
else if (x == 0)
Console.WriteLine(verse, "No more", bottles, "s", (bottles == 1 ? "" : "s"), noBeerLeft);
else
Console.WriteLine(verse, x, x - 1, "s", "s", beerLeft);
}
}
private function GetNumberOfBottles() : int
{
const defaultBottles : int = -1;
// Return value.
var bottles : int = defaultBottles;
// Check the command line arguments.
// (First array element is the program name. Elements 2 thru N
// are the actual arguments).
var args : String[] = Environment.GetCommandLineArgs();
if (args.Length == 1)
{
// If no arguments were given, default to 99 bottles.
bottles = 99;
}
else if (args.Length == 2)
{
// One argument was given.
// Is it a "/?" argument?
if (args[1] == "/?")
{
DisplayUsage();
}
else
{
// Attempt to convert the argument to an integer.
try
{
// Strip out any commas the user may have included
// in the number before attempting the conversion.
bottles = Convert.ToInt32(args[1].Replace(",", ""));
// The conversion succeeded...
if (bottles < 1)
{
// ...but zero and negative numbers are not allowed.
bottles = defaultBottles;
Console.WriteLine("");
Console.WriteLine("ERROR: Can't sing a song for \"{0}\" bottles.", args[1]);
DisplayUsage();
}
}
catch(ex)
{
Console.WriteLine("");
Console.WriteLine("ERROR: Can't sing a song for \"{0}\" bottles.", args[1]);
DisplayUsage();
}
}
}
else
{
// More than one argument was given.
DisplayUsage();
}
return bottles;
}
private function DisplayUsage()
{
Console.WriteLine("");
Console.WriteLine("NinetyNine.exe - Displays lyrics for the \"99 Bottles of Beer\" song.");
Console.WriteLine("");
Console.WriteLine("Usage:");
Console.WriteLine(" NinetyNine [option]");
Console.WriteLine("");
Console.WriteLine(" NinetyNine Command Line Options");
Console.WriteLine("");
Console.WriteLine(" /? : Display this help screen.");
Console.WriteLine("");
Console.WriteLine(" Integer between");
Console.WriteLine(" 1 and 2,147,483,648 : Number of bottles to sing about. ");
Console.WriteLine(" Commas in the number are allowed.");
Console.WriteLine("");
}
}
// *short* JScript/WSH version of 99 Bottles of beer (Bottles.js)
// Philipp Winterberg, http://www.winterbergs.de
//
var beerShell = WScript.CreateObject("WScript.Shell");
for (b = 99 ; b > 0 ; b--) {
beerShell.Popup(b + " bottle(s) of beer on the wall, " + b + " bottle(s) of beer.", 2, "99 Bottles of Beer");
beerShell.Popup("Take one down, pass it around, " + (b - 1) + "bottle(s) of beer on the wall.", 2, "99 Bottles of Beer");
}
<%
// JSP version of 99 bottles of beer on the wall
// 2002 David Hallett (david@pulsar.net.nz)
String s = "";
for (int beers=100; beers>0;)
{
s = (--beers == 1)?"":"s";
%>
<%=beers%> bottle<%=s%> of beer on the wall,
<%=beers%> bottle<%=s%> of beer<br>
<%
if (beers > 0)
{
%> Take one down, pass it around,
<%=beers%> bottle<%=s%> of beer on the wall.<br>
<% }
else
{
%> Go to the store, buy some more,
99 bottles of beer on the wall.<br>
<% }
}
%>