Evaluating postfix
Sep 28, 2013 at 6:47pm UTC
Hello all I am stuck trying to write a program that reads in an expression, turns that expression into postfix, and then evaluates it. The program reads in the expression and converts it to postfix, but I cant get it to evaluate correctly. For example I input 2 + 3 postfix equals 23+ and it returns 1 as the answer. Any help would be greatly appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#include <fstream>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
int evaluate (int opn1, char token, int opn2)
{ int result;
switch (token)
{
case '+' : result = opn1 + opn2;
case '-' : result = opn1 - opn2;
case '*' : result = opn1 * opn2;
case '/' : result = opn1 / opn2;
case '^' : result = opn1 ^ opn2;
}
return result;
}
int priority(char a)
{
int temp;
if (a == '^' )
temp = 3;
else if (a == '*' || a == '/' )
temp = 2;
else if (a == '+' || a == '-' )
temp = 1;
else if ( a == '(' )
temp = 0;
return temp;
}
int main()
{
string aline;
string st;;
int result;
int opn2, opn1, temp;
stack<char > ostack;
stack<int > istack;
stringstream output2;
stringstream output;
getline(cin,aline);
st = output.str();
for (int i = 0; i < aline.length(); i++)
{
if (aline[i] == ' ' )
i++;
if (aline[i] == '+' || aline[i] == '-' || aline[i] == '*' || aline[i] == '/' || aline[i] == '^' )
{
while (!ostack.empty() && priority(ostack.top()) >= priority(aline[i]))
{
output << ostack.top();
ostack.pop();
}
ostack.push(aline[i]);
}
else if (aline[i] == '(' )
{
ostack.push(aline[i]);
}
else if (aline[i] == ')' )
{
while (ostack.top() != '(' )
{
output << ostack.top();
ostack.pop();
}
ostack.pop();
}
else
{
output << aline[i];
}
}
while (!ostack.empty())
{
output << ostack.top();
ostack.pop();
}
cout<<aline<<endl;
cout << output.str() << endl;
st = output.str();
for (int i=0; i < st.length(); i++)
{
if (st[i] != '+' && st[i] != '-' & st[i] != '*' & st[i] != '/' & st[i] != '^' )
{
temp = st[i] - 48;
cout<< "temp " << temp <<endl;
istack.push(temp);
}
else
{
char token = st[i];
cout<<"token " << token <<endl;
opn2 = istack.top();
cout<<"opn2 " << opn2 <<endl;
istack.pop();
opn1 = istack.top();
cout<<"opn1 " << opn1 <<endl;
istack.pop();
result = evaluate(opn1, token, opn2);
istack.push(result);
cout <<" result " << result<<endl;
istack.push(st[i]);
}
}
}
Here is what it prints out:
2 + 3
2 + 3
23+
temp 2
temp 3
token +
opn2 3
opn1 2
result 1
Oct 1, 2013 at 12:08pm UTC
I haven't managed to fix my postfix issues either, but I don't think you need to push both the result and st[i] at the end of your evaluation "for" loop.
Oct 12, 2013 at 11:45pm UTC
Hi zeppelyn sorry I didn't see your comment. If you add breaks after each statement in evaluate this code should work for you.
Topic archived. No new replies allowed.