public override double? performOperation(ICalculationOperation operation)
 {
     operation.FirstArgument = operation.SecondArgument;
     operation.SecondArgument = null;
     this.CalculationOperation = operation;
     reset = true;
     return null;
 }
Beispiel #2
0
        public void GrowNodeTreeTest4()
        {
            string test = "((386))";

            ICalculationOperation node_386 = new Number(386M);

            ICalculationOperation actualNode = CalculationParser.GrowNodeTree(input: test, culture: new CultureInfo("es-ES"));

            Assert.AreEqual(actualNode, node_386);
        }
Beispiel #3
0
        protected CalculationOperation(ICalculationOperation left, ICalculationOperation right)
        {
            if (left == null || right == null)
            {
                throw new CheckingException(message: "Calculation failed");
            }

            Left  = left;
            Right = right;
        }
Beispiel #4
0
        public void GrowNodeTreeTest3()
        {
            string test = "((2) + 3,0000)";

            ICalculationOperation node_2        = new Number(2M);
            ICalculationOperation node_3        = new Number(3.0000M);
            ICalculationOperation node_2_plus_3 = new Addition(node_2, node_3);

            ICalculationOperation actualNode = CalculationParser.GrowNodeTree(input: test, culture: new CultureInfo("es-ES"));

            Assert.AreEqual(actualNode, node_2_plus_3);
        }
Beispiel #5
0
        public void GrowNodeTreeTest1()
        {
            string test = "2 + 3";

            ICalculationOperation node_2        = new Number(2M);
            ICalculationOperation node_3        = new Number(3M);
            ICalculationOperation node_2_plus_3 = new Addition(node_2, node_3);

            ICalculationOperation actualNode = CalculationParser.GrowNodeTree(input: test, culture: new CultureInfo("zh-HK"));

            Assert.AreEqual(actualNode, node_2_plus_3);
        }
Beispiel #6
0
        public void GrowNodeTreeTest2()
        {
            string test = "6-8/((  4.2))";

            ICalculationOperation node_6                          = new Number(6M);
            ICalculationOperation node_8                          = new Number(8M);
            ICalculationOperation node_4_point_2                  = new Number(4.2M);
            ICalculationOperation node_8_divide_4_point_2         = new Division(node_8, node_4_point_2);
            ICalculationOperation node_6_minus_8_divide_4_point_2 = new Subtraction(node_6, node_8_divide_4_point_2);

            ICalculationOperation actualNode = CalculationParser.GrowNodeTree(input: test, culture: new CultureInfo("ja-JP"));

            Assert.AreEqual(actualNode, node_6_minus_8_divide_4_point_2);
        }
Beispiel #7
0
        private static ICalculationOperation MakeNode(string input, CultureInfo culture)
        {
            ICalculationOperation node = null;

            if (input.Any(character => CalculationConstants.PlusMinMultDivPow.Contains(character)))
            {
                node = SplitToNodes(input: input, culture: culture);
            }
            else
            {
                input = BracketsHelper.TrimBrackets(input);
                node  = new Number(value: decimal.Parse(input, culture));
            }

            return(node);
        }
        public override double?performOperation(ICalculationOperation operation)
        {
            CalculationOperation.SecondArgument = operation.SecondArgument;
            CalculationOperation.Operation();
            double result = CalculationOperation.Result.Value;

            calculatorUser.CalculatorHistory?.AddToHistory(CalculationOperation);
            if (!(operation is EqualsOperation))
            {
                operation.FirstArgument = CalculationOperation.Result;
                CalculationOperation    = operation;
                reset = true;
            }
            else
            {
                calculatorUser.CalculatorState = new InitialCalculatorState(calculatorUser);
            }
            return(result);
        }
Beispiel #9
0
        private static ICalculationOperation SplitToNodes(string input, CultureInfo culture)
        {
            input = BracketsHelper.TrimBrackets(input);

            // Since people read from left to right, to ensure the correct order of operations,
            // node tree should be built from right to left with priority of operations outside brackets:
            // plus or minus, multply or divide, power.

            int lastOperationIndex = FindLastOerationWithPriorityPlusMinus(input: input);
            var rightString        = input.Substring(lastOperationIndex + 1);
            var leftString         = input.Substring(0, lastOperationIndex);
            var operation          = input[lastOperationIndex];

            ICalculationOperation node         = null;
            ICalculationOperation leftOperand  = MakeNode(input: leftString, culture: culture);
            ICalculationOperation rightOperand = MakeNode(input: rightString, culture: culture);

            switch (operation)
            {
            case CalculationConstants.Plus:
                node = new Addition(leftOperand, rightOperand);
                break;

            case CalculationConstants.Minus:
                node = new Subtraction(leftOperand, rightOperand);
                break;

            case CalculationConstants.Divide:
                node = new Division(leftOperand, rightOperand);
                break;

            case CalculationConstants.Multiply:
                node = new Multiplication(leftOperand, rightOperand);
                break;

            case CalculationConstants.Power:
                node = new ToThePower(leftOperand, rightOperand);
                break;
            }

            return(node);
        }
Beispiel #10
0
        private static decimal DisplayDecimalResult(ICalculationOperation node, CultureInfo culture)
        {
            decimal result        = node.Calculate();
            string  decimalResult = result.ToString($"n{PrecisionForDecimalResult}", culture);

            decimalResult = decimalResult.TrimEnd('0').TrimEnd(Separator(culture: culture));

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.Write("decimal: ");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(decimalResult);

            int separatorIndex = result.ToString().IndexOf(Separator(culture: culture));

            if (separatorIndex > 0 && result.ToString().Length - separatorIndex > PrecisionForDecimalResult)
            {
                Notify += () => Console.WriteLine($"Note, decimal result is rounded, precision is '{PrecisionForDecimalResult}'");
            }

            return(result);
        }
Beispiel #11
0
 public abstract double?performOperation(ICalculationOperation operation);
Beispiel #12
0
 internal Subtraction(ICalculationOperation left, ICalculationOperation right) : base(left, right)
 {
 }
Beispiel #13
0
 internal Addition(ICalculationOperation left, ICalculationOperation right) : base(left, right)
 {
 }
Beispiel #14
0
 internal Multiplication(ICalculationOperation left, ICalculationOperation right) : base(left, right)
 {
 }
Beispiel #15
0
 public void Initialize()
 {
     _calculationOperation = new CalculationOperation();
 }
 public PostfixNotationParseEquation(ICalculationOperation calculationOperation)
 {
     _calculationOperation = calculationOperation;
 }
Beispiel #17
0
 internal ToThePower(ICalculationOperation left, ICalculationOperation right) : base(left, right)
 {
 }
Beispiel #18
0
 internal Division(ICalculationOperation left, ICalculationOperation right) : base(left, right)
 {
 }
 public PostfixNotationExpression(ICalculationOperation calculationOperation, IPostfixNotationParseEquation postfixNotationParseEquation)
 {
     _postfixNotationParseEquation = postfixNotationParseEquation;
     _calculationOperation         = calculationOperation;
 }
Beispiel #20
0
 public override double?performOperation(ICalculationOperation operation)
 {
     return(null);
 }