Advertisement
Kulas_Code20

ZarcoInfixToPostfix

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