using System; using System.Linq; public class RomanNumeral { const int NaN = -1; private static int ToInteger(string roman_numeral) { string ro_num = roman_numeral.ToUpper().Trim(); char[] ro_num_array = ro_num.ToCharArray(); if (ro_num == "N") { return 0; } char[] symbols = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' }; int [] values = { 1, 5, 10, 50, 100, 500, 1000 }; int int_num = 0; int symb_it = 0, symb_consec = 0; bool to_parse = true; int subtrahend = 0, didgit = -1; foreach (char ro_num_char in ro_num_array.Reverse()) { do { if (ro_num_char == symbols[symb_it]) { symb_consec += (symb_it%2==1)? 3:1; if (symb_consec > 3) { Console.WriteLine("symb_consec>3");return NaN; } didgit = symb_it/2*2+2; subtrahend = ((symb_it-1)/2)*2; int_num += values[symb_it]; to_parse = false; } else if ((ro_num_char == symbols[subtrahend])) { if (didgit == symb_it) { return NaN; } didgit = symb_it; symb_consec = 0; int_num -= values[subtrahend]; to_parse = false; } if (to_parse) { symb_it++; if (symb_it > 6) { Console.WriteLine("symb_it>6");return NaN; } symb_consec = 0; } } while (to_parse); to_parse = true; } return int_num; } public static void Main() { string numerator_r = "LVII"; string denominator_r = "CCCLVII"; string decimal_repr_s = "2013.1589041096"; //int test = RomanNumeral.ToInteger("MMMCMXCIX"); Console.WriteLine( "2013/02/27 is not " + float.Parse(decimal_repr_s) + ". It should be 2013+(30+27)/365=" + (2013.0+ (30.0+27.0)/365.0).ToString() + " So that 01/01 is 2013.0\n\n" + "The roman numeral representation should on the other hand be the other way around regarding 01/01 where it starts with an one instead of a zero:\n"+ RomanNumeral.ToInteger(numerator_r) + "=30+27. The numerator should have been LVIII (="+ RomanNumeral.ToInteger("LVII")+ ")\n\nI hope you are entertained now FELLOW HUMAN" ); } }