public void ReverseBracket() { if (type.Equals(NumericExpressionOperatorType.BracketOn)) { type = NumericExpressionOperatorType.BracketOff; } else if (type.Equals(NumericExpressionOperatorType.BracketOff)) { type = NumericExpressionOperatorType.BracketOn; } }
public override decimal ToNumber() { //Reverse Polish Notation reader Stack <decimal> stack = new Stack <decimal>(); foreach (INumericExpressionElement el in elements) { if (el is INumerable) { stack.Push((el as INumerable).ToNumber()); } else if (el is NumericExpressionOperator) { if ((el as NumericExpressionOperator).GetOperatorType().Equals(NumericExpressionOperatorType.SignChange)) { /// to check if working decimal a = stack.Pop(); stack.Push(-a); } else { NumericExpressionOperatorType type = (el as NumericExpressionOperator).GetOperatorType(); decimal a = stack.Pop(); decimal b = stack.Pop(); switch (type) { case NumericExpressionOperatorType.Plus: stack.Push(a + b); break; case NumericExpressionOperatorType.Minus: stack.Push(a - b); break; case NumericExpressionOperatorType.Multiply: stack.Push(a * b); break; case NumericExpressionOperatorType.Divide: if (a == 0) { throw new RuntimeException("RUNTIME ERROR! Division by zero occured."); } stack.Push(a / b); break; case NumericExpressionOperatorType.Modulo: if (a == 0) { throw new RuntimeException("RUNTIME ERROR! Modulo by zero occured."); } stack.Push(a % b); break; } } } } return(stack.Pop()); }
public void UnaryMinus() { type = NumericExpressionOperatorType.SignChange; }
public NumericExpressionOperator(NumericExpressionOperatorType type) { this.type = type; }