Ejemplo n.º 1
0
        /// <summary>
        /// With subtractive notation, XIV does not represent the same number as XVI (16) but rather is an alternative
        /// way of writing XIIII (14).
        /// </summary>
        private void CalculateSubtractiveNotation(RomanDigit romanDigit)
        {
            UndoLastAddition();
            int combinedValue = CombineWithPreviousValue(romanDigit);

            AddValue(combinedValue);
        }
 public EscalatingDigitConverter(RomanDigit letterForValueOfOne,
                                 RomanDigit letterForValueOfFive, RomanDigit letterForValueOfTen)
 {
     _letterForValueOfOne  = Validate.NotNull(letterForValueOfOne);
     _letterForValueOfFive = Validate.NotNull(letterForValueOfFive);
     _letterForValueOfTen  = Validate.NotNull(letterForValueOfTen);
 }
Ejemplo n.º 3
0
        /**
         * Changes an int into a lower case roman number.
         * @param index the original number
         * @return the roman number (lower case)
         */
        public static String GetString(int index)
        {
            StringBuilder buf = new StringBuilder();

            // lower than 0 ? Add minus
            if (index < 0)
            {
                buf.Append('-');
                index = -index;
            }

            // greater than 3000
            if (index > 3000)
            {
                buf.Append('|');
                buf.Append(GetString(index / 1000));
                buf.Append('|');
                // remainder
                index = index - (index / 1000) * 1000;
            }

            // number between 1 and 3000
            int pos = 0;

            while (true)
            {
                // loop over the array with values for m-d-c-l-x-v-i
                RomanDigit dig = roman[pos];
                // adding as many digits as we can
                while (index >= dig.value)
                {
                    buf.Append(dig.digit);
                    index -= dig.value;
                }
                // we have the complete number
                if (index <= 0)
                {
                    break;
                }
                // look for the next digit that can be used in a special way
                int j = pos;
                while (!roman[++j].pre)
                {
                    ;
                }

                // does the special notation apply?
                if (index + roman[j].value >= dig.value)
                {
                    buf.Append(roman[j].digit).Append(dig.digit);
                    index -= dig.value - roman[j].value;
                }
                pos++;
            }
            return(buf.ToString());
        }
Ejemplo n.º 4
0
        public void Add(RomanDigit romanDigit)
        {
            bool previousValueWasSmaller = _lastDigit != null && romanDigit.Value > _lastDigit.Value;

            if (previousValueWasSmaller)
            {
                CalculateSubtractiveNotation(romanDigit);
            }
            else
            {
                AddValue(romanDigit.Value);
            }
            _lastDigit = romanDigit;
        }
 /// <summary>
 /// DigitToInt
 /// </summary>
 public static int DigitToInt(RomanDigit digit)
 {
     switch (digit)
     {
         case RomanDigit.I:
             return 1;
         case RomanDigit.V:
             return 5;
         case RomanDigit.X:
             return 10;
         default:
             return 0;
     }
 }
Ejemplo n.º 6
0
 public static RomanDigit[] RomanStringToRomanDigitArray(string romanString)
 {
     try
     {
         RomanDigit[] rda = new RomanDigit[romanString.Length];
         for (int i = 0; i < romanString.Length; i++)
         {
             rda[i] = (RomanDigit)Enum.Parse(typeof(RomanDigit), new String(romanString[i], 1));
         }
         return(rda);
     }
     catch (Exception e)
     {
         throw new System.InvalidOperationException("The roman string is not representing a Roman number", e);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// DigitToInt
        /// </summary>
        public static int DigitToInt(RomanDigit digit)
        {
            switch (digit)
            {
            case RomanDigit.I:
                return(1);

            case RomanDigit.V:
                return(5);

            case RomanDigit.X:
                return(10);

            default:
                return(0);
            }
        }
        public Attempt <int> Convert(string input)
        {
            bool isARomanNumber = _romanNumeralValidator.IsARomanNumber(input);

            if (!isARomanNumber)
            {
                return(new Attempt <int>(ErrorRegistry.NotARomanNumeral));
            }

            _valueGatherer.Reset();
            foreach (char character in input)
            {
                RomanDigit romanDigit = RomanDigit.All.First(x => x.Letter == character);
                _valueGatherer.Add(romanDigit);
            }

            int result = _valueGatherer.GetValue();

            return(new Attempt <int>(result));
        }
Ejemplo n.º 9
0
 private int CombineWithPreviousValue(RomanDigit romanDigit)
 {
     return(romanDigit.Value - _lastDigit.Value);
 }
Ejemplo n.º 10
0
        /**
         * Changes an int into a lower case roman number.
         * @param number the original number
         * @return the roman number (lower case)
         */
        public static String ToRomanLowerCase(int number)
        {
            // Buffer
            StringBuilder buf = new StringBuilder();

            // kleiner 0 ? Vorzeichen festlegen
            if (number < 0)
            {
                buf.Append('-');
                number = -number;
            }

            // größer 3000
            if (number > 3000)
            {
                // rekursiver Aufruf (ohne tausender-Bereich)
                buf.Append('|');
                buf.Append(ToRomanLowerCase(number / 1000));
                buf.Append('|');
                // tausender-Bereich
                number = number - (number / 1000) * 1000;
            }

            // Schleife
            int pos = 0;

            while (true)
            {
                // roman-array durchlaufen
                RomanDigit dig = roman[pos];

                // solange Zahl größer roman-Wert
                while (number >= dig.value)
                {
                    // Zeichen hinzufügen
                    buf.Append(dig.digit);
                    // Wert des Zeichens abziehen
                    number -= dig.value;
                }

                // Abbruch
                if (number <= 0)
                {
                    break;
                }
                // pre=false suchen (ab Stelle pos)
                int j = pos;
                while (!roman[++j].pre)
                {
                    ;
                }

                // neuer Wert größer
                if (number + roman[j].value >= dig.value)
                {
                    // hinzufügen
                    buf.Append(roman[j].digit).Append(dig.digit);
                    // Wert vom Rest abziehen
                    number -= dig.value - roman[j].value;
                }
                pos++;
            }
            return(buf.ToString());
        }
 /// <summary>
 /// DigitToInt
 /// </summary>
 public static int DigitToInt(RomanDigit digit)
 {
     throw new Exception();
 }
 public void SetAlias(RomanDigit digit, string alias)
 {
     _aliases[alias] = digit;
 }
Ejemplo n.º 13
0
 public SimpleDigitConverter(RomanDigit romanDigit)
 {
     _romanDigit = Validate.NotNull(romanDigit);
 }
Ejemplo n.º 14
0
 /// <summary>
 /// DigitToInt
 /// </summary>
 public static int DigitToInt(RomanDigit digit)
 {
     throw new Exception();
 }
Ejemplo n.º 15
0
 public override void InitialseGalaxySystem()
 {
     RomanDigit = new RomanDigit();
     Metals     = new List <Metal>();
     Subtitutes = new List <Subtitute>();
 }
 public NumberAliasStatement(RomanDigit digit, string alias)
 {
     Digit = digit;
     Alias = alias;
 }