Language D
(D feuture's galore)
Date: | 10/09/05 |
Author: | Fredrik Olsson |
URL: | n/a |
Comments: | 2 |
Info: | http://www.digitalmars.com/d/ |
Score: | (3.02 in 179 votes) |
/* bob.d bob - 99 Bottles of beer Numbers in written form correct grammar and last verse included. Uses D features such as; Nested functions, in two levels; numToBottles and numToText. Dynamic arrays, static such in numToText for text lookups, and as strings. Strings in switches. Imports, stdio to get writefl()/writefln(), string for capitalize(). Created by Fredrik Olsson on 2005-10-09. Copyright (c) 2005 Treyst AB. All rights reserved. */ module bob; private { import std.stdio; import std.string; } int main(char[][] args) { char[] numToBottles(uint num) { char[] numToText() { static char[][] nums = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"]; static char[][] decs = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]; if (num == 0) return "no more"; if (num < 13) return nums[num]; if (num < 20) { char[] tmp = nums[num % 10]; switch(tmp) { case "three": return "thirteen"; case "five": return "fifteen"; default: return tmp ~ "teen"; } } return decs[(num / 10) - 2] ~ (!(num % 10) ? "" : "-" ~ nums[num % 10]); } return capitalize(numToText()) ~ " bottle" ~ ((num != 1) ? "s" : ""); } int bottles = 99; char[] temp = numToBottles(bottles); while (bottles >= 0) { writefln(temp, " of beer on the wall, ", temp, " of beer."); bottles--; if (bottles >= 0) { writef("Take one down, pass it around,"); temp = numToBottles(bottles); } else { writef("Go to the store and buy some more, "); temp = numToBottles(99); } writefln(temp, " of beer on the wall.\n"); } return 0; }
Download Source | Write Comment
Alternative Versions
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
1 | Stewart Gordon | 05/31/05 | 4 | |
Template metaprogramming | Don Clugston | 07/11/06 | 1 | |
Fiber "Ring" | Bryan Knowles | 11/17/10 | 0 | |
Div Operator Edition | badmadevil | 02/29/08 | 0 | |
5 | Philipp Winterberg | 04/20/05 | 7 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
Stewart Gordon said on 08/06/07 00:38:19
Nice effort. Just a few imperfections:
- missing a space after the comma between the third and fourth half-lines
- "eighteen" is misspelled
- with the verses run into two lines instead of four, the middle capital doesn't look right
qw18Ruby said on 01/13/10 10:25:05
I always <a href=" http://www.gogetessays.com">buy an essay</a> or written essays just about this topic.