2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
|
2011-07-13 15:44:09 -07:00
|
|
|
/* -*- mode::rust;indent-tabs-mode::nil -*-
|
2010-09-20 22:08:28 +01:00
|
|
|
* Implementation of 99 Bottles of Beer
|
|
|
|
* http://99-bottles-of-beer.net/
|
|
|
|
*/
|
|
|
|
use std;
|
2011-05-17 20:41:41 +02:00
|
|
|
import std::int;
|
|
|
|
import std::str;
|
2010-09-20 22:08:28 +01:00
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
fn b1() -> str { ret "# of beer on the wall, # of beer."; }
|
|
|
|
|
2010-09-20 22:08:28 +01:00
|
|
|
fn b2() -> str {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret "Take one down and pass it around, # of beer on the wall.";
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
|
|
|
fn b7() -> str {
|
|
|
|
ret "No more bottles of beer on the wall, no more bottles of beer.";
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-09-20 22:08:28 +01:00
|
|
|
fn b8() -> str {
|
2011-06-15 11:19:50 -07:00
|
|
|
ret "Go to the store and buy some more, # of beer on the wall.";
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn sub(str t, int n) -> str {
|
2011-06-15 11:19:50 -07:00
|
|
|
let str b = "";
|
|
|
|
let uint i = 0u;
|
|
|
|
let str ns;
|
|
|
|
alt (n) {
|
|
|
|
case (0) { ns = "no more bottles"; }
|
|
|
|
case (1) { ns = "1 bottle"; }
|
|
|
|
case (_) { ns = int::to_str(n, 10u) + " bottles"; }
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
while (i < str::byte_len(t)) {
|
|
|
|
if (t.(i) == '#' as u8) {
|
|
|
|
b += ns;
|
|
|
|
} else { str::push_byte(b, t.(i)); }
|
|
|
|
i += 1u;
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
ret b;
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
|
|
|
|
2011-06-15 11:19:50 -07:00
|
|
|
|
2010-09-20 22:08:28 +01:00
|
|
|
/* Straightforward counter */
|
|
|
|
fn main() {
|
2011-06-15 11:19:50 -07:00
|
|
|
let int n = 99;
|
|
|
|
while (n > 0) { log sub(b1(), n); log sub(b2(), n - 1); log ""; n -= 1; }
|
|
|
|
log b7();
|
|
|
|
log sub(b8(), 99);
|
|
|
|
}
|