Conversion of infix string to postfix and evaluation of postfix string to make a simple calculator application in C++.

(A) Algorithm for converting an infix expression into postfix operation

1. Add “(“ at the beginning and “)” at the end of an infix expression Q.
2. Scan Q from left to right and repeat step 3 to step 6.
3  If an operand is encountered, add it into postfix P.
4. If a left parenthesis is encountered, push it onto the stack S
5. If and operator op is encountered then,
(a) Repeatedly pop from stack S and add it to postfix each operator which has
same precedence as or higher precedence than op.
(b) Add op to Stack S.
6. If a right parenthesis is encountered, then
(a) Repeatedly pop from stack S and add
it to postfix each operator until a left parenthesis is encountered on stacks.
(b) Remove the left parenthesis.

(B) Algorithm for evaluation of postfix string

1. Scan postfix P from left to right and repeat  step 2 and 3 for each element of P until the
NULL character or other symbol is encountered.
2. If an operand is encountered then push it on to the stack.
3. If an operator op is encountered, then
(a) Remove the top elements of stack S where A is the
top element and B is next top element
(b) Evaluate B op A
(c) Place the result back onto the stack S
(d) Return top of the stack which is required
the result of our calculation

Source code for both infix to postfix and postfix evaluation

The code is also available on GitHub.

SHARE Conversion of infix string to postfix and evaluation of postfix string to make a simple calculator application in C++.

You may also like...

7 Responses

  1. Raven609 says:

    How would you implement sin() and cos() functions? and variables like x and y, something like z=2x*sin(4y)

  2. It is a very simple conversion from infix to postfix which is valid for simple algebraic calculation and only for single digit. To perform such type of calculation you need parsing and other difficult algorithm. See the limitation that I have written in the source file

  3. Raven609 says:

    Hi again, I was wondering if you could explain me some things,could you explain, what does the "bool Convert::prcd(char op1, char op2){" function do? I kinda have the idea that it chooses which operator to use and the put it into a stack, but I'm not sure of how exactly it works, and if you could please tell me in witch part do you change the char of an operand to an actual number to evaluate? I know that it might be in the "double Evaluate::eval(char expr[],Convert &convert){" function, but I was expecting to see an "atoi" or an "atof" to convert the chars. I hope you don't mind answer me, I pretty green at C++

  4. Hi Raven609, thank u for re visiting my blog. The function prcd ( char op1, char op2) checks the precedence of operators. It returns true if operator op1 has higher precedence than operator op2 otherwise returns false. The code c – '0' converts the character into number i.e integer (why ?) . When you try to subtract character '0' from character c, the compiler converts both character into corresponding ASCII values and returns the result. Try subtracting '1' – '0' and '1' – 0 then you will understand the concept.

  5. Step 5 from algorithm (A)

  6. Step 6 is for balancing opening and closing brackets. It removes the popped left bracket.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share