Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package garcia.activity.com;
- /**
- *
- * 04986 CC-DATASTRUC21
- * 3:00-5:30 tth
- * Infix to Postfix converter
- */
- public class InfixToPostfix {
- //attribute
- private String infix;
- //constructor
- public InfixToPostfix(String infix) {
- this.infix = infix;
- }
- //default constructor
- public InfixToPostfix() {
- }
- //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;
- }
- //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)) {
- // infix to postfix
- while (!stack.isEmpty() && isLowerPrecedence(ch, stack.peek().toString().charAt(0))) {
- postfix.append(stack.pop()).append(" ");
- }
- stack.push(ch);
- } else
- postfix.append(token).append(" ");
- }
- for (; !stack.isEmpty(); postfix.append(stack.pop()).append(" "));
- }
- 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;
- }
- public boolean parenthesisChecker() {
- MyStack stack = new MyStackArray();
- for(int i = 0; i < infix.length(); i++) {
- char c = infix.charAt(i);
- if(c == '[' || c == '(' || c == '{' ) {
- stack.push(c);
- } else if(c == ']') {
- if(stack.isEmpty() || (char)stack.pop() != '[') {
- return false;
- }
- } else if(c == ')') {
- if(stack.isEmpty() || (char)stack.pop() != '(') {
- return false;
- }
- } else if(c == '}') {
- if(stack.isEmpty() || (char)stack.pop() != '{') {
- return false;
- }
- }
- }
- return stack.isEmpty();
- }
- static public void main(String... args) {
- InfixToPostfix itf = new InfixToPostfix("<[{5*(4+5)+(7/3)-(68/2)*5}]>");
- System.out.println(itf.convert());
- System.out.println(itf.compute());
- System.out.print((itf.parenthesisChecker())?"balanced":"unbalanced");
- }
- }// end of class
Advertisement
Add Comment
Please, Sign In to add comment