answersLogoWhite

0

Let Q be an arithmetic expression written in infix notation. Besides operands and operators, Q may also contain left and right parentheses. We assume that the operators in Q consist only of exponentiations (↑), multiplications (*), divisions (/), additions (+) and subtractions (-), and that they have the usual three levels of precedence as given above. We also assume that operators on the same level, including exponentiations, are performed from left to right unless otherwise indicated by parentheses. (This is not standard, since expressions may contain unary operators and some languages perform the exponentiations from right to left. However, these assumptions simplify our algorithm.)

The following algorithm transforms the infix expression Q into its equivalent postfix expression P. The algorithm uses a stack to temporarily hold operators and left parentheses. The postfix expression P will be constructed from left to right using the operands from Q and the operators, which are removed from STACK. We begin by pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q. The algorithm is completed when STACK is empty.

69

Algorithm: POLISH(Q, P)

Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P.

1. Push "(" onto STACK, and add ")" to the end of Q.

2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q until the STACK is empty:

3. If an operand is encountered, add it to P.

4. If a left parenthesis is encountered, push it onto STACK.

5. If an operator ⊗ is encountered, then:

(a) Repeatedly pop from STACK and add to P each operator (on

the top of STACK) which has the same precedence as or higher precedence than ⊗.

(b) Add ⊗ to STACK.

[End of If structure.]

6. If a right parenthesis is encountered, then:

(a) Repeatedly pop from STACK and add to P each operator (on

the top of STACK) until a left parenthesis is encountered.

(b) Remove the left parenthesis. [Do not add the left parenthesis to P.]

[End of If structure.]

[End of Step 2 loop.]

7. Exit.

The terminology sometimes used for Step 5 is that ⊗ will "sink" to its own level. .

70

EXAMPLE

Consider the following arithmetic infix expression Q:

Q: A+(B*C- (D/E↑F)*G)*H

We simulate the previous algorithm to transform Q into its equivalent postfix expression P.

First we push "(" onto STACK, and then we add ")" to the end of Q to obtain:

A + ( B * C - ( D / E ↑ F ) * G ) * H )

(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)(14) (15) (16)(17) (18)(19) (20)

The elements of Q have now been labeled from left to right for easy reference. Table below shows the status of STACK and of the string P as each element of Q is scanned. Observe that

(1) Each operand is simply added to P and does not change STACK.

(2) The subtraction operator (-) in row 7 sends * from STACK to P before it (-) is pushed onto STACK.

(3) The right parenthesis in row 14 sends j and then I from STACK to P, and then removes the left parenthesis from the top of STACK.

(4) The right parenthesis in row 20 sends * and then + from STACK to P, and then removes-the left parenthesis from the top of STACK.

After Step 20 is executed, the STACK is empty and

P: A B C * D E F ↑ / G * - H * +

71

which is the required postfix equivalent of Q.

User Avatar

Wiki User

14y ago

Still curious? Ask our experts.

Chat with our AI personalities

RossRoss
Every question is just a happy little opportunity.
Chat with Ross
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
More answers

The only postfix/prefix operators in C++ are the increment and decrement operators. Converting from one to the other is simply a matter of moving the operator before (prefix) or after (postfix) the operand.

int x = 1;

++x; // prefix (increments x and returns the new value of x)

x++; // postfix (increments x but returns the original value of x)

--x; // prefix (decrements x and returns the new value of x)

x--; // postfix (decrements x but returns the original value of x)

The prefix operators are functionally equivalent to:

// prefix increment

int result = ( x = x + 1 );

// prefix decrement

int result = ( x = x - 1 );

The postfix operators are functionally equivalent to:

// postfix increment

int result = x;

x = x + 1;

// postfix decrement

int result = x;

x = x - 1;

User Avatar

Wiki User

12y ago
User Avatar

Add your answer:

Earn +20 pts
Q: How do you convert postfix expression to prefix expression using c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Example program of how to convert infix notation to postfix notation and prefix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


Evaluate the following postfix expression using stack in C 1 2 3 1 uparrow uparrow 4 5 6star 7star?

Evaluation of a postfix expression is done in the following manner:Scan the expression from the front.1) If it is a number then push it into the stack.2) If it is an operator, then pop two numbers from the stack and then evaluate them using the operator and push it back into the stack.Now consider the given expression. I have presented the sequence in which the expression will be evaluated by using braces.Note - I am using '!' symbol for uparrow1231!!-456*7*-->12(3!1)!-456*7*-->1(2!(3!1))-456*7*-->(1-(2!(3!1)))56*7*-->(1-(2!(3!1)))(5*6)7*-->((1-(2!(3!1)))-((5*6)*7))This will be your final result.


