我有一个程序应该将表达式转换为反向波兰符号,然后在执行计算后显示答案。现在它不能正确执行计算。例如,如果我输入 5+2+5,它出于某种原因只注册了 5+2。谁能告诉我我做错了什么?
#include <iostream>
#include <stack>
void calculation(int, int, char);
using namespace std;
stack<int> a;
void main(void)
{
bool expression = false;
char ch;
char done;
int op1, op2;
cout << "Reverse Polish Notation : " << endl;
cout << "Enter expression: " << endl;
while (!expression)
{
cin >> op1;
cin >> op2;
cin >> ch;
calculation(op1, op2, ch);
if (!cin)
expression = false;
else
{
expression = true;
calculation(op1, op2, ch);
}
}
cout << "Your expression is " << a.top() << endl;
}
void calculation(int oper1, int oper2, char chr)
{
switch (chr)
{
case '+':
a.push(oper1 + oper2);
break;
case '-':
a.push(oper1 - oper2);
break;
case '*':
a.push(oper1 * oper2);
break;
case '/':
a.push(oper1 / oper2);
break;
}
}