package net.ipipip0129.yukicoder.no708; import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); // 演算用クラスのインスタンスを作成 Operation operation = new Operation(scan.nextLine()); System.out.println(operation.getNum()); } } class Operation { int num = 0; // 0=-,1=+ int prev_sign = 1; Operation(String formula){ for (int i = 0; i < formula.length(); i++) { String str = formula.substring(i, i+1); if (str.equalsIgnoreCase("-")) { prev_sign = 0; } else if (str.equalsIgnoreCase("+")) { prev_sign = 1; } else if (str.equalsIgnoreCase("(")){ // "("が出てきたら")"までの式を演算クラスで演算し答えを取得 int end_index = 0; for (int j = i; !formula.substring(j, j + 1).equalsIgnoreCase(")"); j++) { end_index = j; } end_index++; Operation operation = new Operation(formula.substring(i + 1, end_index)); if (prev_sign == 0 ) { num -= operation.num; } else { num += operation.num; } i = end_index; } else { if (prev_sign == 0) { num -= Integer.parseInt(str); } else { num += Integer.parseInt(str); } } } } public int getNum() { return num; } }