Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace ex2
- {
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- // Makes a list of tuples for numerals I through M.
- public static List<Tuple<char, int>> make_tuple()
- {
- return new List<Tuple<char, int>>(){
- new Tuple<char, int>('M', 1000),
- new Tuple<char, int>('D', 500),
- new Tuple<char, int>('C', 100),
- new Tuple<char, int>('L', 50),
- new Tuple<char, int>('X', 10),
- new Tuple<char, int>('V', 5),
- new Tuple<char, int>('I', 1)
- };
- }
- // Compares two values for a ratio. The maximal ratio
- // between two numbers in the Roman numeral system,
- // it turns out, is 0.1. If this condition is not met,
- // throw an exception.
- public static void verify_combo(int first, int second)
- {
- if ((double)first / (double)second >= 0.1)
- {
- // Do nothing
- }
- else
- {
- throw new Exception();
- }
- }
- // In case of Roman numeral combos (e.g., IV),
- // the second value is always bigger than the first.
- // This funcion tests against that.
- public static bool is_combo(int first, int second)
- {
- if (first < second)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- // Compares preceeding, current and succeeding numeral digits.
- // Returns current digit if no combo is present.
- // Returns combo value if current digit and succeeding digit are a combo.
- // Returns 0 if current digit and preceeding digit are a combo.
- public static int compare(char[] numeral, int index)
- {
- List<Tuple<char, int>> NUMERALS = make_tuple();
- // Generate dictionary from list of tuples.
- Dictionary<char, int> NUMERALS_DICT = new Dictionary<char, int>();
- foreach (Tuple<char, int> pair in NUMERALS)
- {
- NUMERALS_DICT.Add(pair.Item1, pair.Item2);
- }
- // Initialise integer values.
- int current = NUMERALS_DICT[numeral[index]];
- if (index != numeral.Length - 1)
- {
- // next is defined here because C# does not
- // have dynamic typing.
- int next = NUMERALS_DICT[numeral[index + 1]];
- if (index != numeral.Length-1 && is_combo(current, next))
- {
- verify_combo(current, next);
- return next - current;
- }
- }
- if (index > 0)
- {
- // previous is defined here because C# does not
- // have dynamic typing.
- int previous = NUMERALS_DICT[numeral[index - 1]];
- if (is_combo(previous, current))
- {
- verify_combo(previous, current);
- return 0;
- }
- }
- return current;
- }
- // Converts a numeral string value to a decimal int value.
- public static int numeral_to_decimal(string numeral)
- {
- int dec = 0;
- char[] num = numeral.ToCharArray();
- // Loops through ever letter in numeral. Adds returned value from
- // compare() to dec.
- for (int i = 0; i < numeral.Length; i++)
- {
- dec += compare(num, i);
- }
- return dec;
- }
- // Converts a decimal int value to a numeral string value.
- public static string decimal_to_numeral(int dec)
- {
- string num = "";
- List<Tuple<char, int>> NUMERALS = make_tuple();
- // For every possible letter from the tuple list.
- for (int i = 0; i < NUMERALS.Count; i++)
- {
- // Perform below operations until dec is smaller than the
- // corresponding value of the letter.
- while (dec >= NUMERALS[i].Item2)
- {
- // The magic happens below. Seriously, I don't know how to
- // document this.
- if (dec.ToString().StartsWith("9")
- && NUMERALS[i].Item2.ToString().StartsWith("5"))
- {
- break;
- }
- else if (dec.ToString().StartsWith("4"))
- {
- dec -= (int)((double)NUMERALS[i-1].Item2 * 0.8);
- num += NUMERALS[i].Item1.ToString() + NUMERALS[i-1].Item1.ToString();
- }
- else if (dec.ToString().StartsWith("9"))
- {
- dec -= (int)((double)NUMERALS[i-2].Item2 * 0.9);
- num += NUMERALS[i].Item1.ToString() + NUMERALS[i-2].Item1.ToString();
- }
- else
- {
- dec -= NUMERALS[i].Item2;
- num += NUMERALS[i].Item1.ToString();
- }
- }
- }
- return num;
- }
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new converter());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment