2012-06-25 11 views
6

मैं के रूप मेंपोस्टफिक्स ढेर कैलकुलेटर

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

क्या मैं यह सोचते हैं कि (है {}, और [] सभी कार्यों के आदेश के संदर्भ में एक ही वजन है, और आप बस की जरूरत है:

यह वही है मैं अब तक किया है सभी तीनों के लिए एक दूसरे के लिए अनुमति देने के लिए अपने कोड को संशोधित करने के लिए।

यदि ऐसा है, तो मैं केवल एक सामान्य रेगेक्स चेक के साथ मैचर क्लास का उपयोग करता हूं यह देखने के लिए कि वर्तमान तार जो आप देख रहे हैं या तो एक कोष्ठक, घुंघराले ब्रेस, या ब्रैकेट है।

//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 
    } 

इस कोड को आप सभी उद्घाटन समूहीकरण पात्रों को खोजने के लिए अनुमति चाहिए (बस स्थानापन्न (, regex में बंद पात्रों को खोजने के लिए {और [के लिए),} और])। इसका उपयोग आपके isParenthesis() विधि में किया जा सकता है।

+2

आपको 'if (current ==' (')' और 'while (nextToken! = '(') 'जैसे लाइनों को भी अपडेट करना होगा। आपका' isParenthesis()' करीबी माता-पिता के लिए भी जांच कर रहा है, इसलिए सुनिश्चित करें कि आप इस उत्तर का प्रयास न करें और कॉपी/पेस्ट करें = पी – Windle

+1

अच्छी कॉल। मैं थोड़ा अस्पष्ट था जहां रेगेक्स चेक का उपयोग किया जा सकता था। –

+2

आप '(2 + 3}' को संसाधित नहीं करना चाहते हैं, क्या आप? – Arkadiy

संबंधित मुद्दे