Advertisement
Kulas_Code20

Checker

Sep 30th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. package garcia.activity.com;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /**
  7.  * Garcia, Lloyd Samuel 04986 CC-DATASTRUC21 3:00-5:30 tth Infix to Postfix
  8.  * converter
  9.  */
  10. public class MyInfixToPostfix {
  11.     // attribute
  12.     private String infix;
  13.  
  14.     // constructor
  15.     public MyInfixToPostfix(String infix) {
  16.         this.infix = infix;
  17.     }
  18.  
  19.     // default constructor
  20.     public MyInfixToPostfix() {
  21.     }
  22.  
  23.     // essential methods
  24.     private boolean isOperator(char ch) {
  25.         return (ch == '/') || (ch == '+') || (ch == '-') || (ch == '*');
  26.     }
  27.  
  28.     private boolean isSpace(char ch) {
  29.         return ch == ' ';
  30.     }
  31.  
  32.     private boolean isLowerPrecedence(char a, char b) {
  33.         switch (a) {
  34.         case '+':
  35.         case '-':
  36.             return b != '+' || b != '-';
  37.         }
  38.         return false;
  39.     }
  40.  
  41.     public boolean parChecker() {
  42.         Map<Character, Character> brackets = new HashMap<Character, Character>();
  43.         brackets.put('(', ')');
  44.         MyStack stk = new MyStackArray();
  45.         for (int i = 0; i < infix.length(); i++) {
  46.             if (brackets.containsKey(infix.charAt(i)))
  47.                 stk.push(infix.charAt(i));
  48.             else if (brackets.containsValue(infix.charAt(i)))
  49.                 if (stk.isEmpty() || brackets.get(stk.pop()) != infix.charAt(i))
  50.                     return false;
  51.         }
  52.  
  53.         if (stk.isEmpty())
  54.             return true;
  55.         else
  56.             return false;
  57.     }
  58.  
  59.     // converter method
  60.     public String convert() {
  61.         StringBuffer postfix = new StringBuffer();
  62.         MyStack stack = new MyStackArray(infix.length());
  63.         if (!infix.equals("")) {
  64.             // use StringTokenizer to parse the infix
  65.             java.util.StringTokenizer st = new java.util.StringTokenizer(infix, "*-/+() ", true);
  66.             while (st.hasMoreTokens()) {
  67.                 String token = st.nextToken();
  68.                 char ch = token.charAt(0);
  69.                 if (isOperator(ch)) {
  70.                     postfix.append(ch);
  71.                 } else if (ch == '(') {
  72.                     stack.push(ch);
  73.                 }
  74.                 // If the scanned character is an ‘)’, pop and output from the stack
  75.                 // until an ‘(‘ is encountered.
  76.                 else if (ch == ')') {
  77.  
  78.                     while (!stack.isEmpty() && (char)stack.peek() != '(') {
  79.                         postfix.append(stack.pop());
  80.                     }
  81.                     if (!stack.isEmpty() && (char)stack.peek() != '(')
  82.                         return null;
  83.                     else if(!stack.isEmpty())
  84.                         stack.pop();
  85.                 }
  86.                 else if (isOperator(ch)) {// operator encountered
  87.                     if (!stack.isEmpty() && isLowerPrecedence(ch, stack.peek().toString().charAt(0))) {
  88.                         postfix.append(stack.pop()).append(" ");
  89.                     }
  90.                     stack.push(ch);
  91.                 }else
  92.                     postfix.append(token).append(" ");
  93.             }
  94.             while (!stack.isEmpty()) {
  95.                 postfix.append(stack.pop());
  96.             }
  97.         }
  98.         return postfix.toString();
  99.     }
  100.  
  101.     public double compute() {
  102.         double result = 0.00;
  103.         String postfix = convert();
  104.         MyStack stack = new StackLinked();
  105.         if (!postfix.equals("")) {
  106.             java.util.StringTokenizer st = new java.util.StringTokenizer(postfix, "*-/+()  ", true);
  107.             for (; st.hasMoreTokens();) {
  108.                 String token = st.nextToken();
  109.                 char ch = token.charAt(0);
  110.                 if (isOperator(ch)) {
  111.                     try {
  112.                         double b = Double.parseDouble(stack.pop().toString());
  113.                         double a = Double.parseDouble(stack.pop().toString());
  114.                         switch (ch) {
  115.                         case '*':
  116.                             stack.push(a * b);
  117.                             break;
  118.                         case '-':
  119.                             stack.push(a - b);
  120.                             break;
  121.                         case '+':
  122.                             stack.push(a + b);
  123.                             break;
  124.                         case '/':
  125.                             stack.push(a / b);
  126.                         }
  127.                     } catch (Exception e) {
  128.                     }
  129.                 } else {
  130.                     if (!isSpace(ch))
  131.                         stack.push(token);
  132.                 }
  133.             }
  134.         }
  135.         result = Double.parseDouble(stack.peek().toString());
  136.         return result;
  137.     }
  138.  
  139.     static public void main(String... args) {
  140.         MyInfixToPostfix itf = new MyInfixToPostfix("((2-3+4)*(5+6*7))");// ((2-3+4)*(5+6*7))
  141.         System.out.println(itf.convert());// 1-2+3*4/5
  142.         System.out.println(itf.compute());// ((1-2)+3)*4
  143.         System.out.println((itf.parChecker()) ? "Balance" : "Unbalance");
  144.  
  145.     }
  146. }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement