Code:
#include <iostream>
#include <stack>
using namespace std;
void main(void)
{
stack <int> a;
int number, number2;
int sentinel = -999;
//char end = ' ';
char operation;
bool doloops = true;
cout << "Reverse Polish Notation : " << endl;
cout << "Enter -999 to stop the calculator and calculate the value." << endl;
while (doloops)
{
cout << "Enter number: " << endl;
cin >> number;
a.push(number);
if (number == sentinel)
doloops = false;
else
doloops = true;
}
while (!a.empty())
{
cout << a.top() << " ";
a.pop();
}
cout << endl;
system ("pause");
}
I am now doing a RPN calculator. I just trying doing the first part where I want user to key in at least 2 numbers before the program asked if the user want to enter a operator such as (+, -, *, /) or another number for doing the RPN calculator.Now, i not doing the operator yet but just want the user to key in the number, push it before printing the number using pop in my code. -999 is to end the program to print the number with pop using <stack>.
Now, i got a problem, let say we need to have at least 2 numbers before it is possible to do a RPN calculator. So, I had to include 2 variables. After initialise it and user enter 2 of the numbers, I want to pop 2 of the numbers and print it using pop with <stack>. The problem is there is no problem if I just used only one variable, but instead I am using 2 variables. Because we need to have at least 2 numbers before possible to do RPN calculator. How should I solve this.
I am thinking of doing it with array but you see, after getting 2 numbers , i want the user to either select another number or operator, so it likes it is not possible using array. So i am using only variables and repeat the whole process using while/for loops.
Anyway, regarding the RPN calculator, is it true that if you had n numbers. n represent the amount of operand or simply called the numbers using algebra. I need to have at least (n - 1) operator?
Example, if you had 5 operands/numbers, you need at least 4 operators such as (+ , -, / , *) and not like only 3 or less operators or more than 4 operators.