Basic Calculator

 avatar
unknown
java
a year ago
3.0 kB
8
Indexable
class Solution {
    public int calculate(String s) {
        return calculate(toPostfix(s));
    }
    
    private List<Node> toPostfix(String s) {
        List<Node> result = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '*' || c == '/' || c == '-' || c == '+') {
                processOperand(stack, String.valueOf(c), result);
            } else if (isNumber(c)) {
                Integer[] tokens = readNumber(s, i);
                i = tokens[1];
                int num = tokens[0];
                result.add(new Node(num));
            }
        }
        while (!stack.isEmpty()) result.add(new Node(stack.pop()));
        return result;
    }
    
    private void processOperand(Stack<String> stack, String operator, List<Node> result) {
        while (!stack.isEmpty()) {
            String topOperator = stack.peek();
            if (getPriority(topOperator) < getPriority(operator)) {
                break;
            } else {
                result.add(new Node(stack.pop()));
            }
        }
        stack.push(operator);
    }
    
    private int getPriority(String operator) {
        if (operator.equals("+") || operator.equals("-")) return 1;
        return 2;
    }
    
    private Integer[] readNumber (String s, int start) {
        int i = start;
        StringBuilder sb = new StringBuilder();
        while (i < s.length() && isNumber(s.charAt(i))) {
            sb.append(s.charAt(i++));
        }
        return new Integer[] {Integer.parseInt(sb.toString()), i - 1};
    }
    
    private boolean isNumber(char c) {
        return c >= '0' && c <= '9';
    }
    
    private int calculate(List<Node> list) {
        Stack<Integer> stack = new Stack<>();
        for (Node node : list) {
            if (node.isNumber) {
                stack.push(node.value);
            } else if (node.operator.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if (node.operator.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            } else if (node.operator.equals("-")) {
                int second = stack.pop();
                int first = stack.pop();
                stack.push(first - second);
            } else if (node.operator.equals("/")) {
                int second = stack.pop();
                int first = stack.pop();
                stack.push(first / second);
            }
        }
        return stack.pop();
    }
    
    static class Node {
        boolean isNumber;
        String operator;
        int value;
        
        public Node (String operator) {
            this.operator = operator;
            this.isNumber = false;
        }
        
        public Node (int value) {
            this.value = value;
            this.isNumber = true;
        }
    }
}
Editor is loading...
Leave a Comment