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

Part of SwitchCaseCategory

Description

Prints the lyrics to the Twelve Days of Christmas (all twelve verses).

TwelveDaysOfChristmasExampleTwo demonstrates different code to get the same result.

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++)
    {
        for (int d=e; d>0; d--)
        {   
            s = "";

            if (d==e)
            {
                s = I1;
                
                switch(d)
                {
                    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 (d)
            {
                case 12: println(D12); break;
                case 11: println(D11); break;
                case 10: println(D10); break;
                case 9:  println(D9); break;
                case 8:  println(D8); break;
                case 7:  println(D7); break;
                case 6:  println(D6); break;
                case 5:  println(D5); break;
                case 4:  println(D4); break;
                case 3:  println(D3); break;
                case 2:  println(D2); break;
                default: println(D1); println(""); break;
            }
        }
    }
    return 0;
}

Source

Link http://jcc_7.tripod.com/d/tutor/twelve_days.html
Author jcc7