2011-05-12 17:24:54 +02: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
|
|
|
|
|
|
|
fn b1() -> str {
|
|
|
|
ret "# of beer on the wall, # of beer.";
|
|
|
|
}
|
|
|
|
fn b2() -> str {
|
|
|
|
ret "Take one down and pass it around, # of beer on the wall.";
|
|
|
|
}
|
|
|
|
fn b7() ->str {
|
|
|
|
ret "No more bottles of beer on the wall, no more bottles of beer.";
|
|
|
|
}
|
|
|
|
fn b8() -> str {
|
|
|
|
ret "Go to the store and buy some more, # of beer on the wall.";
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sub(str t, int n) -> str {
|
|
|
|
let str b = "";
|
|
|
|
let uint i = 0u;
|
|
|
|
let str ns;
|
|
|
|
alt (n) {
|
|
|
|
case (0) {
|
|
|
|
ns = "no more bottles";
|
|
|
|
}
|
|
|
|
case (1) {
|
|
|
|
ns = "1 bottle";
|
|
|
|
}
|
|
|
|
case (_) {
|
2011-05-17 20:41:41 +02:00
|
|
|
ns = int::to_str(n, 10u) + " bottles";
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-17 20:41:41 +02:00
|
|
|
while (i < str::byte_len(t)) {
|
2010-09-20 22:08:28 +01:00
|
|
|
if (t.(i) == ('#' as u8)) {
|
|
|
|
b += ns;
|
|
|
|
}
|
|
|
|
else {
|
2011-05-17 20:41:41 +02:00
|
|
|
str::push_byte(b, t.(i));
|
2010-09-20 22:08:28 +01:00
|
|
|
}
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
ret b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Straightforward counter */
|
|
|
|
fn main() {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|