answersLogoWhite

0


Best Answer

#include <iostream>

#include <stack>

using namespace std;

int prec (char ch){

// Gives precedence to different operators

switch (ch) {

case '^':

return 5;

case '/':

return 4;

case '*':

return 4;

case '+':

return 2;

case '-':

return 1;

default :

return 0;

}

}

bool isOperand(char ch){

// Finds out is a character is an operand or not

if ((ch>='0' && ch<='9') (ch>='a' && ch<='z'))

return true;

else

return false;

}

string postFix (string infix){

string pfix = "";

stack<char> opstack;

for (int i=0; i<infix.length(); i++){

// Scan character by character

if (isOperand(infix[i]))

{

pfix += infix[i];

}

else if (infix[i] ')')

{

// Retrace to last ( closure

while (opstack.top() != '(')

{

pfix += opstack.top();

opstack.pop();

}

// Remove the '(' found by while loop

opstack.pop();

}

User Avatar

Wiki User

βˆ™ 14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 15y ago

If this is a homework assignment, please consider trying to answer it yourself first, otherwise the value of the reinforcement of the lesson offered by the assignment will be lost on you.

To convert infix notation to postfix notation, parse the expression into tokens. As you encounter an operand, push it onto one stack. As you encounter an operator, consider its precedence level and, if it is higher than the current precedence level, push it and the precedence level onto a second stack. Note that the parenthesis is a special operator that overrides precedence level, raising it.

When you encounter an operator with a precedence level equal to or lower than the current precedence level, pop the operand(s) off of the first stack, output them, and the pop the operator off of the second stack (unless it was not pushed in the first place) and output it. As you pop things off of the stacks, consider the current precedence level against the current precedence level, and iterate the pop/output sequence as needed.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

Possible. Here's an example:

(1+3)*(3+4)

1 3 + 3 4 + *

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

convert postfix notation to infix notation in c?

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

Both are expressions..

a+b is infix ...where operater comes between operands

ab+ is postfix..where operator comes at the last

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

Postfix is an open source software. It is used to setup mail servers.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

(a*b+g/k)*l-(2*j)

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

nneee madhu lo solla

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you convert an infix notation to postfix notation?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why do compilers convert infix expressions to postfix?

people almost exclusively use infix notation to write mathematical expressions, computer languages almost exclusively allow programmers to use infix notation. However, if a compiler allowed infix expressions into the binary code used in the compiled version of a program, the resulting code would be larger than needed and very inefficient. Because of this, compilers convert infix expressions into postfix notation expressions, which have a much simpler set of rules for expression evaluation. Postfix notation gets its name from the fact that operators in a postfix expression follow the operands that they specify an operation on. Here are some examples of equivalent infix and postfix expressions Infix Notation Postfix Notation 2 + 3 2 3 + 2 + 3 * 6 3 6 * 2 + (2 + 3) * 6 2 3 + 6 * A / (B * C) + D * E - A - C A B C * / D E * + A C * - Where as infix notation expressions need a long list or rules for evaluation, postfix expressions need very few.


Who invented postfix and infix?

infix: old Egyptians/Assirs some thousands year before prefix: Jan &#321;ukasiewicz (Polish Notation) postfix: Burks, Warren, and Wright (Reverse Polish Notation)


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)


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)


How do infix notation and postfix notation differ?

It's simply a matter of where the operators are placed in relation to their operands: infix: X + Y prefix: + X Y postfix: X Y + All of the above are equivalent. Prefix notation is also known as Polish notation, hence postfix is also known as reverse Polish notation. Given the infix equation A * B + C / D, the order of evaluation is always parenthesis, orders, divide/multiply, add/subtract (PODMAS), thus we must multiply A by B first, then divide C by D, and finally add the two results together. If we wish to perform the addition first, then we must re-write the equation with parenthesis: A * (B + C) / D. With postfix and prefix notation, operator precedence becomes superfluous because we always evaluate these expressions in left-to-right order: Infix A * B + C / D becomes postfix A B * C D / + or prefix / * A + B C D Infix A * (B + C) / D becomes postfix A B C + * D / or prefix + * A B / C D When we eliminate operator precedence with postfix or prefix notation, we greatly simplify the algorithm required to evaluate complex expressions. For example, given the postfix expression A B C + * D /, we simply read the symbols one at a time, placing them on a stack, until we encounter an operator. We then pop the first two elements off the stack, perform the operation, and then pop the result back on the stack. We repeat this process until there are no more symbols left, at which point the stack holds just one value: the result. With prefix notation, we place the operators on the stack instead of the operands. When we read the first operand we simply store it in an accumulator. We continue pushing operators onto the stack until we encounter the second operand, at which point we can pop the first operator off the stack, perform the operation and update the accumulator. We repeat this process until there are no symbols left, at which point the accumulator holds the final result. Note that when presented with an infix expression, a machine has to convert the expression to the equivalent prefix or postfix expression before it can be evaluated. By eliminating this conversion process, computation by machine can be performed with much greater speed.

Related questions

Which data structure is needed to convert infix notations to post fix notations?

stack is the basic data structure needed to convert infix notation to postfix


Why do compilers convert infix expressions to postfix?

people almost exclusively use infix notation to write mathematical expressions, computer languages almost exclusively allow programmers to use infix notation. However, if a compiler allowed infix expressions into the binary code used in the compiled version of a program, the resulting code would be larger than needed and very inefficient. Because of this, compilers convert infix expressions into postfix notation expressions, which have a much simpler set of rules for expression evaluation. Postfix notation gets its name from the fact that operators in a postfix expression follow the operands that they specify an operation on. Here are some examples of equivalent infix and postfix expressions Infix Notation Postfix Notation 2 + 3 2 3 + 2 + 3 * 6 3 6 * 2 + (2 + 3) * 6 2 3 + 6 * A / (B * C) + D * E - A - C A B C * / D E * + A C * - Where as infix notation expressions need a long list or rules for evaluation, postfix expressions need very few.


Who invented postfix and infix?

infix: old Egyptians/Assirs some thousands year before prefix: Jan &#321;ukasiewicz (Polish Notation) postfix: Burks, Warren, and Wright (Reverse Polish Notation)


Why you need convert a expression into postfix expression?

You convert an (infix) expression into a postfix expression as part of the process of generating code to evaluate that expression.


Convert infix to prefix to postfix?

(a + b) * c / ((x - y) * z)


Which data structure convert logical to physical address?

Linear data structure is used to convert the logical address to physical address .Stack is used in this and the various conversion such as postfix,prefix and infix notation are come in this


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.


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)


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)


Write an algorithm in c to convert an infix expression to a postfix expressionexecute your algorithm with the following infix expression as your input. m nk pgh a bc?

An algorithm can not be written with the following infix expression without knowing what the expression is. Once this information is included a person will be able to know how to write the algorithm.


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

Yes