Code:
#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;
system ("pause");
}
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;
}
}
latest code: I still cant figure out what should I do> I want to make it like the function would do the calculations for every numbers that was pushed. Still, i manage to solve it with only operands. How to do it when I want to have more than 3 operands and the function to check all the calculation. No error checking required.
If i try to add more than 2 numbers(eg, 3 numbers with 2 operands), the function would only calculate the first 2 operands and the operator(No error in program). Not sure how to do on the subsequent operands and operators