I am making a converter that will take infix expressions and convert them into postfix expressions.
Example:
Infix: 2 * 3 - 10 / 4
Postfix: 2 3 * 10 4 / -
I have a the method completely coded but the postfix expression it returns is
2 3 * 1 0 4 / -
There are two problems with this: 1. The major problem is that their is a space between the 1 and 0, when they should be together (10). 2. There are a lot of extra spaces, the output should look like the example provided above.
I have done research on converting from infix to postfix, but I couldn’t determine how to do more then single digit expression conversions.
Below is attached my postfixtoinfix class, the expression variable holds the infix indicated in the example above with perfect spacing.
import java.util.*;
public class InfixToPostfix
{
//Declare Instance Variables
private String expression;
private Stack<Character> stack = new Stack<Character>();
//Constructor
public InfixToPostfix(String infixExpression)
{
expression = infixExpression;
}//End of constructor
//Translate's the expression to postfix
public String translate()
{
//Declare Method Variables
String input = "";
String output = "";
char character = ' ';
char nextCharacter = ' ';
for(int x = 0; x < expression.length(); x++)
{
character = expression.charAt(x);
if(isOperator(character))
{
while(!stack.empty() && precedence(stack.peek())>= precedence(character))
output += stack.pop() + " ";
stack.push(character);
}
else if(character == '(')
{
stack.push(character);
}
else if(character == ')')
{
while(!stack.peek().equals('('))
output += stack.pop() + " ";
stack.pop();
}
else
{
if(Character.isDigit(character) && (x + 1) < expression.length() && Character.isDigit(expression.charAt(x+1)))
{
output += character;
}
else if(Character.isDigit(character))
{
output += character + " ";
}
else
{
output += character;
}
}
}//End of for
while(!stack.empty())
{
output += stack.pop() + " ";
}
return output;
}//End of translate method
//Check priority on characters
public static int precedence(char operator)
{
if(operator == '+' || operator =='-')
return 1;
else if(operator == '*' || operator == '/')
return 2;
else
return 0;
}//End of priority method
public boolean isOperator(char element)
{
if(element == '*' || element == '-' || element == '/' || element == '+')
return true;
else
return false;
}//End of isOperator method
}//End of class
Your code is not seeing “10” as a single entity, but rather as two separate characters, ‘1’, and ‘0’. For anything that isn’t an operator or parens you do
output += character + " ";which is going to give you your1 0instead of the desired10.