Rubykuby

Program.cs

Feb 9th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Windows.Forms;
  6.  
  7. namespace ex2
  8. {
  9.     static class Program
  10.     {
  11.         /// <summary>
  12.         /// The main entry point for the application.
  13.         /// </summary>
  14.         [STAThread]
  15.  
  16.         // Makes a list of tuples for numerals I through M.
  17.         public static List<Tuple<char, int>> make_tuple()
  18.         {
  19.             return new List<Tuple<char, int>>(){
  20.                 new Tuple<char, int>('M', 1000),
  21.                 new Tuple<char, int>('D', 500),
  22.                 new Tuple<char, int>('C', 100),
  23.                 new Tuple<char, int>('L', 50),
  24.                 new Tuple<char, int>('X', 10),
  25.                 new Tuple<char, int>('V', 5),
  26.                 new Tuple<char, int>('I', 1)
  27.             };
  28.         }
  29.  
  30.         // Compares two values for a ratio. The maximal ratio
  31.         // between two numbers in the Roman numeral system,
  32.         // it turns out, is 0.1. If this condition is not met,
  33.         // throw an exception.
  34.         public static void verify_combo(int first, int second)
  35.         {
  36.             if ((double)first / (double)second >= 0.1)
  37.             {
  38.                 // Do nothing
  39.             }
  40.             else
  41.             {
  42.                 throw new Exception();
  43.             }
  44.         }
  45.  
  46.         // In case of Roman numeral combos (e.g., IV),
  47.         // the second value is always bigger than the first.
  48.         // This funcion tests against that.
  49.         public static bool is_combo(int first, int second)
  50.         {
  51.             if (first < second)
  52.             {
  53.                 return true;
  54.             }
  55.             else
  56.             {
  57.                 return false;
  58.             }
  59.         }
  60.  
  61.         // Compares preceeding, current and succeeding numeral digits.
  62.         // Returns current digit if no combo is present.
  63.         // Returns combo value if current digit and succeeding digit are a combo.
  64.         // Returns 0 if current digit and preceeding digit are a combo.
  65.         public static int compare(char[] numeral, int index)
  66.         {
  67.             List<Tuple<char, int>> NUMERALS = make_tuple();
  68.  
  69.             // Generate dictionary from list of tuples.
  70.             Dictionary<char, int> NUMERALS_DICT = new Dictionary<char, int>();
  71.             foreach (Tuple<char, int> pair in NUMERALS)
  72.             {
  73.                 NUMERALS_DICT.Add(pair.Item1, pair.Item2);
  74.             }
  75.  
  76.             // Initialise integer values.
  77.             int current = NUMERALS_DICT[numeral[index]];
  78.  
  79.             if (index != numeral.Length - 1)
  80.             {
  81.                 // next is defined here because C# does not
  82.                 // have dynamic typing.
  83.                 int next = NUMERALS_DICT[numeral[index + 1]];
  84.  
  85.                 if (index != numeral.Length-1 && is_combo(current, next))
  86.                 {
  87.                     verify_combo(current, next);
  88.                     return next - current;
  89.                 }
  90.             }
  91.  
  92.             if (index > 0)
  93.             {
  94.                 // previous is defined here because C# does not
  95.                 // have dynamic typing.
  96.                 int previous = NUMERALS_DICT[numeral[index - 1]];
  97.  
  98.                 if (is_combo(previous, current))
  99.                 {
  100.                     verify_combo(previous, current);
  101.                     return 0;
  102.                 }
  103.             }
  104.  
  105.             return current;
  106.         }
  107.  
  108.         // Converts a numeral string value to a decimal int value.
  109.         public static int numeral_to_decimal(string numeral)
  110.         {
  111.             int dec = 0;
  112.             char[] num = numeral.ToCharArray();
  113.  
  114.             // Loops through ever letter in numeral. Adds returned value from
  115.             // compare() to dec.
  116.             for (int i = 0; i < numeral.Length; i++)
  117.             {
  118.                 dec += compare(num, i);
  119.             }
  120.  
  121.             return dec;
  122.         }
  123.  
  124.         // Converts a decimal int value to a numeral string value.
  125.         public static string decimal_to_numeral(int dec)
  126.         {
  127.             string num = "";
  128.             List<Tuple<char, int>> NUMERALS = make_tuple();
  129.  
  130.             // For every possible letter from the tuple list.
  131.             for (int i = 0; i < NUMERALS.Count; i++)
  132.             {
  133.                 // Perform below operations until dec is smaller than the
  134.                 // corresponding value of the letter.
  135.                 while (dec >= NUMERALS[i].Item2)
  136.                 {
  137.                     // The magic happens below. Seriously, I don't know how to
  138.                     // document this.
  139.                     if (dec.ToString().StartsWith("9")
  140.                         && NUMERALS[i].Item2.ToString().StartsWith("5"))
  141.                     {
  142.                         break;
  143.                     }
  144.  
  145.                     else if (dec.ToString().StartsWith("4"))
  146.                     {
  147.                         dec -= (int)((double)NUMERALS[i-1].Item2 * 0.8);
  148.                         num += NUMERALS[i].Item1.ToString() + NUMERALS[i-1].Item1.ToString();
  149.                     }
  150.  
  151.                     else if (dec.ToString().StartsWith("9"))
  152.                     {
  153.                         dec -= (int)((double)NUMERALS[i-2].Item2 * 0.9);
  154.                         num += NUMERALS[i].Item1.ToString() + NUMERALS[i-2].Item1.ToString();
  155.                     }
  156.                     else
  157.                     {
  158.                         dec -= NUMERALS[i].Item2;
  159.                         num += NUMERALS[i].Item1.ToString();
  160.                     }
  161.                 }
  162.             }
  163.  
  164.             return num;
  165.         }
  166.  
  167.         static void Main()
  168.         {
  169.             Application.EnableVisualStyles();
  170.             Application.SetCompatibleTextRenderingDefault(false);
  171.             Application.Run(new converter());
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment