Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Stack;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Vector;
- import java.io.PrintStream;
- import java.io.BufferedReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.StringTokenizer;
- import java.io.InputStream;
- /**
- * Built using CHelper plug-in
- * Actual solution is at the top
- * @author Abrackadabra
- */
- public class Main {
- public static void main(String[] args) {
- InputStream inputStream = System.in;
- OutputStream outputStream = System.out;
- Scanner in = new Scanner(inputStream);
- PrintWriter out = new PrintWriter(outputStream);
- Calculator solver = new Calculator();
- solver.solve(1, in, out);
- out.close();
- }
- }
- class Calculator {
- public void solve(int testNumber, Scanner in, PrintWriter out) {
- String s = in.readLine();
- s = s.replaceAll(" ", "");
- HashMap<String, Integer> operatorPriority = new HashMap<String, Integer>();
- operatorPriority.put("+", 1);
- operatorPriority.put("-", 1);
- operatorPriority.put("*", 2);
- operatorPriority.put("/", 2);
- operatorPriority.put("%", 2);
- operatorPriority.put("^", 3);
- ArrayList<String> reversePolishNotation = new ArrayList<String>();
- Stack<String> stack = new Stack<String>();
- String temp = "";
- for (int i = 0; i < s.length(); i++) {
- if (s.charAt(i) == '.' || (s.charAt(i) >= '0' && s.charAt(i) <= '9')) {
- temp += s.charAt(i);
- continue;
- } else if (temp.length() > 0) {
- try {
- Double.parseDouble(temp);
- } catch (Exception e) {
- System.err.print("Too bad.");
- return;
- }
- reversePolishNotation.add(temp);
- temp = "";
- }
- if (s.charAt(i) == '(') {
- stack.push(s.charAt(i) + "");
- continue;
- }
- if (s.charAt(i) == ')') {
- while (true) {
- if (stack.isEmpty()) {
- System.err.println("To err is human.");
- return;
- }
- String p = stack.pop();
- if (p.equals("("))
- break;
- reversePolishNotation.add(p);
- }
- continue;
- }
- if (operatorPriority.containsKey(s.charAt(i) + "")) {
- int priority = operatorPriority.get(s.charAt(i) + "");
- while (!stack.isEmpty()) {
- if (operatorPriority.containsKey(stack.peek()) && priority <= operatorPriority.get(stack.peek())) {
- reversePolishNotation.add(stack.pop());
- } else {
- break;
- }
- }
- stack.push(s.charAt(i) + "");
- continue;
- }
- System.err.println("WTF is that?");
- return;
- }
- if (temp.length() > 0) reversePolishNotation.add(temp);
- while (!stack.isEmpty())
- reversePolishNotation.add(stack.pop());
- ArrayList<String> t = new ArrayList<String>();
- int n = 0;
- for (String p : reversePolishNotation) {
- if (p.equals("(")) {
- System.err.print("Problems of the existential nature.");
- return;
- }
- if (operatorPriority.containsKey(p)) {
- if (n < 2 || operatorPriority.containsKey(t.get(n - 1)) || operatorPriority.containsKey(t.get(n - 2))) {
- System.err.print("You have some.");
- return;
- }
- double left = Double.parseDouble(t.get(n - 2));
- double right = Double.parseDouble(t.get(n - 1));
- t.remove(--n);t.remove(--n);
- double res = 0.0;
- if (p.equals("+")) res = left + right;
- if (p.equals("-")) res = left - right;
- if (p.equals("*")) res = left * right;
- if (p.equals("/")) res = left / right;
- if (p.equals("%")) res = left % right;
- if (p.equals("^")) res = Math.pow(left, right);
- t.add(Double.toString(res));
- n++;
- } else {
- t.add(p);
- n++;
- }
- }
- if (t.size() != 1) {
- System.err.println("Hey!");
- return;
- }
- out.println(t.get(0));
- }
- }
- class Scanner {
- BufferedReader br;
- StringTokenizer in;
- public Scanner(InputStream inputStream) {
- br = new BufferedReader(new InputStreamReader(inputStream));
- }
- public String readLine() {
- try {
- in = null; // riad legacy
- return br.readLine();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment