Solved

How to convert infix to postfix in c++

Posted on 2004-04-14
1
1,638 Views
Last Modified: 2012-08-13
i need to Write a C++ program that implements infix to postfix conversionand be able to read in a sting consisting of infix expressions and output the postfix expression. i am a begginning programmer and i dont understand this conversion, could you please give me so c++ code or psuedocode that could point me in the right direction?
0
Comment
Question by:JuanPantoja81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
drnick earned 500 total points
ID: 10830964
ok, lets clearify:

1. Infix:  (a + b) - 5/8

1. Postfix: 5 8 / a b + -

to read postfic will not be that complicated.

you go sequentialy through the string you got
two possibilities emerge: i) you hit an operator
                                    ii) you hit a number
no gabs are possible (because they're not needed) in postfix

so what you do?

i expect you to know what a stack is and how int works and how to implement your own one

you need a stack for the numbers

whenever you hit a number, you push it on the stack

whenever you hit an operator, you take the last two numbers from the stack,
apply the operator and push the result on your stack
at the end, you just have one number left on the stack - the result

ok, that was the way you would calculate the whole thing
however, we want to create an infix expression.

next thing is, we make a class like

class my_expr
  {
  int num;
  bool is num;
  my_expr* a, b;
  my_operator: char;

void print_me()
        {
       if(is_num) printf("%i", num);
else {printf("(");   a->print();  printf("%c", my_operator); b->print(); printf(")"); }
  }

now, we let the stack items be pointers to my_expr'sions.
how that?

we use is_num to say if it was a number or an expression
now we go through the input string.
we hit a number: we create new my_expr, set is_num to true, set num to the number and push the thing on the stack

we hit an operator: we take the last two my_expr* from the stack and create a new one
there we set is_num to false and a and b we let point to the two my_expr and we set m_operator to the operator.
now we have it done.

with print, we can create an infix expression recursively.
don't forget the cleanup doing and such

hope that helped, drnick
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question