Advertisement
cortez

Number_as_Words

Sep 22nd, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Problem_11__Number_as_Words
  4. {
  5.     class NumberAsWords
  6.     {
  7.         public static void Main ()
  8.         {
  9.             Console.WriteLine ("Please enter an integer from 0 to 999 inclusive:");
  10.             ushort number = 0;
  11.  
  12.             try
  13.             {
  14.                 number = ushort.Parse (Console.ReadLine());
  15.             }
  16.             catch (Exception e)
  17.             {
  18.                 if (e is FormatException)
  19.                 {
  20.                     Exception fe = new FormatException ("Error! Input was not in the correct format!");
  21.                     Console.WriteLine (fe.Message);
  22.                 }
  23.                 else if (e is OverflowException)
  24.                 {
  25.                     Exception oe = new OverflowException("Error! Input should be a positive 16-bit integer!");
  26.                     Console.WriteLine (oe.Message);
  27.                 }
  28.                 else
  29.                 {
  30.                     Console.WriteLine ("Error occured!!!");
  31.                 }
  32.                 return;
  33.             }
  34.  
  35.             if (number < 0 || number > 999)
  36.             {
  37.                 Console.WriteLine ("Invalid Input! Number should be in the range 0 - 999!");
  38.                 return;
  39.             }
  40.  
  41.             // Declare the output...
  42.             string output = "";
  43.  
  44.             // Set some important values (I'll use arrays)...
  45.             /*-------------------------------------------------*/
  46.             // First array will store the names of the first 20 numbers (from 0 to 19).
  47.             string[] singles = {
  48.                 "Zero", "One", "Two",
  49.                 "Three", "Four", "Five",
  50.                 "Six", "Seven", "Eight",
  51.                 "Nine", "Ten", "Eleven",
  52.                 "Twelve", "Thirteen", "Fourteen",
  53.                 "Fifteen", "Sixteen", "Seventeen",
  54.                 "Eighteen", "Nineteen"
  55.             };
  56.             // Second array will store the names of the decimals (20, 30, ..., 90).
  57.             string[] decimals = {
  58.                 "Twenty", "Thirty", "Fourty",
  59.                 "Fifty", "Sixty", "Seventy",
  60.                 "Eighty", "Ninety"
  61.             };
  62.  
  63.             // Here comes the Magic... :-)
  64.  
  65.             // Some bools to make it easier...
  66.             bool isBetween0and19 = (number >= 0) && (number <= 19);
  67.             bool isBetween19and99 = (number > 19) && (number <= 99);
  68.             bool isBeween100and999 = (number >= 100) && (number <= 999);
  69.  
  70.             // Now, we have 3 cases...
  71.             // Range(0-19), Range(19-99) and Range(100-999)
  72.  
  73.             // 0-19
  74.             if (isBetween0and19)
  75.             {
  76.                 output = TransformLowRangeNumber (number, singles);
  77.             }
  78.             // 19-99
  79.             else if (isBetween19and99)
  80.             {
  81.                 output = TransformMiddleRangeNumber (number, singles, decimals);
  82.             }
  83.             // 100-999
  84.             else if (isBeween100and999)
  85.             {
  86.                 output = TransformHighRangeNumber (number, singles, decimals);
  87.             }
  88.  
  89.             Console.WriteLine (output);
  90.         }
  91.  
  92.         public static string TransformLowRangeNumber (ushort number, string[] singles)
  93.         {
  94.             return singles [number];
  95.         }
  96.  
  97.         public static string TransformMiddleRangeNumber (ushort number, string[] singles, string[] decimals)
  98.         {
  99.             ushort ones = (ushort)(number % 10);
  100.             ushort decs = (ushort)((number / 10) % 10);
  101.  
  102.             if (ones == 0)
  103.             {
  104.                 return decimals [decs - 2];
  105.             }
  106.  
  107.             return decimals [decs - 2] + " " + singles[ones].ToLower();
  108.         }
  109.  
  110.         static string TransformHighRangeNumber (ushort number, string[] singles, string[] decimals)
  111.         {
  112.             ushort ones = (ushort)(number % 10);
  113.             ushort decs = (ushort)((number / 10) % 10);
  114.             ushort hunds = (ushort)((number / 100) % 10);
  115.  
  116.             if (number == 100)
  117.             {
  118.                 return singles[hunds] + " hundred";
  119.             }
  120.  
  121.             if (decs < 2)
  122.             {
  123.                 ones += (ushort)(10 * decs);
  124.                 return singles [hunds] + " hundred and " + singles [ones].ToLower();
  125.             }
  126.             else
  127.             {
  128.                 if (ones == 0)
  129.                 {
  130.                     return singles [hunds] + " hundred and " + decimals [decs - 2].ToLower();
  131.                 }
  132.                 return singles [hunds] + " hundred and " + decimals [decs - 2].ToLower() + " " + singles [ones].ToLower();
  133.             }
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement