answersLogoWhite

0

The following class should convert prefix to infix expression. I hope the logic is pretty simple and easy to understand. For any queries contact me at keepintouchsk@gmail.com

// Author: Skay

// Date: Nov 7, 2010

import java.util.Stack;

import java.util.Enumeration;

public class PrefixToInfixConverter

{

public static Stack<String> infixStk = new Stack<String>();

public PrefixToInfixConverter() { }

public static boolean isOper(String tstr)

{

if( tstr.equals("+") tstr.equals("-") tstr.equals("*") tstr.equals("/") )

return true;

else

return false;

}

public static String convert(String prefixStr)

{

String infixStr=new String();

String tempString = new StringBuffer(prefixStr).reverse().toString();

for(int i=tempString.length()-1;i>=0;i--)

{

while(tempString.charAt(i)==' ')

i--;

infixStk.push(String.valueOf(tempString.charAt(i)));

}

String[] val = new String[50];

int i,j;

while(infixStk.size()>1)

{

i=0;

while(!isOper(val[i++]=infixStk.pop()));

val[i-3] = new String("(" + val[i-2] + val[i-1] + val[i-3] + ")" );

for(j=i-3;j>=0;j--)

infixStk.push(val[j]);

}

return infixStk.pop();

}

}

Input: /-ab*c+de Output: ((a-b)/(c*(d+e)))

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

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 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.


What is prefix expression?

Example: prefix: * 2 + 3 4 infix: 2 * (3+4) postfix: 2 3 4 + *


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.


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)

Related Questions

Program to convert infix to prefix?

I dont have the idea about the program but I know that prefix means the first starting letters of a particular things. I really think so there is a progam to convert infix to prefix but i might have misunderstood your question can you make it little simpler please.


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


Convert infix to prefix to postfix?

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


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)


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


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.


Infix t o profix exmple?

Infix notation is a common way of writing expressions where operators are placed between operands, such as in &quot;A + B&quot;. Profix notation, often referred to as prefix notation or Polish notation, places the operator before the operands, resulting in &quot;+ A B&quot;. For example, the infix expression &quot;A + B&quot; would be written as &quot;+ A B&quot; in profix notation. This structure eliminates the need for parentheses to indicate operation order, as the position of operators and operands inherently defines it.


What are the three kinds of affixes?

Prefix, suffix and infix


Prefix suffix and infix examples with meaning and word definition?

An example of a prefix in the English language is pre, meaning before. An example of a suffix would be ing, meaning a verbal action. An example of an infix would be ful, meaning full of.


What is prefix expression?

Example: prefix: * 2 + 3 4 infix: 2 * (3+4) postfix: 2 3 4 + *


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.


What are the prefixes and suffixes in punctual compared to the prefixes and suffixes in punctilitous?

The prefix in &quot;punctual&quot; is &quot;pun-&quot; and the suffix is &quot;-ual&quot;. In &quot;punctilious&quot;, the prefix is &quot;pun-&quot;, the infix is &quot;-ctil-&quot;, and the suffix is &quot;-ious&quot;.