Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

The Twelve Days of Christmas (Reprise)

Part of SwitchCaseCategory

Description

Another example that prints the lyrics to the Twelve Days of Christmas.

Produces the lyrics of the 12 Days of Christmas (all twelve verses). It has a few subtle changes from TwelveDaysOfChristmasExampleOne such as a break-less switch statement.

Example

const char[] I1 = "On the ";

const char[] I2 = " day of Christmas, my true love gave to me:";
const char[] D1 = "A Partridge in a Pear Tree.";
const char[] D2 = "Two Turtle Doves, and";

const char[] D3 = "Three French Hens,";
const char[] D4 = "Four Calling Birds,";
const char[] D5 = "Five Golden Rings,";

const char[] D6 = "Six Geese a Laying,";
const char[] D7 = "Seven Swans a Swimming,";
const char[] D8 = "Eight Maids a Milking,";

const char[] D9 = "Nine Ladies Dancing,";
const char[] D10 = "Ten Lords a Leaping,";
const char[] D11 = "Eleven Pipers Piping,";

const char[] D12 = "Twelve Drummers Drumming,";


void println(char[] s)
{
    printf(cast(char*) (s ~ "\n\0")); /* A simple \n\0 (without quotes) is valid in D, but d2html won't handle it */
}
 
 
int main (char[][] args)
{
    char[] s;
 
    for (int e=1; e<=12; e++)
    {
        s = I1;
               
        switch(e)       /* ...keeping the cases separate... */

        {
            case 1:  s ~= "first"; break;
            case 2:  s ~= "second"; break;
            case 3:  s ~= "third"; break;
            case 4:  s ~= "fourth"; break;
            case 5:  s ~= "fifth"; break;
            case 6:  s ~= "sixth"; break;
            case 7:  s ~= "seventh"; break;
            case 8:  s ~= "eighth"; break;
            case 9:  s ~= "ninth"; break;
            case 10: s ~= "tenth"; break;   
            case 11: s ~= "eleventh"; break;
            default: s ~= "twelveth"; break;
        }
        s ~= I2;
        println(s);
 
        switch (e)     /* ...and without the break statements... */
        {
            case 12: println(D12);
            case 11: println(D11);
            case 10: println(D10);
            case 9:  println(D9);
            case 8:  println(D8);
            case 7:  println(D7);
            case 6:  println(D6);
            case 5:  println(D5);
            case 4:  println(D4);
            case 3:  println(D3);
            case 2:  println(D2);
            default: println(D1); println(""); break;
        }
    }
    return 0;
}

Source

Link http://jcc_7.tripod.com/d/tutor/simple/twelve_days_2.html
Authors jcc7 (with modifications from Larry Cowan)