Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
hello , i have this assignment of converting from infix equation to a postfix and prefix in c++ , our teacher gave us the algorithm , but it has been a month and no one could make one , so could you please show me the code of it , i looked for codes in the net , but all of them where using libraries , and i dont want to use them , so please , i need your help ...
with regards ..
ahmed yousify ... :)
Posted
Updated 30-Mar-12 11:02am
v2
Comments
nv3 30-Mar-12 17:00pm    
As this is your home work assignment, I think you will be well advised to take yourself a shot at the problem. In this way, you will learn a lot more than letting someone else write some program code for you.

But even taken that aside; what do you mean by "converting from infix to a postfix and prefix"? Are you talking about writing an interpreter or parser? From what you wrote it is not clear to me, what the actual task is?
LifeSmiler 31-Mar-12 4:43am    
thanks for your opinion , i meant , if we i have a normal equation that is written as infix , and i want to change it to postfix and prefix ,do u know what i mean ??
Addy Tas 30-Mar-12 18:52pm    
I'm wondering if you could be managment material.... trying to make other people work for you is in general not a bad idea. In general also means that sometimes it's a bad idea, homework is needed to learn something. Even if you do not reach the correct answer, you will learn from trying.
Good luck with your assignment and one small remark; don't think to hard, it's really not that complicated. Take it all one step at a time!
LifeSmiler 31-Mar-12 4:46am    
thanks for your opinion ,i kind of like your answer , but i really tried but didnt worked out , as i said earlier , all the code that i've seen were using libraries and i dont want that , so if you add a solution to it , i would appreciated it , and if you dont , well thank you any ways ... :)
Addy Tas 31-Mar-12 5:31am    
Hi, i think you have already figured out that i'm not going to give you the(as if there would be just one) answer. I would suggest that you post what you have tried and i'll give you hints on what you might want to review.

Sounds ok?

Cheers, AT

Before asking question please read this[^]
 
Share this answer
 
 
Share this answer
 
First, define the problem such that all open items are specified, e.g. you might assume some constraints
- let's define + and - as operator
- let's define double as operand type
- let's assume that the operators and operands are separated by spaces
Now, identify the objects you need
- stack<double> for push/ pop operations
- string to store the initial expression
- string stream that gets initialized by the expression string and that is read (scanned) by the >> operator into individual string symbols
- a while loop that reads the string stream as long as there are symbols available
- a switch in the while loop body that decides on - to do -, on + to do +, and else to convert to double an push to the stack
- after the while block, write the top of the stack to the output
- the "do -" and the "do +" is double a = stack. pop(); double b = stack.pop(); stack.push(a - b); (respectively a+b)
That's it for postfix. E.g. "2 3 + 4 -" results in "1".

For Prefix:
- string with prefix expression
- target that will hold postfix expression
- string stream that gets initialized with prefix expression string
- string that holds the last operator (initially empty)
- while loop that reads in each symbol by >>
- switch for - and +: append space plus the last operator to the target string and store the new operator as last operator. The other values are appende directly to the target string (with a space before the value).
- after the while block, append the last operator to the target string
- finally, call the Postfix function from above on the target string
That's all, folks!

Have fun!
Cheers
Andi
 
Share this answer
 
v2
Comments
Hivi Dino 12-Apr-12 15:08pm    
Thanks Andi For that declaration :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900