Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

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.01 in 174 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

VersionAuthorDateCommentsRate
1Stewart Gordon05/31/054
Template metaprogrammingDon Clugston07/11/061
Fiber "Ring"Bryan Knowles11/17/100
Div Operator Editionbadmadevil02/29/080
5Philipp Winterberg04/20/057

Comments

>>  Stewart Gordon said on 08/06/07 00:38:19

Stewart Gordon 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

qw18Ruby I always <a href=" http://www.gogetessays.com">buy an essay</a> or written essays just about this topic.

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!

Name:

eMail:

URL:

Security Code:
  
Comment: