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."); }
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; } }