public int calculate(String s) {
        Stack<Integer> stack = new Stack<Integer>();
        int num = 0, result = 0, sign = 1;
        for (int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (c == ' ')
                continue;
            else if (Character.isDigit(c)) {
                num = num * 10 + Character.getNumericValue(c);
            } else if (c == '+') {
                result = result + sign * num;
                num = 0;
                sign = 1;
            } else if (c == '-') {
                result = result + sign * num;
                sign = -1;
                num = 0;
            } else if (c == '(') {
                stack.push(result);
                stack.push(sign);
                result = 0;
                sign = 1;
            } else if (c == ')') {
                result = result + sign * num;
                result *= stack.pop();
                result += stack.pop();
                num = 0;
            }
        }
        if (num != 0)
            result += sign * num;
        return result;
    }