Filip_Markoski

First Midterm.8 Mathematical Expression

Oct 30th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.PriorityQueue;
  5. import java.util.Stack;
  6.  
  7. public class ExpressionEvaluator {
  8.  
  9.     public static int evaluate(int a, char sign, int b) {
  10.         if (sign == '+') return a + b;
  11.         else if (sign == '*') return a * b;
  12.         return 0;
  13.     }
  14.  
  15.     public static int evaluateExpression(String expression) {
  16.         char symbols[] = expression.toCharArray();
  17.  
  18.         Stack<Character> charStack = new Stack<>();
  19.         Stack<Integer> intStack = new Stack<>();
  20.  
  21.         for (int i = 0; i < symbols.length; i++) {
  22.             if (symbols[i] == '+' || symbols[i] == '*') {
  23.                 charStack.push(symbols[i]);
  24.             } else {
  25.                 intStack.push(symbols[i] - '0');
  26.             }
  27.         }
  28.         /*System.out.println(intStack.toString());
  29.         System.out.println(charStack.toString());
  30.  
  31.         System.out.println();*/
  32.  
  33.         charStack = new Stack<>();
  34.         intStack = new Stack<>();
  35.         StringBuilder sb = new StringBuilder();
  36.         for (int i=0 ; i<expression.length(); ++i) {
  37.  
  38.             char curr = expression.charAt(i);
  39.             if ( curr == '+' || curr =='*') {
  40.                 intStack.push(Integer.parseInt(sb.toString()));
  41.                 sb = new StringBuilder();
  42.                 charStack.push(curr);
  43.             }
  44.  
  45.             else {
  46.                 sb.append(curr);
  47.             }
  48.  
  49.         }
  50.         intStack.push(Integer.parseInt(sb.toString()));
  51.  
  52.         /*System.out.println(intStack.toString());
  53.         System.out.println(charStack.toString());*/
  54.  
  55.         while (!charStack.isEmpty()) {
  56.             int a = intStack.pop();
  57.             int b = intStack.pop();
  58.             char sign = charStack.pop();
  59.  
  60.             if (sign == '*') {
  61.                 intStack.push(a * b);
  62.             } else {
  63.                 if (charStack.isEmpty() || charStack.peek() == '+') {
  64.                     intStack.push(a + b);
  65.                 } else {
  66.                     /* a & b want to add, but b must multiply first */
  67.                     int temp = intStack.pop(); /* get next int */
  68.                     charStack.pop(); /* another multiplication, just get rid of it */
  69.                     intStack.push(b * temp); /* second * third */
  70.                     intStack.push(a); /* add a on top after b is done */
  71.                     charStack.push(sign); /* preserve the + we got at the start */
  72.                 }
  73.             }
  74.         }
  75.         return intStack.pop();
  76.     }
  77.  
  78.     public static void main(String[] args) throws IOException {
  79.         BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  80.         System.out.println(evaluateExpression(input.readLine()));
  81.     }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment