### convert infix to postfix

i have problem in convert infix to postfix

some expression evaluate it correctly

and other no

ex:
c*(a+b)

evaluate it correctly

but
A*(B*C+D)+E
doesn't evaluate correctly

and there is another problem in finding result

my code

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191`` ``````// convert.h const char SIZE=100; class stack { public: stack(); bool isempty()const; bool isfull()const; void push(char num); void pop(char &elm); void cntres(); int topnum; void print(); void result(stack &post); private: char data[SIZE]; int top; }; // convert.cpp #include #include"convert.h" #include #include using namespace std; stack::stack() { top=-1; topnum=top; } 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]; cout<<"Please Enter Infex Formula :"; cin.get(infex,SIZE); char OpValue; char ch; int lenght; lenght=strlen(infex); for(int i=0;i
any help ?
What's in convert.h?
class declaration

 ``12345678910111213141516171819`` ``````const char SIZE=100; class stack { public: stack(); bool isempty()const; bool isfull()const; void push(char num); void pop(char &elm); void cntres(); int topnum; void print(); void result(stack &post); private: char data[SIZE]; int top; }; ``````
As far as I can tell, it works. Can you list the input/output that you think is wrong?
PostFix Formula : 222+*
RESULT OF INFIX : -48

postfix evaluate it correctly but
tha result has an error....
The conversion to postfix is correct.

Why don't you just evaluate the postfix? You seem to ignore it once you've generated it. The whole point fo converting to postfix is because the evaluation is easy.
my proplem is here

 `` `` `` if(op.topnum=='*'||op.topnum=='/')``

this make some expression evaluated incorrectly

 The conversion to postfix is correct.

not for all expression ....
i fixed the conversion to postfix

and it work correctly for any expression

but still
the result doesn't evaluate it correctly...
i try to make it array of integer but it seems to be more complex

look for output

 ``123`` ``````Please Enter Infex Formula :(2+7)*6 PostFix Formula : 27+6* RESULT OF INFIX : -48``````

proplem in finding result
in this function

`void stack::result(stack &post)`
?
There was nothing wrong to the conversion to postfix, that bit was fine. It was the evaluation that was broken.

 ``123456789`` ``````for each token: if it's a number, push it on the evaluation stack else pop two numbers off the evaluation stack apply the operation push the result display the number on the top of the stack ``````

infix: 2*(2+2) = 2 * 4 = 8

postfix: 222+*

push 2
push 2
push 2

pop 2 and 2, add them and push the result, (push 4)

pop 4 and 2, multiply them, push the result, (push 8)

display top of the stack, 8
Last edited on
fine

but all element stored as a chars

so problem in convert from char to int
You can check if the char is a digit by calling isdigit() in ctype.h. If it's not a digit, you can assume it's an operation.
i try to debug the code

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````void stack::result(stack &post) { int a,b,c; char num; int res=0; int loop=strlen(post.data); for(int i=0;i

still problem ....
I don't understand why you're passing in a stack into the function. I've described an algorithm that starts with an postfix expression in a string, returns a single value and uses a stack within the process.

You've already done the hard part, the infix to postfix converter. Just take string output from that and pass it to the evaluator.
ok

i do this in main

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` cout<<"RESULT OF INFIX : "; while(!post.isempty()) { post.pop(out); if(out=='+'||out=='-'||out=='*'||out=='/') { char num; int a,b,c; res.pop(num); a=num-'0'; res.pop(num); b=num-'0'; switch(out) { case '+': c=a+b; break; case '-': c=a-b; break; case '*': c=a*b; break; case '/': c=a/b; break; } res.push(c); } else res.push(out); } char c; res.pop(out); c=char(out); cout<

but couldn't find it

pls help me

this is a project
and this the last day ....
We seem to have some kind of disconnect. This is what I had in mind, but it's incomplete:
 ``12345678910111213141516171819202122232425262728293031`` ``````int evaluate(const std::string &postfix) { for (size_t i = 0; i != postfix.size(); ++i) { if (isdigit(postfix[i])) { working_stack.push(postfix[i]); } else { int result = 0; int op2 = working_stack.top() - '0'; working_stack.pop(); int op1 = working_stack.top() - '0'; working_stack.pop(); switch (postfix[i]) { case '+': result = op1 + op2; break; } working_stack.push(char(result + '0')); } } return working_stack.top() - '0'; }``````
how i could use this function
?
i declare an array of char
You write out the postfix expression, with .print, just make it write to a general stream instead of stdout.

For example:
 ``12345`` ``````void stack::print(std::ostream &os) { for (int i=0; i <= top; ++i) os << data[i]; }``````

Then you can print to an in-memory stream and convert that to a string:
 ``123456`` `````` std::ostringstream os; // in-memory stream post.print(os); // write the stack content to the stream std::string postfix = os.str(); // save the stream into a string int value = evaluate(postfix); // evaluate the postfix expression std::cout << "postfix expression: " << postfix << std::endl; std::cout << "value: " << value << std::endl;``````

Topic archived. No new replies allowed.