簡體   English   中英

為什么即使我將某些東西推入堆棧,堆棧仍是空的?

[英]Why is it saying stack is empty even though I push something into it?

由於某種原因,當我調用stack1.empty()或stack1.isEmpty()時,即使我將某些內容推入堆棧,它也會返回true? 這是我用來將某些東西推入堆棧的方法...

//Case B:
     else if(currentChar == '('){
         Character charObj = Character.valueOf('(');
         stack1.push(charObj);
         System.out.println("Case B");
     }

基本上,它會遍歷字符串中的每個字符,並執行我編碼的情況之一。 在這種情況下,字符為'(',因此我將其壓入堆棧。

現在,字符串中的下一個字符是字母,因此這種情況稱為:

//Case A:
     if(currentChar != '+' && currentChar != '-' && currentChar != '*' && currentChar != '/' && currentChar != '(' && currentChar != ')' ){
         postfixString += currentChar;
         System.out.println("Case A");
     }

該方法工作正常。 現在,下一部分由於某種原因而出錯。 字符串中的下一個內容是*。 因此,它應該運行我編寫的某種情況,但是它運行的是另一種情況。...這是它運行的情況:

//Case C:
     else if(stack1.empty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
         stack1.push(currentChar);
         System.out.println("Case C");
     }

如您所見,運行這種情況的唯一方法是堆棧空,但不為空! 我將某些東西推入堆棧...即使堆棧不為空,我也不明白為什么它會繼續運行這種情況。

我希望它運行這種情況:

     //Case D: If character is an operator, it goes into a loop checking if topstack is higher precedence to the current character
     // If it is, the stack pops onto the end of the postfix string. If it isn't, the stack pushes the current scanned character.
     // It then breaks out of the loop
     else if(currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/' && !stack1.isEmpty()){
         char topStack = stack1.peek();

        while(!stack1.isEmpty()){
            if(precedence(topStack, currentChar)){
                postfixString += stack1.pop();
            }
            else{
                stack1.push(currentChar);
                break;
            }

            System.out.println("Case D");

        }



     }

我的意思是應該在情況D下運行,而在情況C下運行。為什么要這樣做?

編輯:

這是整個課程:

import java.util.Stack;

public class InfixToPostfixConverter
{
//**********************************************************************
//The precedence method determines the precedence between two operators.
//If the first operator is of higher or equal precedence than the second
//operator, it returns the value true, otherwise it returns false.
//***********************************************************************
   public static boolean precedence(char topStack, char currentChar)
   {
   if(topStack == currentChar){
       return true;
   }

   // If topStack is division or multiplication, it will always have precedence no matter what
   if(topStack == '/' || topStack == '*'){
       return true;
   }
   // If topStack is addition or subtraction...
   else if(topStack == '+' || topStack == '-'){
       if(currentChar == '+' || currentChar == '-'){
           return true;
       }
       else if(currentChar == '*' || currentChar == '/'){
           return false;
       }
   }

   return false;
   }

//*************************************************************************
//The static convertToPostfix method will convert the infixString
//into the corresponding postfix string. Check the algorithm on
//assignment #11's description page. Mark each case clearly inside the code
//*************************************************************************
   public static String convertToPostfix(String infixString)
   {
  //initialize the resulting postfix string
  String postfixString = "";

  //initialize the stack
  Stack<Character> stack1 = new Stack<Character>();

 //Obtain the character at index i in the string
  for (int i=0; i < infixString.length(); i++)
  {
     char currentChar = infixString.charAt(i);

    //Case A:
     if(currentChar != '+' && currentChar != '-' && currentChar != '*' && currentChar != '/' && currentChar != '(' && currentChar != ')' ){
         postfixString += currentChar;
         System.out.println("Case A");
     }

    //Case B:
     else if(currentChar == '('){
         stack1.push(currentChar);
         System.out.println("Case B");
     }

     else if(currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
         //Case C
         if(stack1.isEmpty()){
             stack1.push(currentChar);
             System.out.println("Case C");
         }
         //Case D
         else{
             char topStack = stack1.peek();

             while(!stack1.isEmpty()){
                if(precedence(topStack, currentChar)){
                    postfixString += stack1.pop();
                }
                else{
                    stack1.push(currentChar);
                    break;
                }

                System.out.println("Case D");

            }
         }
     }
    //Case E:
     else if(currentChar == ')' && !stack1.isEmpty()){
         while(!stack1.isEmpty() && stack1.peek() != '('){
             postfixString += stack1.pop();
             System.out.println("Case E");
         }
         if(!stack1.isEmpty() && stack1.peek() == '('){
             stack1.pop();
         }
     }


  } //end of for loop


    //Case F:
  if(!stack1.isEmpty() && stack1.peek() == '('){
      return "No matching close parenthesis error.";
  }
  else if(!stack1.isEmpty() && stack1.peek() != '('){
      while(!stack1.isEmpty() && stack1.peek() != '('){
          postfixString += stack1.pop();
      }
  }

  System.out.println("Case F");
  return postfixString;


}//end of convertToPostfix method

}//end of the InfixToPostfixConverter class

&&運算符的優先級高於|| ,因此在此語句中:

else if(stack1.empty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){

以上等同於:

else if( ( stack1.empty() && currentChar == '+' ) || currentChar == '-' || currentChar == '*' || currentChar == '/'){

舉一個簡單的例子:

if (a && b || c)

相當於

if ( (a && b) || c )

如有疑問,請添加括號以使操作順序明確,以便您(和其他閱讀代碼的程序員)清楚您的意圖。

說明

這對於您的總體問題意味着什么,您的stack1可能不是空的。 else if我在上面引用過, stack1.empty() && currentChar == '+'都必須為true。 由於不是這種情況,因此將評估下一項,直到它到達currentChar == '*'為止,這正確的,因此它將運行案例C。

您的情況D永遠不會正確,因為情況C已經檢查了相同的字符。 情況D將無法達成。

假設案例C表示“ stack1為空”和“ currentChar是+,-,*或/之一”,那么您需要這樣寫:

else if (stack1.empty() && (currentChar == '+' || ...)) {

但是由於您每次都要檢查相同的字符,因此我個人會使用多級if語句:

else if (currentChar == '+' || currentChar == '-' || ...) {
    if (stack1.empty()) {
    // Case C

    } else {
    // Case D

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM