/// <summary>
        ///     Initializes the collection of roman numerals.
        /// </summary>
        static RomanNumeralConverter()
        {
            var romanNumeralI = new RomanNumeral(1, 'I');
            var romanNumeralV = new RomanNumeral(5, 'V');
            var romanNumeralX = new RomanNumeral(10, 'X');
            var romanNumeralL = new RomanNumeral(50, 'L');
            var romanNumeralC = new RomanNumeral(100, 'C');
            var romanNumeralD = new RomanNumeral(500, 'D');
            var romanNumeralM = new RomanNumeral(1000, 'M');

            romanNumeralV.SubtractiveNumeral = romanNumeralI;
            romanNumeralX.SubtractiveNumeral = romanNumeralI;
            romanNumeralL.SubtractiveNumeral = romanNumeralX;
            romanNumeralC.SubtractiveNumeral = romanNumeralX;
            romanNumeralD.SubtractiveNumeral = romanNumeralC;
            romanNumeralM.SubtractiveNumeral = romanNumeralC;

            numerals = new LinkedList<RomanNumeral>(new RomanNumeral[]
            {
                    romanNumeralI,
                    romanNumeralV,
                    romanNumeralX,
                    romanNumeralL,
                    romanNumeralC,
                    romanNumeralD,
                    romanNumeralM
            });

            lowestNumeral = numerals.First;
            highestNumeral = numerals.Last;
        }
 public void LessThanZeroTest()
 {
     for (int i = 1; i <= 3; i++)
     {
         RomanNumeral romanNumeral = RomanNumeral.Retrieve(i);
         romanNumeral.ConvertArabicNumeral(-1);
     }
 }
 public void GreaterThanNineTest()
 {
     for (int i = 1; i <= 3; i++)
     {
         RomanNumeral romanNumeral = RomanNumeral.Retrieve(i);
         romanNumeral.ConvertArabicNumeral(10);
     }
 }
 public void NotNullTest()
 {
     for (int i = 1; i <= 3; i++)
     {
         RomanNumeral romanNumeral = RomanNumeral.Retrieve(i);
         Assert.IsNotNull(romanNumeral.ConvertArabicNumeral(1));
     }
 }
Exemple #5
0
    public static void Main()
    {
        RomanNumeral  roman = new RomanNumeral(122);
        BinaryNumeral binary;

        binary = roman;
        roman  = (RomanNumeral)binary;
    }
        public void should_convert_arabic_numeral_to_roman_numeral([Range(1, 20)] int arabicNumeral)
        {
            RomanNumeral romanNumeral = new RomanNumeral(arabicNumeral);

            string expectedValue = _arabicToRomanNumeralsMapping[arabicNumeral];

            Assert.AreEqual(romanNumeral.ToRomanNumeral(), expectedValue);
        }
        public void should_convert_roman_numeral_to_arabic_numeral([Range(1, 20)] int arabicNumeral)
        {
            RomanNumeral romanNumeral = new RomanNumeral(_arabicToRomanNumeralsMapping[arabicNumeral]);

            int expectecdValue = arabicNumeral;

            Assert.AreEqual(romanNumeral.ToInt(), expectecdValue);
        }
Exemple #8
0
        public void return_true_when_the_next_roman_symbol_value_can_be_substracted_by_the_previous_one()
        {
            string       romanSymbol        = "IX";
            RomanNumeral romanNumeral       = new RomanNumeral();
            bool         answerBeSubtracted = romanNumeral.IsSubtractableBy(romanSymbol);

            Check.That(answerBeSubtracted).IsEqualTo(true);
        }
Exemple #9
0
        public void OperatorToPowerOf_ValidRomanNumerals_SetsResult(string a, string b, string expected)
        {
            RomanNumeral romanNumber1 = new RomanNumeral(a);
            RomanNumeral romanNumber2 = new RomanNumeral(b);

            RomanNumeral result = romanNumber1 ^ romanNumber2;

            Assert.AreEqual(expected, result.romanNumeralStr);
        }
Exemple #10
0
        public void OperatorSubstract_ValidRomanNumerals_SetsDifference(string a, string b, string expected)
        {
            RomanNumeral romanNumber1 = new RomanNumeral(a);
            RomanNumeral romanNumber2 = new RomanNumeral(b);

            RomanNumeral result = romanNumber1 - romanNumber2;

            Assert.AreEqual(expected, result.romanNumeralStr);
        }
Exemple #11
0
        public void OperatorDivide_ValidRomanNumerals_SetsQuotient(string a, string b, string expected)
        {
            RomanNumeral romanNumber1 = new RomanNumeral(a);
            RomanNumeral romanNumber2 = new RomanNumeral(b);

            RomanNumeral result = romanNumber1 / romanNumber2;

            Assert.AreEqual(expected, result.romanNumeralStr);
        }
Exemple #12
0
        public void OperatorMultiply_ValidRomanNumerals_SetsProduct(string a, string b, string expected)
        {
            RomanNumeral romanNumber1 = new RomanNumeral(a);
            RomanNumeral romanNumber2 = new RomanNumeral(b);

            RomanNumeral result = romanNumber1 * romanNumber2;

            Assert.AreEqual(expected, result.romanNumeralStr);
        }
Exemple #13
0
        public void OperatorModulo_ValidRomanNumerals_SetsRemainder(string a, string b, string expected)
        {
            RomanNumeral romanNumber1 = new RomanNumeral(a);
            RomanNumeral romanNumber2 = new RomanNumeral(b);

            RomanNumeral result = romanNumber1 % romanNumber2;

            Assert.AreEqual(expected, result.romanNumeralStr);
        }
Exemple #14
0
        public void RomanNumeral_Input_1404_Returns_MCDIV()
        {
            // Given
            RomanNumeral romanNumeral = new RomanNumeral();
            string       result       = romanNumeral.TranslateNumberToRomanNumeral(1404);

            // Then
            Assert.AreEqual("MCDIV", result);
        }
Exemple #15
0
        public void return_XIII_when_the_arabic_number_is_13()
        {
            int          arabicNumeral = 13;
            RomanNumeral romanNumeral  = new RomanNumeral();

            string convertedNumber = romanNumeral.ConvertToRomanNumerals(arabicNumeral);

            Check.That(convertedNumber).IsEqualTo("XIII");
        }
Exemple #16
0
        public void RomanNumeral_IV_IsItsOwnGetMinimalForm()
        {
            var number = new RomanNumeral();
            var actual_roman_numeral   = "IV";
            var expected_roman_numeral = actual_roman_numeral;

            number.Value = actual_roman_numeral;
            Assert.AreEqual(expected_roman_numeral, number.GetMinimalForm());
        }
 public void ZeroTest()
 {
     for (int i = 1; i <= 3; i++)
     {
         RomanNumeral romanNumeral = RomanNumeral.Retrieve(i);
         string       zeroResult   = romanNumeral.ConvertArabicNumeral(0);
         Assert.AreEqual(String.Empty, zeroResult);
     }
 }
Exemple #18
0
        public void GetRomanNumeralsLessThanKnown()
        {
            var romanNumeral = new RomanNumeral(4);

            Assert.AreEqual(romanNumeral.RomanValue, "IV");

            romanNumeral = new RomanNumeral(9);
            Assert.AreEqual(romanNumeral.RomanValue, "IX");
        }
Exemple #19
0
        public void RomanNumeral_Input_19_Returns_XIX()
        {
            // Given
            RomanNumeral romanNumeral = new RomanNumeral();
            string       result       = romanNumeral.TranslateNumberToRomanNumeral(19);

            // Then
            Assert.AreEqual("XIX", result);
        }
