Beispiel #1
0
        public static ArabicNumeral ConvertRomanNumeral(string romanNumeral)
        {
            if (String.IsNullOrEmpty(romanNumeral))
            {
                throw new ArgumentException("The Roman Numeral to convert cannot be null or empty.", nameof(romanNumeral));
            }

            ArabicNumeral arabicNumeral = null;

            if (ARABIC_NUMERALS_MAP.Keys.Contains(romanNumeral))
            {
                arabicNumeral = ARABIC_NUMERALS_MAP[romanNumeral];
            }

            return(arabicNumeral);
        }
Beispiel #2
0
        public static string ConvertRomanNumber(string romanNumber)
        {
            if (String.IsNullOrEmpty(romanNumber))
            {
                throw new ArgumentException("The Roman Numeral to convert cannot be null or empty.", nameof(romanNumber));
            }

            int?arabicNumber = 0;

            for (int i = 0; i < romanNumber.Length && arabicNumber.HasValue; i++)
            {
                ArabicNumeral arabicNumeral = ConvertRomanNumeral(romanNumber[i].ToString());

                if (arabicNumeral != null)
                {
                    ArabicNumeral nextArabicNumeral = null;

                    if (i + 1 < romanNumber.Length)
                    {
                        nextArabicNumeral = ConvertRomanNumeral(romanNumber[i + 1].ToString());
                    }

                    if ((nextArabicNumeral != null) && (nextArabicNumeral.IntegerValue > arabicNumeral.IntegerValue))
                    {
                        if (((nextArabicNumeral.IntegerValue != arabicNumeral.IntegerValue * 5) && (nextArabicNumeral.IntegerValue != arabicNumeral.IntegerValue * 10)) ||
                            (arabicNumeral.IntegerValue == 5) || (arabicNumeral.IntegerValue == 50) || (arabicNumeral.IntegerValue == 500))
                        {
                            arabicNumber = null;
                        }
                        else
                        {
                            arabicNumber -= arabicNumeral.IntegerValue;
                        }
                    }
                    else
                    {
                        arabicNumber += arabicNumeral.IntegerValue;
                    }
                }
            }

            return(arabicNumber.HasValue ? arabicNumber.ToString() : String.Empty);
        }