| 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()) |
nothing calls this directly
no outgoing calls
no test coverage detected