Exemple #20
0
        public void OperatorPlus_ValidRomanNumerals_SetsSum(string a, string b, string expected)
        {
            RomanNumeral romanNumber1 = new RomanNumeral(a);
            RomanNumeral romanNumber2 = new RomanNumeral(b);

            RomanNumeral result = romanNumber1 + romanNumber2;

            Assert.AreEqual(expected, result.romanNumeralStr);
        }
Exemple #21
0
        public void return_9_when_given_roman_symbol_is_IX()
        {
            string       romanSymbol  = "IX";
            RomanNumeral romanNumeral = new RomanNumeral();

            int convertedNumber = romanNumeral.ConvertToArabicNumber(romanSymbol);

            Check.That(convertedNumber).IsEqualTo(9);
        }
Exemple #22
0
 // Instantiates and runs through 4000 roman numeral conversions
 static void Main(string[] args)
 {
     for (int i = 0; i < 4000; i++)
     {
         var romanNumeral = new RomanNumeral();
         Console.WriteLine(String.Format("The roman numeral conversion for {0} is:  {1}\n", i, romanNumeral.Convert(i)));
     }
     Console.Read();
 }
Exemple #23
0
        public static string Solve(string rnExpression)
        {
            string normalExpression = RomanExpression.ChangeRomanExpressionToNormalExpression(rnExpression);
            string rpn           = RPN.ConvertToRPN(normalExpression);
            int    decimalResult = RPN.SolveRPN(rpn);
            string romanResult   = new RomanNumeral(decimalResult).romanNumeralStr;

            return(romanResult);
        }
Exemple #24
0
        public void return_43_when_the_given_roman_symbol_is_XLIII()
        {
            string       romanSymbol  = "XLIII";
            RomanNumeral romanNumeral = new RomanNumeral();

            int convertedNumber = romanNumeral.ConvertToArabicNumber(romanSymbol);

            Check.That(convertedNumber).IsEqualTo(43);
        }
Exemple #25
0
        public void RomanNumeral_Input_42_Returns_XLII()
        {
            // Given
            RomanNumeral romanNumeral = new RomanNumeral();
            string       result       = romanNumeral.TranslateNumberToRomanNumeral(42);

            // Then
            Assert.AreEqual("XLII", result);
        }
Exemple #26
0
        public void RomanNumeral_Input_986_Returns_CMLXXXVI()
        {
            // Given
            RomanNumeral romanNumeral = new RomanNumeral();
            string       result       = romanNumeral.TranslateNumberToRomanNumeral(986);

            // Then
            Assert.AreEqual("CMLXXXVI", result);
        }
        public void OutOfNumericRangeTest()
        {
            string zeroResult = RomanNumeral.ConvertArabicNumber(0);

            Assert.AreEqual(String.Empty, zeroResult);
            string fourThousandResult = RomanNumeral.ConvertArabicNumber(4000);

            Assert.AreEqual(String.Empty, fourThousandResult);
        }
Exemple #28
0
        public void RomanNumeral_Input_10_returns_X()
        {
            // Given
            RomanNumeral romanNumeral = new RomanNumeral();
            // When
            string result = romanNumeral.TranslateNumberToRomanNumeral(10);

            // Then
            Assert.AreEqual("X", result);
        }
Exemple #29
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MathTests"/> class.
        /// </summary>
        public MathTests()
        {
            var random = new Random(DateTime.Now.Millisecond);

            _firstNumber  = random.Next(1, 39);
            _secondNumber = random.Next(50, 100);

            _firstRomanNumeral  = new RomanNumeral(_firstNumber);
            _secondRomanNumeral = new RomanNumeral(_secondNumber);
        }
