Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Task: Generate the lyrics to 99 bottles.
- * I decided to challenge myself and make it spell out the numbers, which proved to be a very interesting challenge.
- * Because of the elegant way the versing is handled, this could be translated to a spanish version by merely changing the dictionaries and the verses.
- * In fact, I'll do that right now. Google Translate, ahoy!
- * -Braden
- */
- #include <stdio.h>
- #include <string.h>
- #define VERSE_1 "%s bottle%s of beer on the wall, %s bottle%s of beer.\n"
- #define VERSE_2 "Take one down, pass it around, %s bottle%s of beer on the wall.\n\n"
- #define VERSE_3 "No more bottles of beer on the wall, no more bottles of beer.\n"
- #define VERSE_4 "Go to the store, buy some more, ninety-nine bottles of beer on the wall.\n"
- #define SEPARATOR "-"
- const char *ones_dict[] = {"no more", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
- const char *tens_dict[] = {0, 0, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
- const char *teens_dict[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
- void to_english(char *buf, int bottles);
- int plural(int qty);
- void write_verse(int bottles);
- void
- to_english(char *buf, int bottles)
- {/*{{{*/
- int ones = bottles % 10,
- tens = (bottles - ones) / 10;
- if(bottles == 0){
- strncpy(buf, ones_dict[0], 30);
- } else if(tens == 0){
- strncpy(buf, ones_dict[ones], 30);
- } else if(tens == 1){
- strncpy(buf, teens_dict[ones], 30);
- } else if(ones == 0){
- strncpy(buf, tens_dict[tens], 30);
- } else {
- snprintf(buf, 30, "%s" SEPARATOR "%s", tens_dict[tens], ones_dict[ones]);
- }
- }/*}}}*/
- int
- plural(int qty)
- {/*{{{*/
- return qty != 1;
- }/*}}}*/
- void
- write_verse(int bottles)
- {/*{{{*/
- char numbuf[30] = "";
- to_english(numbuf, bottles);
- printf(VERSE_1, numbuf, plural(bottles) ? "s" : "", numbuf, plural(bottles) ? "s" : "");
- to_english(numbuf, bottles - 1);
- printf(VERSE_2, numbuf, plural(bottles - 1) ? "s" : "");
- if(bottles - 1 == 0){
- printf(VERSE_3 VERSE_4);
- }
- }/*}}}*/
- int
- main()
- {/*{{{*/
- int bottles = 99;
- while(bottles){
- write_verse(bottles);
- bottles--;
- }
- }/*}}}*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement