Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.swing;
- import java.util.*;
- /**
- * Zarco, Lousie Joaquine G. 04986 CC-DATASTRUC21 3:00-5:30 tth Infix to Postfix
- * converter
- */
- public class ZarcoInfixToPostfix {
- // attribute
- private String infix;
- // constructor
- public ZarcoInfixToPostfix(String infix) {
- this.infix = infix;
- }
- // default constructor
- public ZarcoInfixToPostfix() {
- }
- // essential methods
- private boolean isOperator(char ch) {
- return (ch == '/') || (ch == '+') || (ch == '-') || (ch == '*');
- }
- private boolean isSpace(char ch) {
- return ch == ' ';
- }
- private int isLowerPrecedence(char op) {
- int res = 0;
- switch (op) {
- case '-':
- case '+':
- res = 1;
- break;
- case '*':
- case '/':
- res = 2;
- }
- return res;
- }
- public String convertPfx() {
- MyStack stack = new MyStackLinked();
- char c;
- StringBuffer postfix = new StringBuffer(infix.length());
- char symbol;
- for (int i = 0; i < infix.length(); ++i) {
- symbol = infix.charAt(i);
- if (Character.isDigit(symbol)) {
- postfix.append(symbol).append(" ");
- } else if (symbol == '(') {
- stack.push(symbol);
- } else if (symbol == ')') {
- while ((char) stack.peek() != '(') {
- postfix.append((char) stack.pop()).append(" ");
- }
- stack.pop();
- } else {
- while (!stack.isEmpty() && !((char) stack.peek() == '(')
- && isLowerPrecedence(symbol) <= isLowerPrecedence((char) stack.peek()))
- postfix.append((char) stack.pop()).append(" ");
- stack.push(symbol);
- }
- }
- while (!stack.isEmpty())
- postfix.append((char) stack.pop()).append(" ");
- return postfix.toString();
- }// end convertToPostfix
- public double compute() {
- double result = 0.00;
- String postfix = convertPfx();
- MyStack stack = new MyStackLinked();
- if (!postfix.equals("")) {
- java.util.StringTokenizer st = new java.util.StringTokenizer(postfix, "*-/+()[]{}<> ", true);
- for (; st.hasMoreTokens();) {
- String token = st.nextToken();
- char ch = token.charAt(0);
- if (isOperator(ch)) {
- try {
- double b = Double.parseDouble(stack.pop().toString());
- double a = Double.parseDouble(stack.pop().toString());
- switch (ch) {
- case '*':
- stack.push(a * b);
- break;
- case '-':
- stack.push(a - b);
- break;
- case '+':
- stack.push(a + b);
- break;
- case '/':
- stack.push(a / b);
- }
- } catch (Exception e) {
- }
- } else {
- if (!isSpace(ch))
- stack.push(token);
- }
- }
- }
- result = Double.parseDouble(stack.peek().toString());
- return result;
- }
- public boolean parenthesisChecker() {
- MyStack stack = new MyStackArray();
- for (int n = 0; n < infix.length(); n++) {
- char ch = infix.charAt(n);
- if (ch == '(') {
- stack.push(ch);
- } else if (ch == ')') {
- if (stack.isEmpty() || (char) stack.pop() != '(') {
- return false;
- }
- }
- }
- return stack.isEmpty();
- }
- static public void main(String... args) {
- ZarcoInfixToPostfix itf = new ZarcoInfixToPostfix("1-2*3+4/5");
- System.out.println(itf.convertPfx());
- System.out.println(itf.compute());
- System.out.print((itf.parenthesisChecker()) ? "balanced" : "unbalanced");
- }
- } // end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement