- Push operands onto the operand stack.
- Push operators onto the operator stack.
- Ignore left parentheses.
- On encountering a right parenthesis, pop an operator, pop the requisite number of operands, and push onto the operand stack the result of applying that operator to those operands.
For the sake of simplicity I use a ConsoleApplication project. Add the following code to your Main() method:
// You must separate operands and operators by a white space
// Otherwise it won't work
string[] Expr = Console.ReadLine().Split(' ');
// The two stacks: one for operators and the other for operands
Stack<string> ops = new Stack<string>();
Stack<double> vals = new Stack<double>();
// The Algorithm
foreach (string item in Expr)
{
switch (item)
{
// If item is operator then push
case "(": break;
case "+": ops.Push(item); break;
case "-": ops.Push(item); break;
case "*": ops.Push(item); break;
case "/": ops.Push(item); break;
case "mod": ops.Push(item); break;
case "sqrt": ops.Push(item); break;
case "^": ops.Push(item); break;
case ")":
{
// Pop, evaluate and push result if item is ")"
string op = ops.Pop();
double val = vals.Pop();
switch (op)
{
case "+": val += vals.Pop(); break;
case "-": val = vals.Pop() - val; break;
case "*": val *= vals.Pop(); break;
case "/": val = vals.Pop() / val; break;
case "mod": val = vals.Pop() % val; break;
case "sqrt": val = Math.Sqrt(val); break;
case "^": val = Math.Pow(vals.Pop(), val); break;
}
vals.Push(val);
} break;
// If not operator or "(" then push double value
default: vals.Push(double.Parse(item)); break;
}
}
// Finally, show the result
Console.Write(vals.Pop());
// Wait for user input
Console.ReadKey();
For example, the algorithm performs the following steps to compute the value for ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ):
Don't forget to add spaces between operands and operators! Also you have to surround your expression with parenthesis. That's it! You can now try to add some trig functions or whatever you want. You can also choose to parse the string differently without using space as a delimitator.
