|
| 1 | +/* |
| 2 | +Copyright (C) Deepali Srivastava - All Rights Reserved |
| 3 | +This code is part of DSA course available on CourseGalaxy.com |
| 4 | +*/ |
| 5 | + |
| 6 | +package postfix; |
| 7 | + |
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +public class Demo |
| 11 | +{ |
| 12 | + public static void main(String[] args) |
| 13 | + { |
| 14 | + String infix; |
| 15 | + |
| 16 | + Scanner scan = new Scanner(System.in); |
| 17 | + |
| 18 | + System.out.print("Enter infix expression : "); |
| 19 | + infix = scan.nextLine(); |
| 20 | + |
| 21 | + String postfix = infixToPostfix(infix); |
| 22 | + |
| 23 | + System.out.println("Postfix expression is : " + postfix); |
| 24 | + |
| 25 | + System.out.println("Value of expression : " + evaluatePostfix(postfix)); |
| 26 | + |
| 27 | + scan.close(); |
| 28 | + } |
| 29 | + |
| 30 | + public static String infixToPostfix(String infix) |
| 31 | + { |
| 32 | + String postfix = new String(); |
| 33 | + |
| 34 | + StackChar st = new StackChar(20); |
| 35 | + |
| 36 | + char next,symbol; |
| 37 | + for(int i=0; i<infix.length(); i++) |
| 38 | + { |
| 39 | + symbol=infix.charAt(i); |
| 40 | + |
| 41 | + if(symbol==' ' || symbol=='\t') /*ignore blanks and tabs*/ |
| 42 | + continue; |
| 43 | + |
| 44 | + switch(symbol) |
| 45 | + { |
| 46 | + case '(': |
| 47 | + st.push(symbol); |
| 48 | + break; |
| 49 | + case ')': |
| 50 | + while((next=st.pop())!='(') |
| 51 | + postfix = postfix + next; |
| 52 | + break; |
| 53 | + case '+': |
| 54 | + case '-': |
| 55 | + case '*': |
| 56 | + case '/': |
| 57 | + case '%': |
| 58 | + case '^': |
| 59 | + while( !st.isEmpty() && precedence(st.peek())>= precedence(symbol) ) |
| 60 | + postfix = postfix + st.pop(); |
| 61 | + st.push(symbol); |
| 62 | + break; |
| 63 | + default: /*operand*/ |
| 64 | + postfix = postfix + symbol; |
| 65 | + } |
| 66 | + } |
| 67 | + while(!st.isEmpty()) |
| 68 | + postfix = postfix + st.pop(); |
| 69 | + return postfix; |
| 70 | + } |
| 71 | + |
| 72 | + public static int precedence(char symbol) |
| 73 | + { |
| 74 | + switch(symbol) |
| 75 | + { |
| 76 | + case '(': |
| 77 | + return 0; |
| 78 | + case '+': |
| 79 | + case '-': |
| 80 | + return 1; |
| 81 | + case '*': |
| 82 | + case '/': |
| 83 | + case '%': |
| 84 | + return 2; |
| 85 | + case '^': |
| 86 | + return 3; |
| 87 | + default : |
| 88 | + return 0; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static int evaluatePostfix(String postfix) |
| 93 | + { |
| 94 | + StackInt st = new StackInt(20); |
| 95 | + |
| 96 | + int x,y; |
| 97 | + for(int i=0; i<postfix.length(); i++) |
| 98 | + { |
| 99 | + if(Character.isDigit(postfix.charAt(i))) |
| 100 | + st.push(Character.getNumericValue(postfix.charAt(i))); |
| 101 | + else |
| 102 | + { |
| 103 | + x=st.pop(); |
| 104 | + y=st.pop(); |
| 105 | + switch(postfix.charAt(i)) |
| 106 | + { |
| 107 | + case '+': |
| 108 | + st.push(y+x); break; |
| 109 | + case '-': |
| 110 | + st.push(y-x); break; |
| 111 | + case '*': |
| 112 | + st.push(y*x); break; |
| 113 | + case '/': |
| 114 | + st.push(y/x); break; |
| 115 | + case '%': |
| 116 | + st.push(y%x); break; |
| 117 | + case '^': |
| 118 | + st.push(power(y,x)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return st.pop(); |
| 123 | + } |
| 124 | + |
| 125 | + public static int power(int b,int a) |
| 126 | + { |
| 127 | + int i,x=1; |
| 128 | + for(i=1; i<=a; i++) |
| 129 | + x=x*b; |
| 130 | + return x; |
| 131 | + } |
| 132 | +} |
| 133 | + |
0 commit comments