fork download
  1. using System;
  2. using System.Linq;
  3.  
  4. public class RomanNumeral
  5. {
  6. const int NaN = -1;
  7. private static int ToInteger(string roman_numeral) {
  8. string ro_num = roman_numeral.ToUpper().Trim();
  9. char[] ro_num_array = ro_num.ToCharArray();
  10. if (ro_num == "N") { return 0; }
  11.  
  12. char[] symbols = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
  13. int [] values = { 1, 5, 10, 50, 100, 500, 1000 };
  14. int int_num = 0;
  15. int symb_it = 0, symb_consec = 0;
  16.  
  17. bool to_parse = true;
  18. int subtrahend = 0, didgit = -1;
  19. foreach (char ro_num_char in ro_num_array.Reverse<char>()) {
  20. do
  21. {
  22. if (ro_num_char == symbols[symb_it])
  23. {
  24. symb_consec += (symb_it%2==1)? 3:1;
  25. if (symb_consec > 3) {
  26. Console.WriteLine("symb_consec>3");return NaN;
  27. }
  28. didgit = symb_it/2*2+2;
  29.  
  30.  
  31. subtrahend = ((symb_it-1)/2)*2;
  32. int_num += values[symb_it];
  33. to_parse = false;
  34.  
  35. }
  36. else if ((ro_num_char == symbols[subtrahend]))
  37. {
  38. if (didgit == symb_it) { return NaN; }
  39. didgit = symb_it;
  40. symb_consec = 0;
  41. int_num -= values[subtrahend];
  42. to_parse = false;
  43.  
  44. }
  45. if (to_parse) {
  46. symb_it++;
  47. if (symb_it > 6) { Console.WriteLine("symb_it>6");return NaN; }
  48. symb_consec = 0;
  49. }
  50. } while (to_parse);
  51.  
  52. to_parse = true;
  53. }
  54. return int_num;
  55. }
  56. public static void Main()
  57. {
  58. string numerator_r = "LVII";
  59. string denominator_r = "CCCLVII";
  60. string decimal_repr_s = "2013.1589041096";
  61.  
  62. //int test = RomanNumeral.ToInteger("MMMCMXCIX");
  63. Console.WriteLine(
  64. "2013/02/27 is not " +
  65. float.Parse(decimal_repr_s) +
  66. ". It should be 2013+(30+27)/365=" +
  67. (2013.0+ (30.0+27.0)/365.0).ToString() +
  68. " So that 01/01 is 2013.0\n\n" +
  69. "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"+
  70. RomanNumeral.ToInteger(numerator_r) + "=30+27. The numerator should have been LVIII (="+
  71. RomanNumeral.ToInteger("LVII")+ ")\n\nI hope you are entertained now FELLOW HUMAN"
  72. );
  73.  
  74. }
  75. }
Success #stdin #stdout 0.05s 23952KB
stdin
Standard input is empty
stdout
2013/02/27 is not 2013.159. It should be 2013+(30+27)/365=2013.15616438356 So that 01/01 is 2013.0

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:
57=30+27. The numerator should have been LVIII (=57)

I hope you are entertained now FELLOW HUMAN