2012-06-25 104 views
6

我創建了一個堆棧計算器爲我的Java類求解方程如後綴堆棧計算器

2 + (2 * (10 – 4)/((4 * 2/(3 + 4)) + 2) – 9) 
2 + { 2 * (10 – 4)/[ { 4 * 2/(3 + 4) } + 2 ] – 9 } 

我們想實現{ } [ ]到我們的代碼。我只用括號做了。它工作100%,只有()。當我嘗試添加{ } [ ]時,它變成了香蕉。

這是我到目前爲止有:

package stackscalc; 

import java.util.Scanner; 
import java.util.Stack; 
import java.util.EmptyStackException; 


class Arithmetic { 
    int length; 
    Stack stk; 
    String exp, postfix; 

    Arithmetic(String s) { 
     stk = new Stack(); 
     exp = s; 
     postfix = ""; 
     length = exp.length(); 

    } 
    boolean isBalance() { 
     boolean fail = false; 
     int index = 0; 

     try { 
      while (index < length) { 
       char ch = exp.charAt(index); 

       switch(ch) { 
       case ')': 
        stk.pop(); 
        break; 

       case '(': 
        stk.push(new Character(ch)); 
        break; 

       default: 
        break; 
       } 
       index++; 
      } 
     } catch (EmptyStackException e) { 
      fail = true; 
     } 
     return stk.empty() && !fail; 
    } 
    void postfixExpression() { 
     String token = ""; 
     Scanner scan = new Scanner(exp); 
     stk.clear(); 

     while(scan.hasNext()) { 
      token = scan.next(); 
      char current = token.charAt(0); 

      if (isNumber(token)) { 
       postfix = postfix + token + " "; 
      } else if(isParentheses(current)) { 
       if (current == '(') { 
        stk.push(current); 
       } else { 
        Character ch = (Character) stk.peek(); 
        char nextToken = ch.charValue(); 

        while(nextToken != '(') { 
         postfix = postfix + stk.pop() + " "; 

         ch = (Character) stk.peek(); 

         nextToken = ch.charValue(); 
        } 
        stk.pop(); 
       } 
      } else { 
       if (stk.empty()) { 
        stk.push(current); 
       } else { 
        Character ch = (Character) stk.peek(); 
        char top = ch.charValue(); 

        if (hasHigherPrecedence(top, current)) { 
         stk.push(current); 
        } else { 
         ch = (Character) stk.pop(); 

         top = ch.charValue(); 

         stk.push(current); 

         stk.push(top); 
        } 
       } 
      } 
     } 
     try { 
      Character ch = (Character) stk.peek(); 
      char nextToken = ch.charValue(); 

      while (isOperator(nextToken)) { 
       postfix = postfix + stk.pop() + " "; 
       ch = (Character) stk.peek(); 
       nextToken = ch.charValue(); 
      } 
     } catch (EmptyStackException e) {} 
    } 
    boolean isNumber(String s) { 
     try { 
      int Num = Integer.parseInt(s); 
     } catch(NumberFormatException e) { 
      return false; 
     } 
     return true; 
    } 
    void evaluateRPN() { 
     Scanner scan = new Scanner(postfix); 
     String token = ""; 
     stk.clear(); 

     while(scan.hasNext()) { 
      try { 
       token = scan.next(); 
       if (isNumber(token)) { 
        stk.push(token); 
       } else { 
        char current = token.charAt(0); 
        double t1 = Double.parseDouble(stk.pop().toString()); 
        double t2 = Double.parseDouble(stk.pop().toString()); 
        double t3 = 0; 

        switch (current) { 
        case '+': { 
         t3 = t2 + t1; 
         stk.push(t3); 
         break; 
        } 
        case '-': { 
         t3 = t2 - t1; 
         stk.push(t3); 
         break; 
        } 
        case '*': { 
         t3 = t2 * t1; 
         stk.push(t3); 
         break; 
        } 
        case '/': { 
         t3 = t2/t1; 
         stk.push(t3); 
         break; 
        } 
        default: { 
         System.out.println("Reverse Polish Notation was unable to be preformed."); 
        } 
       } 
      } 

     } catch (EmptyStackException e) {} 
    } 
} 
String getResult() { 
    return stk.toString(); 
} 

int stackSize() { 
    return stk.size(); 
} 

boolean isParentheses(char current) { 
    if ((current == '(') || (current == ')')) { 
     return true; 
    } else { 
     return false; 
    } 
} 

boolean isOperator(char ch) { 
    if ((ch == '-')) { 
     return true; 
    } else if ((ch == '+')) { 
     return true; 
    } 
    else if ((ch == '*')) { 
     return true; 
    } 
    else if((ch == '/')) { 
     return true; 
    } else { 

    } 
    return false; 
} 

boolean hasHigherPrecedence(char top, char current) { 
    boolean HigherPre = false; 

    switch (current) { 
    case '*': 
     HigherPre = true; 
    break; 

    case '/': 
     HigherPre = true; 
    break; 

    case '+': 

     if ((top == '*') || (top == '/') || (top == '-')) { 
      HigherPre = false; 
     } else { 
      HigherPre = true; 
     } 

     break; 

    case '-': 
     if ((top == '*') || (top == '/') || (top == '-')) { 
      HigherPre = false; 
     } else { 
      HigherPre = true; 
     } 
     break; 

    default: 
     System.out.println("Higher Precedence Unsuccessful was unable to be preformed."); 
     break; 
    } 

    return HigherPre; 


    } 

    String getPostfix() { 
     return postfix; 
    } 
} 
+0

你應該添加'{} []'來做什麼? – EJP

回答

1

什麼我假設是(){}和[]都具有相同的權重的操作順序而言,你只需要修改你的代碼,以便允許所有三個可以互換。

如果是這樣的話,我只需使用matcher類和一個簡單的正則表達式檢查來查看當前正在查看的字符是括號,大括號還是括號。

//convert char to string 
    String temp += currentChar; 
    //this will check for (, [, and { (need escapes because of how regex works in java) 
    Pattern bracePattern = Pattern.compile("[\(\{\[]"); 
    Matcher matcher = numPatt.matcher(temp); 
    if(matcher.find()){ 
    //you know you have a grouping character 
    } 

此代碼應該讓你找到所有的開放分組字符(剛剛替補({和[爲)}和]正則表達式查找關閉字符)。這可以用在你的isParenthesis()方法中。

+2

你還需要更新if(current =='(')'和while(nextToken!='(')'這兩行,你的'isParenthesis()'也在檢查關閉的parens,所以請確保你不只是嘗試複製/粘貼這個答案= P – Windle

+1

好的呼叫,我可以使用正則表達式檢查的地方有點模糊 –

+2

你不想處理'(2 + 3)',你做? – Arkadiy