answersLogoWhite

0


Best Answer

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

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

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;

This answer is:
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; }

Related questions

Prefix to postfix conversion using C programming?

To convert an infix expression to a postfix expression in C programming, you can use the Shunting Yard algorithm. This algorithm allows you to scan the infix expression from left to right, and based on the precedence of operators, convert it to a postfix expression. You can use a stack to hold operators and output queue to store the final postfix expression. By following the algorithm, you can convert the infix expression to postfix successfully.


Sample program of postfix to infix?

You can convert from postfix to infix through the use of stacks. Consider the following expression conversion:54+67*+ -> ((5+4)+(6*7))The way this can be achieved is that whenever you encounter an operator, pop the last two expressions and join them using the operator. Remember to include the open braces before the first expression and a close braces after the second expression. Check the given link below for the program:


How do you convert infix to postfix without using data structures?

Without data-structures you cannot even store expressions, let alone convert or evaluate them.


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)


When a pre increment is followed by a post increment?

You cannot follow a prefix increment with a postfix increment. int x = 40; int y = ++x++; // error This has to be an error because the postfix operator has higher precedence than the prefix operator. Thus the above code is equivalent to: int x = 40; int y = ++(x++); // error The expression (x++) is evaluated first. This increments x to 41 but the expression evaluates to 40 (the original value of x). Thus the prefix operator is essentially trying to evaluate the expression ++40 rather than ++x. This cannot work because the value 40 is not a modifiable lvalue. It has to be modifiable because we want to increment the value and return a reference to the modified value. But there's nothing to refer to here. The value is temporary and will fall from scope immediately after we use it. That is, the prefix operator may well be able to increment the temporary value to 41, but that value immediately falls from scope. With nothing to refer to, the prefix expression cannot be evaluated. The only way we can use both operators together is if we reverse the precedence using parenthesis: int x = 40; int y = (++x)++; Now the prefix operator is evaluated first, returning a reference to x which (now) holds the value 41. The postfix operator then increments x to 42 but returns the original value of 41 which is then assigned to y. Thus when all statements have been executed, y holds the value 41 while x holds the value 42.


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.


C plus plus program using a stacks converting a postfix-infix?

Yes


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; }


What does C plus C mean?

In terms of the C++ programming language itself, C++ literally means "the successor to C". In terms of the operator, operator++ is the increment operator. It has two forms: prefix (++c) and postfix (c++). Both do exactly the same thing and are effectively shorthand for the more verbose c = c + 1 or the more concise c += 1. The difference between the the prefix and postfix versions is in their evaluation. ++c will evaluate to c + 1 while c++ evaluates to c (the value of before the increment). However, the evaluations are only of importance when used in compound expressions: int a, b, c = 42; a = c++; // a==42, c==43 b = ++c; // b==44, c==44 Where the evaluation is of no importance, we can use either form. However, for user-defined types, the postfix operator usually incurs an overhead. This can be demonstrated by the following minimal example: struct foo { int i; foo& operator++ () { // prefix increment ++i; // increment this->i return *this; // return the incremented object } foo& operator++ (int) { // postfix increment foo f {*this}; // copy this object ++i; // increment this->i return f; // return the pre-incremented object } // ... }; Note that in both cases, the object itself is incremented, the only difference is in what value is returned to the caller (the newly incremented object or the original non-incremented object). However, because postfix increment requires that we copy the original value, we incur an overhead. The more complex the object, the greater that overhead will be. Built-in types do not suffer the copy overhead because the copy can be factored away by the compiler. Even so, it's a good idea to get into the habit of using prefix increment for built-in types unless you have good reason to use postfix increment. Similarly, it is usually a good idea to omit the postfix operator from user-defined classes unless we have good reason to provide it. Note that the postfix operator has an unused int argument in order to differentiate it from the prefix operator. The way to remember which is which is that the prefix operator has no argument, which is in common with all the other unary member operators. The postfix operator is the anomaly, thus it gets the dummy argument. The prefix and postfix decrement operators (operator--) work in a similar way, except they decrement the operand rather than increment it.


What is the Opposite of judge using a prefix?

The opposite of judge using a prefix is misjudge.