Advertisement
Guest User

infixevaluation

a guest
Nov 20th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class CSE220Assignment04 {
  5.     public static void main(String[] args) {
  6.         String exp=new Scanner(System.in).next();
  7.         String postFixExp = postFixBuilder(exp);
  8.         System.out.println("Post Fix Expression: "+postFixExp);
  9.         int result = postFixEvaluator(add(postFixExp));
  10.         System.out.println("Answer: "+result);
  11.     }
  12.  
  13.     private static String add(String postFixExp) {
  14.         String s ="";
  15.         for (int i=0; i<postFixExp.length(); i++){
  16.             s+=postFixExp.charAt(i)+" ";
  17.         }
  18.         return s;
  19.     }
  20.     public static String postFixBuilder(String exp){
  21.         String result = "";
  22.  
  23.         Stack<Character> stack = new Stack<>();
  24.  
  25.         for (int i = 0; i<exp.length(); ++i)
  26.         {
  27.             char c = exp.charAt(i);
  28.  
  29.             if (Character.isLetterOrDigit(c))
  30.                 result += c;
  31.  
  32.             else if (c == '(')
  33.                 stack.push(c);
  34.  
  35.             else if (c == ')')
  36.             {
  37.                 while (!stack.isEmpty() && stack.peek() != '(')
  38.                     result += stack.pop();
  39.  
  40.                 if (!stack.isEmpty() && stack.peek() != '(')
  41.                     return "Invalid Expression";
  42.                 else
  43.                     stack.pop();
  44.             }
  45.             else // an operator is encountered
  46.             {
  47.                 while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek()))
  48.                     result += stack.pop();
  49.                 stack.push(c);
  50.             }
  51.  
  52.         }
  53.         while (!stack.isEmpty())
  54.             result += stack.pop();
  55.  
  56.         return result;
  57.     }
  58.     static int Prec(char ch)
  59.     {
  60.         switch (ch)
  61.         {
  62.             case '+':
  63.             case '-':
  64.                 return 1;
  65.  
  66.             case '*':
  67.             case '/':
  68.                 return 2;
  69.  
  70.             case '^':
  71.                 return 3;
  72.         }
  73.         return -1;
  74.     }
  75.     public static int postFixEvaluator(String postfixExpr) {
  76.         Stack<Integer> s = new Stack<Integer>();
  77.         String[] items   = postfixExpr.split(" ");
  78.  
  79.         for (String item : items) {
  80.             try {
  81.                 s.push(Integer.valueOf(item));
  82.             } catch (NumberFormatException e) {
  83.                 Integer value1 = s.pop();
  84.                 Integer value2 = s.pop();
  85.  
  86.                 switch (item) {
  87.                     case "+":
  88.                         s.push(value2 + value1);
  89.                         break;
  90.                     case "-":
  91.                         s.push(value2 - value1);
  92.                         break;
  93.                     case "*":
  94.                         s.push(value2 * value1);
  95.                         break;
  96.                     case "/":
  97.                         s.push(value2 / value1);
  98.                         break;
  99.                 }
  100.             }
  101.         }
  102.  
  103.         return s.pop();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement