NumberElement calculate(NumberElement left, NumberElement right, OperatorElement op) { Double temp = Double.MaxValue; if (op.type == OperatorType.ADD) { temp = left.getNumber() + right.getNumber(); } else if (op.type == OperatorType.SUBTRACT) { temp = left.getNumber() - right.getNumber(); } else if (op.type == OperatorType.MULTIPLY) { temp = left.getNumber() * right.getNumber(); } else if (op.type == OperatorType.DIVIDE) { temp = left.getNumber() / right.getNumber(); } else if (op.type == OperatorType.EXPONENTIAL) { temp = Math.Pow(left.getNumber(), right.getNumber()); } return(new NumberElement(temp.ToString())); }
public Double Evaluate(List <Element> e) { List <Element> v = new List <Element>(e); for (int i = 0; i < v.Count; i++) { Element element = v[i]; if (element.GetType().Equals(typeof(NumberElement))) { stack.Push(element); } if (element.GetType().Equals(typeof(OperatorElement))) { NumberElement right = (NumberElement)stack.Pop(); NumberElement left = (NumberElement)stack.Pop(); NumberElement result = calculate(left, right, (OperatorElement)element); stack.Push(result); } } return(((NumberElement)stack.Pop()).getNumber()); }