public override int Evaluate() { int value1 = _expr1.Evaluate(); int value2 = _expr2.Evaluate(); return(value1 - value2); }
static void Main(string[] args) { Parser parser = new Parser(); string[] commands = new string[] { "+ 5 6", "- 6 5", "+ - 4 5 6", "+ 4 - 5 6", "+ - + - - 2 3 4 + - -5 6 + -7 8 9 10" }; foreach (string command in commands) { ExpressionBase expression = parser.Parse(command); Console.WriteLine("{0} = {1}", expression, expression.Evaluate()); } }
// Also called Prefix notation private static void PolishNotationExample() { var parser = new PolishNotationParser(); string[] commands = { "+ 5 6", "- 6 5", "+ - 4 5 6", "+ 4 - 5 6", "+ - + - - 2 3 4 + - -5 6 + -7 8 9 10" }; foreach (string command in commands) { ExpressionBase expression = parser.Parse(command); Console.WriteLine(command); Console.WriteLine(expression); Console.WriteLine(expression.Evaluate()); Console.WriteLine(); } }