# Thread: I need help with RPN notation!!!

1. ## I need help with RPN notation!!!

Hi,
I'm currently working on a program that converts infix notation to postfix notation, then calculates the postfix notation. I was successful in the conversion, but I can't get the calculation to work. Below is my code, can someone please help, I've been working on this for hours!

Code:
```
#include<iostream>

using namespace std;

/*-------------------- Stack class ----------------------*/
template<class T>
class Stack
{
private:
int size;
int top;
T *arrayptr;
public:
Stack(int=20);
bool Push(T);
T Pop();
bool isfull();
bool isempty();
};

template<class T>
Stack<T> :: Stack(int s)
{
size = s > 0?s:20;
top = -1;
arrayptr = new T[size];
}

template<class T>
bool Stack<T> :: Push(T val)
{
if(!isfull()){
arrayptr[++top]=val;
return true;}
return false;
}

template<class T>
T Stack<T> :: Pop()
{
return arrayptr[top--];
}

template<class T>
bool Stack<T>::isfull()
{

}

template<class T>
bool Stack<T>::isempty()
{
}

void eVal(char *str,Stack<char> s,int siz);

/*------------------------ main --------------------------*/

int main()
{
Stack<char> s1;
int sizestr;
char string[20];

cout << "Enter an infix expression: ";
cin >> string;
sizestr = strlen(string);

cout << "The RPN Notation is: ";
eVal(string,s1,sizestr);

system("PAUSE");
return EXIT_SUCCESS;
}

/*----------------------- eVal ---------------------------*/

void eVal(char *str,Stack<char> s,int siz)
{
char pop1;
char str2[20];

for(int i = 0;i < siz;i++)
{
if(str[i] > '0' && str[i] <= '9')
{
cout << str[i];
str2[i] = str[i];
}
else if(str[i] == '*')
{
if(s.isempty())
{
s.Push(str[i]);
}
else
{
pop1 = s.Pop();
if(pop1 == '(')
{
s.Push(pop1);
s.Push(str[i]);
}
else if(pop1 == '*'|| pop1 =='/')
{
str2[i] = pop1;
cout << pop1;
s.Push(str[i]);
}
else if(pop1 == '+' || pop1 == '-')
{
s.Push(pop1);
s.Push(str[i]);
}
}
}

else if(str[i] == '/')
{
if(s.isempty())
{
s.Push(str[i]);
}
else
{
pop1 = s.Pop();
if(pop1 == '(')
{
s.Push(pop1);
s.Push(str[i]);
}
else if(pop1 == '*' || pop1 == '/')
{
str2[i] = pop1;
cout << pop1;
s.Push(str[i]);
}
else if(pop1 == '+' || pop1 == '-')
{
s.Push(pop1);
s.Push(str[i]);
}
}
}

else if(str[i] == '+')
{
if(s.isempty())
{
s.Push(str[i]);
}
else
{
pop1 = s.Pop();
if(pop1 == '(')
{
s.Push(pop1);
s.Push(str[i]);
}
else if(pop1 == '*' || pop1 == '/' || pop1 == '+' || pop1 == '-')
{
str2[i] = pop1;
cout << pop1;
s.Push(str[i]);
}
}
}

else if(str[i] == '-')
{
if(s.isempty())
{
s.Push(str[i]);
}
else
{
pop1 = s.Pop();
if(pop1 == '(' )
{
s.Push(pop1);
s.Push(str[i]);
}
else if(pop1 == '*' || pop1 == '/' || pop1 == '+' || pop1=='-')
{
str2[i] = pop1;
cout << pop1;
s.Push(str[i]);
}
}
}

else if(str[i] == '(')
s.Push(str[i]);
else if(str[i] == ')')
{
pop1 = s.Pop();
while(pop1 != '(')
{
str2[i] = pop1;
cout << pop1;
pop1 = s.Pop();
}
}
}

while(!s.isempty())
{
cout << s.Pop();
}
cout << endl;
int left, right;
char type;
int convert;

for(i = 0;i < siz;i++)
{
type = str2[i];
if (isdigit(type))
{
convert = str2[i] - '0';
i = convert;
s.Push(str2[i]);
}
else
{
switch(type)
{
case '+':
s.Push(s.Pop() + s.Pop());
break;
case '-':
right = s.Pop();
left = s.Pop();
s.Push(left - right);
break;
case '*':
s.Push(s.Pop() * s.Pop());
break;
case '/':
right = s.Pop();
left = s.Pop();
s.Push(left / right);
break;
default:
throw domain_error("Undefined operator");
}
}
}
cout << "Result: " << str2[0] << str2[1] << str2[2] << str2[3] << str2[4] <<  endl;
}```

2. It's not working because you wrote eVal as one big, obfuscated function, instead of breaking it up into pieces. If you broke it up into smaller, simpler functions and debugged each part, you'd be able to solve your problem.

3. I understand that I need to break it up into smaller pieces, but I need some advice on how to transfer the RPN notation char's into a seperate array...