Kulas_Code20

InfixToPostfix

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