コード例 #1
0
        public static IOperation Create(decimal firstNumber, decimal secondNumber, string operatorValue)
        {
            IOperation operation = null;

            switch (operatorValue)
            {
            case OperatorSymbols.Addition:
                operation = new SumOperation(firstNumber, secondNumber);
                break;

            case OperatorSymbols.Substraction:
                operation = new SubstractOperation(firstNumber, secondNumber);
                break;

            case OperatorSymbols.Multiplication:
                operation = new MultiplyOperation(firstNumber, secondNumber);
                break;

            case OperatorSymbols.Division:
                operation = new DivideOperation(firstNumber, secondNumber);
                break;

            default:
                throw new InvalidOperationException("Invalid operator");
            }
            return(operation);
        }
コード例 #2
0
        public IOperation ParseOperation(string eq)
        {
            // if the equation doesn't end with ')' then it is invalid. There is no need to process it.
            if (eq.EndsWith(")") == false)
            {
                return(null);
            }

            // Idea is simple. Figure out the opeation in the begining and creating the corresponding operation object.
            if (eq.StartsWith(AddOperation + OpeningBracket))
            {
                // break into two parameter
                var parameters = BreakTheParameter(eq, AddOperation.Length + 1, 1);
                var operation  = new AddOperation();
                operation.Left  = parameters.Item1;
                operation.Right = parameters.Item2;
                return(operation);
            }

            if (eq.StartsWith(MultiOperation + OpeningBracket))
            {
                // break into two parameter
                var parameters = BreakTheParameter(eq, MultiOperation.Length + 1, 1);
                var operation  = new MultiplyOperation();
                operation.Left  = parameters.Item1;
                operation.Right = parameters.Item2;
                return(operation);
            }

            if (eq.StartsWith(DivOperation + OpeningBracket))
            {
                // break into two parameter
                var parameters = BreakTheParameter(eq, DivOperation.Length + 1, 1);
                var operation  = new DivideOperation();
                operation.Left  = parameters.Item1;
                operation.Right = parameters.Item2;
                return(operation);
            }

            if (eq.StartsWith(LetOperation + OpeningBracket))
            {
                var variableNameParameter = BreakTheParameter(eq, LetOperation.Length + 1, 1);
                // Since let has three parameter therefore we are breaking the parameters twice.
                var restOfTheParameter = BreakTheParameter(variableNameParameter.Item2, 0, 0);

                var operation = new LetOperation();
                operation.VariableName   = variableNameParameter.Item1;
                operation.ValueOperation = restOfTheParameter.Item1;
                operation.NextOperation  = restOfTheParameter.Item2;
                return(operation);
            }

            throw new Exception("Invalid Input.");
        }
コード例 #3
0
        static void Main(string[] args)
        {
            bool            exitLoop = true;
            bool            isInputCorrect;
            IMathOperations mathOperations;

            do
            {
                Console.WriteLine("Choose operation:");
                Console.WriteLine("1. Add.");
                Console.WriteLine("2. Subtract.");
                Console.WriteLine("3. Multiply.");
                Console.WriteLine("4. Divide.");
                Console.WriteLine("0. Quit.\n");
                isInputCorrect = int.TryParse(Console.ReadLine(), out int caseChoice);
                if (isInputCorrect)
                {
                    mathOperations = new NullOperation();

                    switch (caseChoice)
                    {
                    case 1:
                        mathOperations = new SumOperation();
                        break;

                    case 2:
                        mathOperations = new SubtractionOperation();
                        break;

                    case 3:
                        mathOperations = new MultiplyOperation();
                        break;

                    case 4:
                        mathOperations = new DivideOperation();
                        break;

                    case 0:
                        exitLoop = false;
                        break;

                    default:
                        PrintNoOptionSelected();
                        break;
                    }
                    mathOperations.MathOperation();
                }
                else
                {
                    PrintNoOptionSelected();
                }
            } while (exitLoop);
        }
コード例 #4
0
        private void HandleOperatorPressed(object sender, OperatorPressedEventArgs e)
        {
            entry        = Entry.Integer;
            decimalCount = 1;

            switch (e.Operator)
            {
            case Operator.Add:
                if (parenClosed)
                {
                    this.view.Display = parenStack[0].Result.ToString("0.####");
                    AddOperation parenAdd = new AddOperation();
                    parenAdd.LeftOperand = parenStack[0];
                    current     = parenAdd;
                    mode        = Mode.Replace;
                    parenClosed = false;
                    break;
                }

                AddOperation add = new AddOperation();
                add.LeftOperand = storedOperation;
                current         = add;
                mode            = Mode.Replace;
                break;

            case Operator.Subtract:
                if (parenClosed)
                {
                    this.view.Display = parenStack[0].Result.ToString("0.####");
                    SubtractOperation parenSubtract = new SubtractOperation();
                    parenSubtract.LeftOperand = parenStack[0];
                    current     = parenSubtract;
                    mode        = Mode.Replace;
                    parenClosed = false;
                    break;
                }

                SubtractOperation subtract = new SubtractOperation();
                subtract.LeftOperand = storedOperation;
                current = subtract;
                mode    = Mode.Replace;
                break;

            case Operator.Multiply:
                if (parenClosed)
                {
                    MultiplyOperation parenMultiply = new MultiplyOperation();
                    parenMultiply.LeftOperand = parenStack[0];
                    current     = parenMultiply;
                    mode        = Mode.Replace;
                    parenClosed = false;
                    break;
                }
                MultiplyOperation multiply = new MultiplyOperation();
                multiply.LeftOperand = storedOperation;
                current = multiply;
                mode    = Mode.Replace;
                break;

            case Operator.Divide:
                if (parenClosed)
                {
                    DivideOperation parenDivide = new DivideOperation();
                    parenDivide.LeftOperand = parenStack[0];
                    current     = parenDivide;
                    mode        = Mode.Replace;
                    parenClosed = false;
                    break;
                }
                DivideOperation divide = new DivideOperation();
                divide.LeftOperand = storedOperation;
                current            = divide;
                mode = Mode.Replace;
                break;
            }
        }