Algorithm to convert postfix notation into infix notation?

/**************************//**********cReDo**********//*****mchinmay@live.com***///C PROGRAM TO CONVERT GIVEN VALID INFIX EXPRESSION INTO POSTFIX EXPRESSION USING STACKS.#include#include#include#define MAX 20char stack[MAX];int top=-1;char pop();void push(char item);int prcd(char symbol){switch(symbol){case '+':case '-':return 2;break;case '*':case '/':return 4;break;case '^':case '$':return 6;break;case '(':case ')':case '#':return 1;break;}}int isoperator(char symbol){switch(symbol){case '+':case '-':case '*':case '/':case '^':case '$':case '(':case ')':return 1;break;default:return 0;}}void convertip(char infix[],char postfix[]){int i,symbol,j=0;stack[++top]='#';for(i=0;iprcd(stack[top]))push(symbol);else{while(prcd(symbol)


Infix to postfix C?

Infix Expression :Any expression in the standard form like "2*3-4/5" is an Infix(Inorder) expression.Postfix Expression :The Postfix(Postorder) form of the above expression is "23*45/-".Infix to Postfix Conversion :In normal algebra we use the infix notation like a+b*c. The corresponding postfix notation is abc*+. The algorithm for the conversion is as follows :Scan the Infix string from left to right.Initialise an empty stack.If the scannned character is an operand, add it to the Postfix string. If the scanned character is an operator and if the stack is empty Push the character to stack. If the scanned character is an Operand and the stack is not empty, compare the precedence of the character with the element on top of the stack (topStack). If topStack has higher precedence over the scanned character Pop the stack else Push the scanned character to stack. Repeat this step as long as stack is not empty and topStack has precedence over the character.Repeat this step till all the characters are scanned.(After all characters are scanned, we have to add any character that the stack may have to the Postfix string.) If stack is not empty add topStack to Postfix string and Pop the stack. Repeat this step as long as stack is not empty.Return the Postfix string.Example :Let us see how the above algorithm will be imlemented using an example.Infix String : a+b*c-dInitially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is 'a'. 'a' is added to the Postfix string. The next character scanned is '+'. It being an operator, it is pushed to the stack.StackPostfix StringNext character scanned is 'b' which will be placed in the Postfix string. Next character is '*' which is an operator. Now, the top element of the stack is '+' which has lower precedence than '*', so '*' will be pushed to the stack.StackPostfix StringThe next character is 'c' which is placed in the Postfix string. Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'. Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty. Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.StackPostfix StringNext character is 'd' which is added to Postfix string. Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string. At this stage we have only a '-' in the stack. It is popped out and added to the Postfix string. So, after all characters are scanned, this is how the stack and Postfix string will be :StackPostfix StringEnd result :Infix String : a+b*c-dPostfix String : abc*+d-


How do you convert a prefix expression to postfix using recursion?

struct stack { char ele; struct stack *next; }; void push(int); int pop(); int precedence(char); struct stack *top = NULL; int main() { char infix[20], postfix[20]; int i=0,j=0; printf("ENTER INFIX EXPRESSION: "); gets(infix); while(infix[i]!='\0') { if(isalnum(infix[i])) postfix[j++]=infix[i]; else { if(top==NULL) push(infix[i]); else { while(top!=NULL && (precedence(top->ele)>=precedence(infix[i]))) postfix[j++]=pop(); push(infix[i]); } } ++i; } while(top!=NULL) postfix[j++]=pop(); postfix[j]='\0'; puts(postfix); getchar(); return 0; } int precedence(char x) { switch(x) { case '^': return 4; case '*': case '/': return 3; case '+': case '-': return 2; default: return 0; } } void push(int x) { int item; struct stack *tmp; if(top==NULL) { top=(struct stack *)malloc(sizeof(struct stack)); top->ele=x; top->next=NULL; } else { tmp=top; top->ele=x; top->next=tmp; } } int pop() { struct stack *tmp; int item; if(top==NULL) puts("EMPTY STACK"); else if(top->next==NULL) { tmp=top; item=top->ele; top=NULL; free(tmp); } else { tmp=top; item=top->ele; top=top->next; free(tmp); } return item; }