예제 #1
0
        private RomanNumeral ProcessSpecialCases(int number, RomanNumeral max)
        {
            if (number == max.Number)
            {
                return null;
            }

            var greater = RomanNumeralRepository.Numerals.OrderBy(x => x.Number)
                .Where(x => max.Number < x.Number);
            var less = RomanNumeralRepository.Numerals.OrderByDescending(x => x.Number)
                .Where(x => number > x.Number);

            var closestGreater = greater.FirstOrDefault();
            var nextLess = less.Skip(1).FirstOrDefault();

            if (closestGreater == null)
            {
                return null;
            }

            var roundedValueToPrecision = ((int)(number / max.Precision) * max.Precision);

            if (nextLess != null && roundedValueToPrecision == closestGreater.Number - nextLess.Number)
            {
                return new RomanNumeral { Number = closestGreater.Number - nextLess.Number, Value = string.Join("", nextLess.Value, closestGreater.Value) };
            }

            if (roundedValueToPrecision == closestGreater.Number - max.Number)
            {
                return new RomanNumeral { Number = closestGreater.Number - max.Number, Value = string.Join("", max.Value, closestGreater.Value) };
            }

            return null;
        }
        public void Should_parse_94_to_XCIV()
        {
            var output = new RomanNumeral ().Parse (94);

            Assert.That (output, Is.EqualTo ("XCIV"));
        }
        public void Should_parse_6_to_VI()
        {
            var output = new RomanNumeral().Parse (6);

            Assert.That (output, Is.EqualTo ("VI"));
        }
        public void Should_parse_50_to_L()
        {
            var output = new RomanNumeral ().Parse (50);

            Assert.That (output, Is.EqualTo ("L"));
        }
        public void Should_parse_20_to_XX()
        {
            var output = new RomanNumeral().Parse (20);

            Assert.That (output, Is.EqualTo ("XX"));
        }
        public void Should_parse_1990_to_MCMXC()
        {
            var output = new RomanNumeral ().Parse (1990);

            Assert.That (output, Is.EqualTo ("MCMXC"));
        }
        public void Should_parse_100_to_C()
        {
            var output = new RomanNumeral ().Parse (100);

            Assert.That (output, Is.EqualTo ("C"));
        }
        public void Should_parse_two_to_II()
        {
            var output = new RomanNumeral().Parse (2);

            Assert.That (output, Is.EqualTo ("II"));
        }