### problem if finding postfix

hello

my code convert some expression correctly and other not..

Ex:(2+7)*6
RESULT:54 TRUE
BUT 1*4+4*0-3
RESULT:1

but my code out 7

here is my code
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258`` ``````const char SIZE=100; class stack { public: stack(); bool isempty()const; bool isfull()const; void push(char num); void pop(char &elm); void cntres(); char topnum()const; void print(); void result(stack &post); private: char data[SIZE]; int top; }; #include #include"convert.h" #include #include #include using namespace std; stack::stack() { top=-1; } bool stack::isempty()const { return top==-1; } bool stack::isfull()const { return top==SIZE-1; } void stack::push(char elm) { if(isfull()) cout<<"Cannot Add New Item , No Free Space ."< #include"convert.h" #include using namespace std; int main() { stack op; stack post; stack res; char infex[SIZE]; bool isEx=false; cout<<"Please Enter Infex Formula :"; cin.get(infex,SIZE); bool isnum=false; char OpValue; char ch; int lenght; lenght=strlen(infex); for(int i=0;i='a'&&infex[i]<='z')||(infex[i]>='A'&&infex[i]<='Z')) isnum=true; if(infex[i]=='+'||infex[i]=='-'||infex[i]=='*'||infex[i]=='/'||infex[i]=='('||infex[i]==')') { if(infex[i]=='*'||infex[i]=='/'||infex[i]=='(') op.push(infex[i]); else if(infex[i]=='+'||infex[i]=='-') { if(op.topnum()=='*'||op.topnum()=='/') { op.pop(ch); post.push(ch); op.push(infex[i]); } else op.push(infex[i]); } else if(infex[i]==')') { op.pop(ch); OpValue=ch; while(OpValue!='(') { post.push(OpValue); op.pop(ch); OpValue=ch; } } } else post.push(infex[i]); } while(!op.isempty()) { op.pop(ch); OpValue=ch; post.push(OpValue); } post.print(); cout<
Last edited on
It looks like you are not handling operator precedence correctly. Normally you do division and multiplication operations before you do addition and subtraction. Also you don't seem to be allowing grouping with parentheses.
where is the wrong ???
I would start by insuring your if/else clauses are correct. I suggest using braces, even when not technically required. For example:
 ``123456789101112131415`` `````` if(infex[i]=='+'||infex[i]=='-'||infex[i]=='*'||infex[i]=='/'||infex[i]=='('||infex[i]==')') { if(infex[i]=='*'||infex[i]=='/'||infex[i]=='(') op.push(infex[i]); else if(infex[i]=='+'||infex[i]=='-') { if(op.topnum()=='*'||op.topnum()=='/') { op.pop(ch); post.push(ch); op.push(infex[i]); } else op.push(infex[i]); }``````

What if statement does that last closing brace belong to. Looking at your indentation it may appear to belong to the if(op.topnum()=='*'||op.topnum()=='/') but it actually belongs to the else if.

Topic archived. No new replies allowed.