# Thread: Help with my RPN calculator using c++ #include <stack>

1. Originally Posted by laserlight
Okay. Notice that you did not talk about an array. You need to be clear on what you are trying to do. Why reach for an array before you are sure you need one?

I have never heard of "character RPN". Your example is unclear since I have no clue what is "XYZ XY-" and I don't see why it would give Z as the solution.

After having read your thread on mastering C++, and now hearing your repeated talk of operator overloading, I am starting to suspect that you are infected with C++ featuritis. The symptom is a compulsion to use every feature of C++ that the sufferer knows, just because the sufferer can
This is from my school question. Other than doing numbers, i also need to do the character. They specify to use operator overloading for the charcter RPN. Only need + and - for operator in character example:
Normal
XYZ+ A = XYZA
XYZ - X =YZ
RPN mode
XYZ A = XYZA
XYZ X - = YZ

2. What does + and - mean? You need to be sure of that.

But before you jump into that, just concentrate on dealing with the numbers.

3. just normal add and minus

4. Originally Posted by evildotaing
I am talking about the character thing. There is no "normal add and minus" here.

5. Example:
AB C + = ABC(if C is added to AB, it would add into ABC
BEF F - = BE(if F is minus from BEF, irt become BE
ABD D += ABD(if D is added while there are already a 'D' letter, would remain the same)

Like adding characters, or removing. I would do the numbers RPN calculators first before the charcters.

6. Originally Posted by evildotaing
Rubbish. I don't want to be harsh, but you cannot just have a vague notion of what you are trying to do here. You need to be clear.

Examples are great to illustrate, but unless your examples are exhaustive, they are inadequate compared to a formal description of what you are trying to do.

Let me consider the examples that you have given, save the last one, loosely expressed in RPN:

XYZ XY - = Z
XYZ A + = XYZA
XYZ X - = YZ
AB C + = ABC
BEF F - = BE

Now, if I were shown ABD D + I would state:
ABD D + = ABDD
which turns out is wrong.

Your last example shows that the characters form a set, but maybe order is important. We are not told. Originally, I thought that + is the string concatenation operator, but maybe it is actually the set union operator, or maybe...

Again, you need to be clear on what you are doing.

My suggestion is this: forget about the characters for the time being. Concentrate on implementing a basic algorithm for computing a numeric expression expressed in RPN.

7. Code:
```#include <iostream>
#include <stack>

using namespace std;

void main(void)
{
bool expression;
char ch;
stack<int> a;
int op1, op2;

cout << "Reverse Polish Notation : " << endl;
cout << "Enter expression: " << endl;
while (true)
{
cin >> op1;
cin >> op2;
cin >> ch;
a.push(op1);
a.push(op2);

switch (ch)
{
case '+':
stack.pop(op1 + op2);
break;
case '-':
stack.pop(op1 - op2);
break;
case '*':
stack.pop(op1 * op2);
break;
case '/':
stack.pop(op1 / op2);
break;
}

cout << a.top() << " ";
}
system ("pause");
}```
1>------ Build started: Project: c++ assignment 2, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(70) : error C2955: 'std::stack' : use of class template requires template argument list
1> c:\program files\microsoft visual studio 9.0\vc\include\stack(18) : see declaration of 'std::stack'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(70) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(70) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(73) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(73) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(76) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(76) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(79) : error C2143: syntax error : missing ';' before '.'
1>c:\documents and settings\owner\my documents\visual studio 2008\projects\c++ assignment 2\c++ assignment 2\main.cpp(79) : error C2143: syntax error : missing ';' before '.'
1>Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2008\Projects\c++ assignment 2\c++ assignment 2\Debug\BuildLog.htm"
1>c++ assignment 2 - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Doing numbers RPN first. But encounter error. Why the program keep asking me to input the missing ; when I had already added. The switch structure should be correct ma?

8. It would be easier to determine what is going on if your error messages matched the code you provided. Most of the errors are for lines which do not exist in the posted code.

Jim

9. Code:
```#include <iostream>
#include <stack>

using namespace std;

void main(void)
{
bool expression;
char ch;
stack<int> a;
int op1, op2;

cout << "Reverse Polish Notation : " << endl;
cout << "Enter expression: " << endl;
while (true)
{
cin >> op1;
cin >> op2;
cin >> ch;
a.push(op1);
a.push(op2);

switch (ch)
{
case '+':
a.push(op1 + op2);
break;
case '-':
a.push(op1 - op2);
break;
case '*':
a.push(op1 * op2);
break;
case '/':
a.push(op1 / op2);
break;

}
//while (!a.empty())
//{
cout << "Your expression is " <<  a.top() << endl;
//a.pop();
//}

}
system ("pause");
}```
Nvm, i solve it, but only 2 variables and a operator allowed . No error checking. What should I do if I want to have more than 2 variables for the calculation, should I use array? And what should I do for the subaequent numbers. Anyway the program works only if the user press 2 numbers with a operator. I want to increase that.

10. Code:
```#include <iostream>
#include <stack>

using namespace std;

void main(void)
{
bool expression;
char ch;
char done;
stack<int> a;
int op1, op2;

cout << "Reverse Polish Notation : " << endl;
cout << "Enter expression: " << endl;
while (true)
{
cin >> op1;
cin >> op2;
cin >> ch;
a.push(op1);
a.push(op2);

switch (ch)
{
case '+':
a.push(op1 + op2);
break;
case '-':
a.push(op1 - op2);
break;
case '*':
a.push(op1 * op2);
break;
case '/':
a.push(op1 / op2);
break;

}
cout << "Press = when you had done" << endl;
cin >> done;
if (done = '=')
{
cout << "Your expression is " <<  a.top() << endl;
}

}
system ("pause");
}```
I want to make it like if the user press '=', the program would output the answer. But somehow, this code cant work if i press '='. Instead i manage to get the answer if i press 'Enter' in the keyboard.

11. Code:
```#include <iostream>
#include <stack>

void calculation(int, int, char);
using namespace std;
stack<int> a;

void main(void)
{
bool expression;
char ch;
char done;

int op1, op2;

cout << "Reverse Polish Notation : " << endl;
cout << "Enter expression: " << endl;
while (true)
{
cin >> op1;
cin >> op2;
cin >> ch;

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;

}
}```
Using function. I still not sure how to do if I had more than 2 umbers and 1 operator. Helpppppppppppppppppppppppppppp!!!!!!!!!!!!!!!!!!! !!!!!!

12. 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

13. 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;

cout << "Reverse Polish Notation : " << endl;
cout << "Enter expression: " << endl;
while (!expression)
{
cin >> op1;
cin >> op1;
cin >> ch;

calculation(op1, op1, ch);
if (!cin)
expression = false;
else
{
expression = true;
//cin >> op1;
//cin >> op2;
//cin >> ch;
//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;

}
}```
Guess what, if I let the both variables be op1, then if calculate: why that when i add 3 3 + ; no problem encounter it gives 6.
When i add 2 3 +, it gives 6 instead of 5, i dun understand the logic and error.
When i add 1 2+, it gives 4.

14. help!!!!!!!!!!!

15. Why is line 23 a copy/paste of line 22?