Java 从中缀到后缀括号的转换问题
目前,我正在研究一种从中缀符号转换为后缀符号的方法。我已经编写了多个JUnit测试,像2*2或1+3这样的常规测试都能正常工作,但任何带括号的测试都不能正常工作 例如,(5+2)*(3*5) 在调试过程中,我总是看到我的postFixResult字符串总是将第一个括号添加到字符串中,因此字符串的开头是5(2)。我不确定我做错了什么,因为我已经在这里介绍了关于这个问题的多篇文章Java 从中缀到后缀括号的转换问题,java,Java,目前,我正在研究一种从中缀符号转换为后缀符号的方法。我已经编写了多个JUnit测试,像2*2或1+3这样的常规测试都能正常工作,但任何带括号的测试都不能正常工作 例如,(5+2)*(3*5) 在调试过程中,我总是看到我的postFixResult字符串总是将第一个括号添加到字符串中,因此字符串的开头是5(2)。我不确定我做错了什么,因为我已经在这里介绍了关于这个问题的多篇文章 public static String infixToPostFix(String message) throws P
public static String infixToPostFix(String message) throws PostFixException {
MyStack<Character> operatorStack = new MyStack<Character>();
String postFixResult = "";
Scanner tokenizer = new Scanner(message);
while (tokenizer.hasNext()) {
if (tokenizer.hasNextInt()) {
postFixResult += " " + tokenizer.nextInt();
} else {
String value = tokenizer.next();
Operator op = new Operator(value.charAt(0));
char c = value.charAt(0);
String operators = "*/-+()";
if (!isOperator(c)) {
//If the character is not an operator or a left parentheses.
throw new PostFixException("Invalid Operator");
} else {
if (c == ')') {
while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
postFixResult += " " + operatorStack.pop();
}
if (!operatorStack.isEmpty()) {
operatorStack.pop();
}
}
else {
if (!operatorStack.isEmpty() && !isLowerPrecedence(c, operatorStack.peek())) {
operatorStack.push(c);
}
else {
while (!operatorStack.isEmpty() && isLowerPrecedence(c, operatorStack.peek())) {
Character pop = operatorStack.pop();
if (c != '(') {
postFixResult += " " + pop;
} else {
c = pop;
}
}
operatorStack.push(c);
}
}
}
}
}
while (!operatorStack.isEmpty()) {
postFixResult += " " + operatorStack.pop();
}
return postFixResult;
}
/**
* Method that returns true if c is an operator.
* @param c
* @return
*/
private static boolean isOperator(char c)
{
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^'
|| c == '(' || c == ')';
}
/**
* Determines whether or not the character is actually a number.
* @param c
* @return true if character is number false, otherwise.
*/
private boolean isNumber(char c){
return Character.isDigit(c);
}
/**
* A method to determine precedence of operators.
* @param c1 Operator 1
* @param c2 Operator 2
* @return true if c2 is lower precedence than c1.
*/
private static boolean isLowerPrecedence(char c1, char c2)
{
switch (c1)
{
case '+':
case '-':
return !(c2 == '+' || c2 == '-');
case '*':
case '/':
return c2 == '^' || c2 == '(';
case '^':
return c2 == '(';
case '(':
return true;
default:
//means that the character must have passed through as something else.
return false;
}
}
公共静态字符串infixToPostFix(字符串消息)引发PostFixException{
MyStack操作符stack=新MyStack();
字符串postfix结果=”;
扫描器标记器=新扫描器(消息);
while(tokenizer.hasNext()){
if(tokenizer.hasNextInt()){
postFixResult+=“”+tokenizer.nextInt();
}否则{
String value=tokenizer.next();
运算符op=新运算符(value.charAt(0));
字符c=值。字符(0);
字符串运算符=“*/-+()”;
如果(!等参算子(c)){
//如果字符不是运算符或左括号。
抛出新的PostFixException(“无效运算符”);
}否则{
如果(c==')'){
而(!operatorStack.isEmpty()&&operatorStack.peek()!='('){
postFixResult+=“”+运算符stack.pop();
}
如果(!operatorStack.isEmpty()){
运算符stack.pop();
}
}
否则{
if(!operatorStack.isEmpty()&&!isLowerRecessence(c,operatorStack.peek()){
操作器固定推力(c);
}
否则{
而(!operatorStack.isEmpty()&&isLowerRecessence(c,operatorStack.peek()){
字符pop=运算符stack.pop();
如果(c!='('){
postFixResult+=“”+pop;
}否则{
c=流行音乐;
}
}
操作器固定推力(c);
}
}
}
}
}
而(!operatorStack.isEmpty()){
postFixResult+=“”+运算符stack.pop();
}
返回后处理结果;
}
/**
*方法,如果c是运算符,则返回true。
*@param c
*@返回
*/
专用静态布尔等运算符(字符c)
{
返回c='+'| | c='-'| | c='*'| | c='/'| | c='^'
||c=='('| c==');
}
/**
*确定字符是否实际上是数字。
*@param c
*@如果字符为数字false,则返回true,否则返回true。
*/
专用布尔isNumber(字符c){
返回字符.isDigit(c);
}
/**
*一种确定运算符优先级的方法。
*@param c1运算符1
*@param c2操作员2
*@如果c2的优先级低于c1,则返回true。
*/
私有静态布尔IsLowerRecessence(字符c1、字符c2)
{
开关(c1)
{
格“+”:
案例'-':
返回!(c2='+'| | c2=='-');
案例“*”:
案例“/”:
返回c2='^'| | c2=='(');
案例‘^’:
返回c2=='(';
格“(”:
返回true;
违约:
//表示角色必须作为其他对象通过。
返回false;
}
}
后缀表达式中不需要括号,这就是它的优点。(“and”)不应该进入最终结果(后缀),从堆栈本身弹出时应该丢弃。后缀表达式中不需要括号,这就是它的优点。(“and”)不应该进入最终结果(后缀)当从堆栈本身弹出时,应该丢弃。在点”(“
添加到postFixResult
)的运算符堆栈是什么?它是您期望的吗?(顺便说一句,我不知道这是否会得出答案,但这是一个复杂的程序,我现在太累了,无法进行那种深入的调试,这实际上是你的工作。)“(”和“)”不是等运算符()
在该算法中,需要单独处理。您的Dijkstra调车场算法格式不正确。请查找它。在点“(“
被添加到postFixResult
”)处的运算符堆栈是什么?它是您期望的吗?(顺便说一句,我不知道这是否会得出答案,但这是一个复杂的程序,我现在太累了,无法进行那种深入的调试,这实际上是你的工作。)“(”和“)”不是等运算符()
在该算法中,需要单独处理。您的Dijkstra调车场算法格式不正确。请查找。