Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.array;
- import std.conv;
- import std.stdio;
- void main(string[] args)
- {
- if (args.length != 2)
- {
- stderr.writeln("Usage:\n\tprogram.exe number\n");
- return;
- }
- string process(ulong n)
- {
- immutable string[ulong] dictionary = [
- 0: "",
- 1: "One",
- 2: "Two",
- 3: "Three",
- 4: "Four",
- 5: "Five",
- 6: "Six",
- 7: "Seven",
- 8: "Eight",
- 9: "Nine",
- 10: "Ten",
- 11: "Eleven",
- 12: "Twelve",
- 13: "Thirteen",
- 14: "Fourteen",
- 15: "Fifteen",
- 16: "Sixteen",
- 17: "Seventeen",
- 18: "Eighteen",
- 19: "Nineteen",
- 20: "Twenty",
- 30: "Thirty",
- 40: "Forty",
- 50: "Fifty",
- 60: "Sixty",
- 70: "Seventy",
- 80: "Eighty",
- 90: "Ninety"
- ];
- immutable string[ulong] levels = [
- 1: "",
- 1_000: "Thousand",
- 1_000_000: "Million",
- 1_000_000_000: "Billion",
- 1_000_000_000_000: "Trillion",
- 1_000_000_000_000_000: "Quadrillion",
- 1_000_000_000_000_000_000: "Quintillion"
- ];
- string s;
- while (n != 0)
- {
- static ulong currentLevel = 1;
- string m = to!string(n % 1_000);
- string hundreds;
- string tens;
- if (m.length == 3)
- {
- hundreds = dictionary[parse!ulong(m[0 .. 1])] ~ "Hundred";
- m = m[1 .. $];
- }
- if (m.length == 2)
- {
- if (m[0] == '1')
- tens ~= dictionary[parse!ulong(m[0 .. $])];
- else
- if (m[0] != '0')
- tens ~= dictionary[parse!ulong(m[0 .. 1]) * 10];
- if (m[1] != '0' && m[0] != '1')
- tens ~= dictionary[parse!ulong(m[1 .. 2])];
- }
- else
- if (m.length == 1)
- tens ~= dictionary[parse!ulong(m[0 .. $])];
- if (!tens.empty && n >= 1_000)
- hundreds ~= "And";
- s = hundreds ~ tens ~ (hundreds.empty && tens.empty ? "" : levels[currentLevel]) ~ s;
- currentLevel *= 1_000;
- n /= 1_000;
- }
- return s;
- }
- writeln(process(parse!ulong(args[1])));
- }
Advertisement
Add Comment
Please, Sign In to add comment