Guest User

Untitled

a guest
Dec 7th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.10 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStreamReader;
  3. import java.util.Stack;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.Vector;
  7. import java.io.PrintStream;
  8. import java.io.BufferedReader;
  9. import java.io.OutputStream;
  10. import java.io.PrintWriter;
  11. import java.util.StringTokenizer;
  12. import java.io.InputStream;
  13.  
  14. /**
  15.  * Built using CHelper plug-in
  16.  * Actual solution is at the top
  17.  * @author Abrackadabra
  18.  */
  19. public class Main {
  20.     public static void main(String[] args) {
  21.         InputStream inputStream = System.in;
  22.         OutputStream outputStream = System.out;
  23.         Scanner in = new Scanner(inputStream);
  24.         PrintWriter out = new PrintWriter(outputStream);
  25.         Calculator solver = new Calculator();
  26.         solver.solve(1, in, out);
  27.         out.close();
  28.     }
  29. }
  30.  
  31. class Calculator {
  32.     public void solve(int testNumber, Scanner in, PrintWriter out) {
  33.         String s = in.readLine();
  34.         s = s.replaceAll(" ", "");
  35.  
  36.         HashMap<String, Integer> operatorPriority = new HashMap<String, Integer>();
  37.         operatorPriority.put("+", 1);
  38.         operatorPriority.put("-", 1);
  39.         operatorPriority.put("*", 2);
  40.         operatorPriority.put("/", 2);
  41.         operatorPriority.put("%", 2);
  42.         operatorPriority.put("^", 3);
  43.  
  44.         ArrayList<String> reversePolishNotation = new ArrayList<String>();
  45.         Stack<String> stack = new Stack<String>();
  46.         String temp = "";
  47.         for (int i = 0; i < s.length(); i++) {
  48.             if (s.charAt(i) == '.' || (s.charAt(i) >= '0' && s.charAt(i) <= '9')) {
  49.                 temp += s.charAt(i);
  50.                 continue;
  51.             } else if (temp.length() > 0) {
  52.                 try {
  53.                     Double.parseDouble(temp);
  54.                 } catch (Exception e) {
  55.                     System.err.print("Too bad.");
  56.                     return;
  57.                 }
  58.                 reversePolishNotation.add(temp);
  59.                 temp = "";
  60.             }
  61.             if (s.charAt(i) == '(') {
  62.                 stack.push(s.charAt(i) + "");
  63.                 continue;
  64.             }
  65.             if (s.charAt(i) == ')') {
  66.                 while (true) {
  67.                     if (stack.isEmpty()) {
  68.                         System.err.println("To err is human.");
  69.                         return;
  70.                     }
  71.                     String p = stack.pop();
  72.                     if (p.equals("("))
  73.                         break;
  74.                     reversePolishNotation.add(p);
  75.                 }
  76.                 continue;
  77.             }
  78.             if (operatorPriority.containsKey(s.charAt(i) + "")) {
  79.                 int priority = operatorPriority.get(s.charAt(i) + "");
  80.                 while (!stack.isEmpty()) {
  81.                     if (operatorPriority.containsKey(stack.peek()) && priority <= operatorPriority.get(stack.peek())) {
  82.                         reversePolishNotation.add(stack.pop());
  83.                     } else {
  84.                         break;
  85.                     }
  86.                 }
  87.                 stack.push(s.charAt(i) + "");
  88.                 continue;
  89.             }
  90.             System.err.println("WTF is that?");
  91.             return;
  92.         }
  93.         if (temp.length() > 0) reversePolishNotation.add(temp);
  94.         while (!stack.isEmpty())
  95.             reversePolishNotation.add(stack.pop());
  96.  
  97.         ArrayList<String> t = new ArrayList<String>();
  98.         int n = 0;
  99.         for (String p : reversePolishNotation) {
  100.             if (p.equals("(")) {
  101.                 System.err.print("Problems of the existential nature.");
  102.                 return;
  103.             }
  104.             if (operatorPriority.containsKey(p)) {
  105.                 if (n < 2 || operatorPriority.containsKey(t.get(n - 1)) || operatorPriority.containsKey(t.get(n - 2))) {
  106.                     System.err.print("You have some.");
  107.                     return;
  108.                 }
  109.                 double left = Double.parseDouble(t.get(n - 2));
  110.                 double right = Double.parseDouble(t.get(n - 1));
  111.                 t.remove(--n);t.remove(--n);
  112.                 double res = 0.0;
  113.                 if (p.equals("+")) res = left + right;
  114.                 if (p.equals("-")) res = left - right;
  115.                 if (p.equals("*")) res = left * right;
  116.                 if (p.equals("/")) res = left / right;
  117.                 if (p.equals("%")) res = left % right;
  118.                 if (p.equals("^")) res = Math.pow(left, right);
  119.                 t.add(Double.toString(res));
  120.                 n++;
  121.             } else {
  122.                 t.add(p);
  123.                 n++;
  124.             }
  125.         }
  126.         if (t.size() != 1) {
  127.             System.err.println("Hey!");
  128.             return;
  129.         }
  130.         out.println(t.get(0));
  131.     }
  132. }
  133.  
  134. class Scanner {
  135.     BufferedReader br;
  136.     StringTokenizer in;
  137.  
  138.     public Scanner(InputStream inputStream) {
  139.         br = new BufferedReader(new InputStreamReader(inputStream));
  140.     }
  141.  
  142.     public String readLine() {
  143.         try {
  144.             in = null; // riad legacy
  145.             return br.readLine();
  146.         } catch (Exception e) {
  147.             e.printStackTrace();
  148.             return null;
  149.         }
  150.     }
  151.  
  152.     }
Add Comment
Please, Sign In to add comment