Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter expression: ");
            var expression = Console.ReadLine();

            IExpressionValidator validator = new ExpressionValidator();
            var validationResult           = validator.Validate(expression);

            if (validationResult != null)
            {
                Console.WriteLine(validationResult);
                Console.ReadKey();
                return;
            }

            IExpressionCorrector corrector = new ExpressionCorrector();

            expression = corrector.Correct(expression);

            ICalculator calculator = new Calculator(new Parser(), new OperationPerformer());
            var         result     = calculator.Compute(expression);

            Console.WriteLine($"Result: {result}");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        public void UnitTestWithDivisionReturns182_5()
        {
            // arrange
            var expression = "876/4.8";

            var corrector = new CalculatorProject.ExpressionCorrector();

            expression = corrector.Correct(expression);

            var calculator = new Calculator(
                new Parser(),
                new OperationPerformer());

            // act
            var actualResult = calculator.Compute(expression);

            // assert
            Assert.AreEqual(182.5, actualResult);
        }
Ejemplo n.º 3
0
        public void UnitTestWithSubtractionReturns65_8()
        {
            // arrange
            var expression = "67.8-2";

            var corrector = new CalculatorProject.ExpressionCorrector();

            expression = corrector.Correct(expression);

            var calculator = new Calculator(
                new Parser(),
                new OperationPerformer());

            // act
            var actualResult = calculator.Compute(expression);

            // assert
            Assert.AreEqual(65.8, actualResult);
        }
Ejemplo n.º 4
0
        public void UnitTestWithAllOperatorsReturns45_0()
        {
            // arrange
            var expression = "42+5.2-2*5.5/5";

            var corrector = new CalculatorProject.ExpressionCorrector();

            expression = corrector.Correct(expression);

            var calculator = new CalculatorProject.Calculator(
                new Parser(),
                new OperationPerformer());

            // act
            var actualResult = calculator.Compute(expression);

            // assert
            Assert.AreEqual(45.0, actualResult);
        }
Ejemplo n.º 5
0
        public void UnitTestWithBracketsReturnsMin1_4()
        {
            // arrange
            var expression = "8.4+(6-7.8)-(9*(7-6)-1)";

            var corrector = new CalculatorProject.ExpressionCorrector();

            expression = corrector.Correct(expression);

            var calculator = new CalculatorProject.Calculator(
                new Parser(),
                new OperationPerformer());

            // act
            var actualResult = calculator.Compute(expression);

            // assert
            Assert.AreEqual(-1.4, actualResult);
        }