Пример #1
0
        private int GetResultOfCalculationInArabicFormat(string reversePolishSignature)
        {
            var stack = new Stack <string>();
            var resultOfCurrentOperation = 0;
            var listElementsOfExpression = _arithmeticExpressionParser.InitializeListOfElements(reversePolishSignature);

            foreach (var elementOfExpression in listElementsOfExpression)
            {
                if (!ArithmeticExpressionParser.IsSignOfOperation(elementOfExpression))
                {
                    stack.Push(elementOfExpression);
                }
                else
                {
                    resultOfCurrentOperation = CalculateCurrentOperation(stack, elementOfExpression, resultOfCurrentOperation);
                    stack.Push(resultOfCurrentOperation.ToString());
                }
            }
            return(int.Parse(stack.Pop()));
        }
        public void ConvertRomanNumberToArabic_RomanNumberIsProperlyConvertedToArabicNotation()
        {
            if (!_isInitializedDictionary)
                InitializeDictionary();

            var parser = new ArithmeticExpressionParser();
            foreach (var arabicNumber in _dictionaryOfArabicAndRomanNumbers.Keys)
            {
                var romanNumber = _dictionaryOfArabicAndRomanNumbers[arabicNumber];

                var expectedArabicNumber = arabicNumber;
                var actualArabicNumberAfterConverting = parser.ConverterOfNumbers.ConvertRomanNumberToArabic(romanNumber);
                Assert.AreEqual(expectedArabicNumber, actualArabicNumberAfterConverting);
            }
        }
        public void ConvertInputStringToReversePolishSignature_InputExpressionIsProperlyConvertedInReversePolishSignature()
        {
            var parser = new ArithmeticExpressionParser();
            const string inputArithmeticExpression = "(VII*IX)*(IX+(LVII*I))";

            const string expectedReversePolishSignature = "$VII$$IX$*$IX$$LVII$$I$*+*";
            var actualReversePolishSignature = parser.ConvertInputStringToReversePolishSignature(inputArithmeticExpression);
            Assert.AreEqual(expectedReversePolishSignature, actualReversePolishSignature);
        }
        public void ConvertArabicNumberToRoman_ArabicNumberIsProperlyConvertedToRomanNotation()
        {
            if (!_isInitializedDictionary)
                InitializeDictionary();

            var parser = new ArithmeticExpressionParser();
            foreach (var romanNumber in _dictionaryOfArabicAndRomanNumbers.Values)
            {
                var arabicNumber = _dictionaryOfArabicAndRomanNumbers.FirstOrDefault(x => x.Value == romanNumber).Key;
                var expectedRomanNumber = romanNumber;
                var actualRomanNumberAfterConverting = parser.ConverterOfNumbers.ConvertArabicNumberToRoman(arabicNumber);
                Assert.AreEqual(expectedRomanNumber, actualRomanNumberAfterConverting);
            }
        }