java - Java 中缀到后缀转换的括号错误检查

标签 java stack infix-notation postfix-notation

我一直在开发一个用于中缀到后缀转换的程序,并且一切正常,除了我无法弄清楚在哪里对缺少的左括号进行错误检查。基本上,用户输入一个字符串,程序会转到此类并对其进行转换,但我想确保他们输入了正确数量的括号。我已经尝试了多个地方,但不断出现 EmptyStackExceptions。

import java.util.*;

public class PostfixConversion {


   public static boolean precedence(char first, char second)
   {
      int v1 = 0, v2 = 0;
      //find value for first
      if(first == '-' || first == '+'){
         v1 = 1;
      }else if(first == '*' || first == '/'){
         v1 = 2;    
      }//end if

      //find value for second
      if(second == '-' || second == '+'){
         v2 = 1;
      }else if(second == '*' || second == '/'){
         v2 = 2;    
      }//end if

     if(v1 < v2){
        return false;
     }//end if

     return true;
  }//end precedence method

 //converts infix expression into postfix expression
 public static String convertToPostfix(String infixExp)
  {
     String postFix = "The Postfix Expression is: ";
     Stack<Character> stack = new Stack<Character>();
     char character = ' ';

     for(int i = 0; i < infixExp.length(); i++)
     {
         character = infixExp.charAt(i);

         //determine if character is an operator
         if(character == '*' || character == '-' || character == '/' || character == '+')
         {
             while(!stack.empty() && precedence(stack.peek(), character)){
                 postFix += stack.pop();
             }//end while
             stack.push(character);
         }
         else if(character == '(') //check for left parenthesis
         {
             stack.push(character);
         }
         else if (character == ')') 
         {
             while(!stack.peek().equals('(') && !stack.isEmpty()){ //add characters until left parenthesis
                 postFix += stack.pop();
             }//end while

             if(!stack.isEmpty() && stack.peek().equals('(')){
                 stack.pop(); // pop/remove left parenthesis
             }
         }
         else
         {
             postFix += character;
         }//end if
     }//end for
     while(!stack.empty()) //add the remaining elements of stack to postfix expression
     {
         if(stack.peek().equals('('))
         {
             postFix = "There is no matching right parenthesis.";
             return postFix;
         }
         postFix += stack.pop();
     }
         return postFix;
 }//end convertToPostfix
}

最佳答案

首先,您必须将 while 循环更改为以下形式:

while (!stack.empty() && precedence(stack.peek(), character)) {
    postFix += stack.pop();
}

即更改 while 检查中表达式的顺序:stack.empty() 检查应该是第一个。

第二个修复将在此处添加“没有匹配的左括号。” 错误消息:

if (!stack.isEmpty() && stack.peek().equals('(')) {
   stack.pop(); // pop/remove left parenthesis
} else {
   postFix = "There is no matching left parenthesis.";
   return postFix;
}

关于java - Java 中缀到后缀转换的括号错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16100036/

相关文章:

java - 堆栈扩展 vector

list - Haskell 中后缀形式的中缀

java - 将中缀字符串转换为二叉树的代码,我收到了: Uncompilable source code - Erroneous sym type

haskell - `flip` 中缀应用程序内联参数

java - Java 中的变量值开关案例

java - 如何在Android Studio上修复Gradle错误

java - Quarkus mongodb 集成

java - 如何限制 JTextArea 的最大行数和列数?

java - java中使用单栈实现队列

c++ - 检查堆栈中是否存在元素