将Infix表达式转换为Postfix表达式-无效的空格插入 [英] Converting a Infix expression into a Postfix expression - invalid space insertion
问题描述
我正在制作一个将使用中缀表达式并将其转换为后缀表达式的转换器.
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 / -
这有两个问题:1.主要问题是,当它们应该在一起时,它们是1和0之间的空格(10). 2.有很多多余的空间,输出应类似于上面的示例.
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.
我已经完成了从infix到postfix转换的研究,但是我无法确定除单位表达式转换之外的其他方法.
I have done research on converting from infix to postfix, but I couldn't determine how to do more then single digit expression conversions.
下面是我的postfixtoinfix类的附件,表达式变量以完美的间距保存上面示例中指示的中缀.
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
推荐答案
您的代码不会将"10"视为单个实体,而是将其视为两个单独的字符"1"和"0".对于不是运算符或原谅的任何事情,您可以执行output += character + " ";
,这将为您提供1 0
而不是所需的10
.
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 your 1 0
instead of the desired 10
.
这篇关于将Infix表达式转换为Postfix表达式-无效的空格插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!