Jul 3, 2012 at 3:33am UTC
so getting the sum of a postfix equation , im using push and pop until the whole string is gone thru, when it should be done i should have one value left, i want to pop it and return it but im having alot of problems with that
#include<stack>
stack <int> calc;
bool ope(char op)
{
if (op=='+' || op=='-' || op=='/' || op=='*' || op=='^'){
return true;
}
else{
return false;
}
}
int calculate(string p)
{
char first;
int sum;
int f;
int a;
int b;
for(int i=0; i< p.length();i++){
first= p[i];
f = (int)first;
a=p[i-1];
b=p[i-2];
if(ope(p[i])==true){
if(p[i]=='^'){
calc.push(pow(b,a));
calc.pop();
calc.pop();
}
else if(p[i]=='*'){
calc.push(b*a);
calc.pop();
calc.pop();
}
else if(p[i]=='/'){
calc.push(b/a);
calc.pop();
calc.pop();
}
else if(p[i]=='+'){
calc.push(b+a);
calc.pop();
calc.pop();
}
else if(p[i]=='-'){
calc.push(b-a);
calc.pop();
calc.pop();
}
else{
calc.push(p[i]);
}
}
}
int z=calc.pop(); <-------- problem
return z;
-------------------------
return calc.pop(); <------ is another way i tryed but didnt work
}