private static int IncrementOrResetRepeatCount(int repeat, RomanCharacter current, RomanCharacter next)
 {
     return current.Value == next.Value ? repeat + 1 : 1;
 }
 private static string RomanNumeralForNumber(int number, RomanCharacter romanCharacter)
 {
     int digit = PlaceValueForRomanNumeral(number, romanCharacter);
     if (digit == 9)
         return String.Concat(romanCharacter.Character, romanCharacter.TenOfThese.Character);
     if (digit == 4)
         return String.Concat(romanCharacter.Character, romanCharacter.FiveOfThese.Character);
     string roman = "";
     if (digit >= 5)
     {
         roman = romanCharacter.FiveOfThese.Character.ToString();
         digit -= 5;
     }
     roman += new string(romanCharacter.Character, digit);
     return roman;
 }
 private static int ShouldAddOrSubtractTheCurrentValue(RomanCharacter current, RomanCharacter prevCharacter)
 {
     if (prevCharacter == null)
         return 1;
     return prevCharacter.Decrementor == current ? -1 : 1;
 }
 /// <summary>
 /// Return the digit for a place value; for instance, for Roman Numeral C (100)
 ///   and number 3214, return 2 (the hundred's place)
 /// </summary>
 private static int PlaceValueForRomanNumeral(int number, RomanCharacter roman)
 {
     int digit = number%(roman.Value*10)/roman.Value;
     if (digit < 0 || digit > 10)
         throw new ArithmeticException("digit must be between 0 and 9 inclusive");
     return digit;
 }
 private static bool NextIsValidRegular(RomanCharacter current, RomanCharacter next, RomanCharacter maxSoFar)
 {
     return next.Value >= current.Value && next.Value >= maxSoFar.Value;
 }
 private static bool NextIsValidDecrement(RomanCharacter current, RomanCharacter next, RomanCharacter previous, int repeat)
 {
     var prevValue = previous == null ? 0 : previous.Value;
     bool currentDecrements = current.Value < prevValue;
     bool nextDecrements = next.Value < current.Value;
     if (nextDecrements && currentDecrements)
         return false;
     bool nextIsValidDecrement =
         nextDecrements
         && next.Value == current.Decrementor.Value
         && repeat == 1
         ;
     return nextIsValidDecrement;
 }
 private static bool IsCurrentValueValid(RomanCharacter current, RomanCharacter next, RomanCharacter previous, RomanCharacter maxSoFar, int repeat)
 {
     if (NextIsValidDecrement(current, next, previous, repeat))
         return true;
     if (NextIsValidRegular(current, next, maxSoFar))
         return true;
     return false;
 }
示例#8
0
        private static void Init()
        {
            I = new RomanCharacter { Character = 'I', Value = 1,    Decrementor = null };
            V = new RomanCharacter { Character = 'V', Value = 5,    Decrementor = I    };
            X = new RomanCharacter { Character = 'X', Value = 10,   Decrementor = I    };
            L = new RomanCharacter { Character = 'L', Value = 50,   Decrementor = X    };
            C = new RomanCharacter { Character = 'C', Value = 100,  Decrementor = X    };
            D = new RomanCharacter { Character = 'D', Value = 500,  Decrementor = C    };
            M = new RomanCharacter { Character = 'M', Value = 1000, Decrementor = C    };

            I.FiveOfThese = V;
            I.TenOfThese = X;
            X.FiveOfThese = L;
            X.TenOfThese = C;
            C.FiveOfThese = D;
            C.TenOfThese = M;

            Symbols = new Dictionary<char, RomanCharacter>
                          {
                              {I.Character, I},
                              {V.Character, V},
                              {X.Character, X},
                              {L.Character, L},
                              {C.Character, C},
                              {D.Character, D},
                              {M.Character, M}
                          };
        }