Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package garcia.activity.com;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Garcia, Lloyd Samuel 04986 CC-DATASTRUC21 3:00-5:30 tth Infix to Postfix
- * converter
- */
- public class MyInfixToPostfix {
- // attribute
- private String infix;
- // constructor
- public MyInfixToPostfix(String infix) {
- this.infix = infix;
- }
- // default constructor
- public MyInfixToPostfix() {
- }
- // essential methods
- private boolean isOperator(char ch) {
- return (ch == '/') || (ch == '+') || (ch == '-') || (ch == '*');
- }
- private boolean isSpace(char ch) {
- return ch == ' ';
- }
- private boolean isLowerPrecedence(char a, char b) {
- switch (a) {
- case '+':
- case '-':
- return b != '+' || b != '-';
- }
- return false;
- }
- public boolean parChecker() {
- Map<Character, Character> brackets = new HashMap<Character, Character>();
- brackets.put('(', ')');
- MyStack stk = new MyStackArray();
- for (int i = 0; i < infix.length(); i++) {
- if (brackets.containsKey(infix.charAt(i)))
- stk.push(infix.charAt(i));
- else if (brackets.containsValue(infix.charAt(i)))
- if (stk.isEmpty() || brackets.get(stk.pop()) != infix.charAt(i))
- return false;
- }
- if (stk.isEmpty())
- return true;
- else
- return false;
- }
- // converter method
- public String convert() {
- StringBuffer postfix = new StringBuffer();
- MyStack stack = new MyStackArray(infix.length());
- if (!infix.equals("")) {
- // use StringTokenizer to parse the infix
- java.util.StringTokenizer st = new java.util.StringTokenizer(infix, "*-/+() ", true);
- while (st.hasMoreTokens()) {
- String token = st.nextToken();
- char ch = token.charAt(0);
- if (isOperator(ch)) {
- postfix.append(ch);
- } else if (ch == '(') {
- stack.push(ch);
- }
- // If the scanned character is an ‘)’, pop and output from the stack
- // until an ‘(‘ is encountered.
- else if (ch == ')') {
- while (!stack.isEmpty() && (char)stack.peek() != '(') {
- postfix.append(stack.pop());
- }
- if (!stack.isEmpty() && (char)stack.peek() != '(')
- return null;
- else if(!stack.isEmpty())
- stack.pop();
- }
- else if (isOperator(ch)) {// operator encountered
- if (!stack.isEmpty() && isLowerPrecedence(ch, stack.peek().toString().charAt(0))) {
- postfix.append(stack.pop()).append(" ");
- }
- stack.push(ch);
- }else
- postfix.append(token).append(" ");
- }
- while (!stack.isEmpty()) {
- postfix.append(stack.pop());
- }
- }
- return postfix.toString();
- }
- public double compute() {
- double result = 0.00;
- String postfix = convert();
- MyStack stack = new StackLinked();
- 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;
- }
- static public void main(String... args) {
- MyInfixToPostfix itf = new MyInfixToPostfix("((2-3+4)*(5+6*7))");// ((2-3+4)*(5+6*7))
- System.out.println(itf.convert());// 1-2+3*4/5
- System.out.println(itf.compute());// ((1-2)+3)*4
- System.out.println((itf.parChecker()) ? "Balance" : "Unbalance");
- }
- }// end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement