Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var mathOperationsContainer = new MathOperationsContainer();
            var parser            = new Parser();
            var notationConverter = new NotationConverter();
            var mathProcessor     = new MathProcessor();

            var calculator = new MathCalculator(parser, notationConverter, mathProcessor, mathOperationsContainer);

            Console.WriteLine("Calculator");
            Console.WriteLine("To complete the work, type exit.");
            while (true)
            {
                Console.Write("Enter a math expression: ");
                var expression = Console.ReadLine();

                if (expression == "exit")
                {
                    Console.WriteLine("Work completed.");
                    break;
                }

                var result = calculator.Calculate(expression);
                Console.WriteLine("Result: {0}", result);
            }
        }
Ejemplo n.º 2
0
        public void Calculator_calculates()
        {
            const string inputExpression = "1+2*3";

            var parsedExpression = new List <ExpressionUnit>
            {
                new NumberExpressionUnit("1.00"),
                new OperationExpressionUnit("+"),
                new NumberExpressionUnit("2.00"),
                new OperationExpressionUnit("*"),
                new NumberExpressionUnit("3.00")
            };

            var convertedExpression = new List <ExpressionUnit>
            {
                new NumberExpressionUnit("1.00"),
                new NumberExpressionUnit("2.00"),
                new NumberExpressionUnit("3.00"),
                new OperationExpressionUnit("*"),
                new OperationExpressionUnit("+")
            };

            const double expectedResult = 7;

            _mockParser
            .Setup(p => p.Parse(inputExpression, _mathOperationsContainer))
            .Returns(parsedExpression);

            _mockNotationConverter
            .Setup(nc => nc.ConvertToReversePolishNotation(parsedExpression, _mathOperationsContainer))
            .Returns(convertedExpression);

            _mockMathProcessor
            .Setup(mp => mp.Process(convertedExpression, _mathOperationsContainer))
            .Returns(expectedResult);

            var actualResult = _calculator.Calculate(inputExpression);

            Assert.Equal("7", actualResult);
        }