Exemple #30
0
        public void RomanNumeral_input_1_2_3_returnsRomanNumeral_I_II_III()
        {
            //Given (class we need to create)
            RomanNumeral romanNumeral = new RomanNumeral();

            //Then
            Assert.AreEqual("I", romanNumeral.TranslateNumberToRomanNumeral(1));
            Assert.AreEqual("II", romanNumeral.TranslateNumberToRomanNumeral(2));
            Assert.AreEqual("III", romanNumeral.TranslateNumberToRomanNumeral(3));
        }
        public void ToRomanNumeral_2019_ReturnsMMXIX()
        {
            var romanNumeral = new RomanNumeral();

            var num = 2019;

            var result = romanNumeral.ToRomanNumeral(num);

            Assert.AreEqual("MMXIX", result);
        }
        public void ToRomanNumeral_86_ReturnsLXXXVI()
        {
            var romanNumeral = new RomanNumeral();

            var num = 86;

            var result = romanNumeral.ToRomanNumeral(num);

            Assert.AreEqual("LXXXVI", result);
        }
        /// <summary>
        /// Returns a string representation of the specified numeral and numeral count
        /// </summary>
        /// <param name="numeralCount"></param>
        /// <param name="romanNumeral"></param>
        /// <returns></returns>
        private static string GetAdditiveFragmentString(int numeralCount, RomanNumeral romanNumeral)
        {
            string result = string.Empty;

            for (int i = 0; i < numeralCount; i++)
            {
                result += romanNumeral.ToString();
            }

            return result;
        }
        /// <summary>
        /// Returns one of:
        ///     0 fragments
        ///     1 additive or subtractive fragment
        ///     Both
        /// </summary>
        /// <param name="romanNumeral"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static List<RomanNumeralFragment> GetFragmentsForRomanNumeral(RomanNumeral romanNumeral, int value)
        {
            var result = new List<RomanNumeralFragment>();
            var count = GetFullNumeralCount(value, romanNumeral);
            var fragmentString = GetAdditiveFragmentString(count, romanNumeral);

            if (fragmentString.Length > 0)
            {
                var fragment = new RomanNumeralFragment(fragmentString);

                result.Add(fragment);

                value = value - fragment.Value;
            }

            var subtractiveFragment = GetSubtractiveFragment(romanNumeral, value);

            if (subtractiveFragment != null)
            {
                result.Add(subtractiveFragment);
            }

            return result;
        }
 /// <summary>
 /// Simply appends first and second parameters as a concatenated string
 /// </summary>
 /// <param name="firstLetter"></param>
 /// <param name="secondLetter"></param>
 /// <returns></returns>
 private static string GetSubtractiveFragmentString(RomanNumeral firstLetter, RomanNumeral secondLetter)
 {
     var result = firstLetter.ToString() + secondLetter.ToString();
     return result;
 }
        ///// <summary>
        ///// Returns the number of numerals required for the specified value.  Does not include subtractive fragments
        ///// </summary>
        ///// <param name="value"></param>
        ///// <param name="numeral"></param>
        ///// <returns></returns>
        private static int GetFullNumeralCount(int value, RomanNumeral numeral)
        {
            // Get number of whole numerals to return from value
            int numeralCount = value / (int)numeral;

            return numeralCount;
        }
        /// <summary>
        /// Returns a subtractive fragment using the specified roman numeral.  Returns null if not needed
        /// </summary>
        /// <param name="currentRomanNumeral"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private static RomanNumeralFragment GetSubtractiveFragment(RomanNumeral currentRomanNumeral, int value)
        {
            RomanNumeralFragment fragment = null;

            // check if value if difference is greater than numeral minus C
            if ((value > (int)RomanNumeral.C) && (value >= ((int)currentRomanNumeral - (int)RomanNumeral.C)))
            {
                // if greater, we need to use a C subtractive fragment
                fragment = new RomanNumeralFragment(GetSubtractiveFragmentString(RomanNumeral.C, currentRomanNumeral));
            }
            // if false, check for X and I using rules above
            else if ((value > (int)RomanNumeral.X) && (value >= ((int)currentRomanNumeral - (int)RomanNumeral.X)))
            {
                // if greater, we need to use a X subtractive fragment
                fragment = new RomanNumeralFragment(GetSubtractiveFragmentString(RomanNumeral.X, currentRomanNumeral));
            }
            else if ((value > (int)RomanNumeral.I) && (value >= ((int)currentRomanNumeral - (int)RomanNumeral.I)))
            {
                // if greater, we need to use a I subtractive fragment
                fragment = new RomanNumeralFragment(GetSubtractiveFragmentString(RomanNumeral.I, currentRomanNumeral));
            }

            return fragment;
        }
 public void should_return_XLIX_for_49()
 {
     var result = new RomanNumeral("XLIX");
     Assert.That(result.ToInt(), Is.EqualTo(49));
 }
 public SingleNumeralConverter(RomanNumeral the_numeral, IConvertSingleNumerals next_converter)
 {
     this.the_numeral = the_numeral;
     this.next_converter = next_converter;
 }
 public void should_return_IV_for_4()
 {
     var result = new RomanNumeral(4);
     Assert.That(result.ToNumeral(), Is.EqualTo("IV"));
 }
        private static double UpdateAndReturnValueByNumeral(StringBuilder builder, double value, RomanNumeral numeral)
        {
            var numeralValue = (double)numeral.Value;

            if (value >= numeralValue)
            {
                var count = (int)Math.Floor(value / numeralValue);
                builder.Append(numeral.Symbol, count);

                value -= (numeralValue * count);
            }

            if (numeral.SubtractiveNumeral == null)
            {
                return value;
            }
            else
            {
                return UpdateAndReturnValueBySubtractiveNumeral(builder, value, numeral);
            }
        }
 public void should_return_XCIX_for_99()
 {
     var result = new RomanNumeral(99);
     Assert.That(result.ToNumeral(), Is.EqualTo("XCIX"));
 }
 public void should_return_9_forIX()
 {
     var result = new RomanNumeral("IX");
     Assert.That(result.ToInt(), Is.EqualTo(9));
 }
 public void should_return_4_for_IV()
 {
     var result = new RomanNumeral("IV");
     Assert.That(result.ToInt(), Is.EqualTo(4));
 }
 public void should_return_2_for_II()
 {
     var result = new RomanNumeral("II");
     Assert.That(result.ToInt(), Is.EqualTo(2));
 }
 public void should_return_bbc_format_for_1999()
 {
     var result = new RomanNumeral(1999);
     Assert.That(result.ToNumeral(), Is.EqualTo("MCMXCIX"));
 }
 public void should_return_1999_for_bbc()
 {
     var result = new RomanNumeral("MCMXCIX");
     Assert.That(result.ToInt(), Is.EqualTo(1999));
 }
 private static void ThrowIfNumeralNotFound(RomanNumeral numeral, string romanNumeralString, int charIndex, string paramName)
 {
     if (numeral == null)
     {
         throw new ArgumentException($"{paramName} contains invalid char {romanNumeralString[charIndex]} at index {charIndex}.");
     }
 }
 public void should_return_II_for_2()
 {
     var result = new RomanNumeral(2);
     Assert.That(result.ToNumeral(), Is.EqualTo("II"));
 }
 public void should_return_VI_for_6()
 {
     var result = new RomanNumeral(6);
     Assert.That(result.ToNumeral(), Is.EqualTo("VI"));
 }
Exemple #51
0
 public RomanNumeral(string numeral, int arabicValue, RomanNumeral canBeModifiedBy)
 {
     Numeral = numeral;
     ArabicValue = arabicValue;
     this.canBeModifiedBy = canBeModifiedBy;
 }
        private static double UpdateAndReturnValueBySubtractiveNumeral(StringBuilder builder, double value, RomanNumeral numeral)
        {
            var subNumeral = numeral.SubtractiveNumeral;
            var subValue = (double)(numeral.Value - subNumeral.Value);

            if (value >= subValue)
            {
                builder.Append(subNumeral.Symbol);
                builder.Append(numeral.Symbol);

                value -= subValue;
            }

            return value;
        }