Guest User

Duoas' Challenge!

a guest
Jul 3rd, 2012
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.86 KB | None | 0 0
  1. import std.array;
  2. import std.conv;
  3. import std.stdio;
  4.  
  5. void main(string[] args)
  6. {
  7.     if (args.length != 2)
  8.     {
  9.         stderr.writeln("Usage:\n\tprogram.exe number\n");
  10.         return;
  11.     }
  12.  
  13.     string process(ulong n)
  14.     {
  15.         immutable string[ulong] dictionary = [
  16.             0: "",
  17.             1: "One",
  18.             2: "Two",
  19.             3: "Three",
  20.             4: "Four",
  21.             5: "Five",
  22.             6: "Six",
  23.             7: "Seven",
  24.             8: "Eight",
  25.             9: "Nine",
  26.             10: "Ten",
  27.             11: "Eleven",
  28.             12: "Twelve",
  29.             13: "Thirteen",
  30.             14: "Fourteen",
  31.             15: "Fifteen",
  32.             16: "Sixteen",
  33.             17: "Seventeen",
  34.             18: "Eighteen",
  35.             19: "Nineteen",
  36.             20: "Twenty",
  37.             30: "Thirty",
  38.             40: "Forty",
  39.             50: "Fifty",
  40.             60: "Sixty",
  41.             70: "Seventy",
  42.             80: "Eighty",
  43.             90: "Ninety"
  44.         ];
  45.  
  46.         immutable string[ulong] levels = [
  47.             1: "",
  48.             1_000: "Thousand",
  49.             1_000_000: "Million",
  50.             1_000_000_000: "Billion",
  51.             1_000_000_000_000: "Trillion",
  52.             1_000_000_000_000_000: "Quadrillion",
  53.             1_000_000_000_000_000_000: "Quintillion"
  54.         ];
  55.  
  56.         string s;
  57.  
  58.         while (n != 0)
  59.         {
  60.             static ulong currentLevel = 1;
  61.             string m = to!string(n % 1_000);
  62.             string hundreds;
  63.             string tens;
  64.  
  65.             if (m.length == 3)
  66.             {
  67.                 hundreds = dictionary[parse!ulong(m[0 .. 1])] ~ "Hundred";
  68.                 m = m[1 .. $];
  69.             }
  70.  
  71.             if (m.length == 2)
  72.             {
  73.                 if (m[0] == '1')
  74.                     tens ~= dictionary[parse!ulong(m[0 .. $])];
  75.                 else
  76.                 if (m[0] != '0')
  77.                     tens ~= dictionary[parse!ulong(m[0 .. 1]) * 10];
  78.  
  79.                 if (m[1] != '0' && m[0] != '1')
  80.                     tens ~= dictionary[parse!ulong(m[1 .. 2])];
  81.             }
  82.             else
  83.             if (m.length == 1)
  84.                 tens ~= dictionary[parse!ulong(m[0 .. $])];
  85.  
  86.             if (!tens.empty && n >= 1_000)
  87.                 hundreds ~= "And";
  88.  
  89.             s = hundreds ~ tens ~ (hundreds.empty && tens.empty ? "" : levels[currentLevel]) ~ s;
  90.             currentLevel *= 1_000;
  91.             n /= 1_000;
  92.         }
  93.  
  94.         return s;
  95.     }
  96.  
  97.     writeln(process(parse!ulong(args[1])));
  98. }
Advertisement
Add Comment
Please, Sign In to add comment