using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { string inpt = Reader.ReadLine(); string[] splt = DivideKakko(inpt); for (int i = 0; i < splt.Length; i++) { if(splt[i].Contains('(')) { splt[i] = GetCalc(splt[i]).ToString(); } } int ans = GetCalc(string.Join("", splt)); Console.WriteLine(ans); } private int GetCalc(string inpt) { string src = inpt; if(inpt[0] == '(') { src = inpt.Substring(1, inpt.Length - 2); } int ans = 0; bool isAdd = true; for (int i = 0; i < src.Length; i++) { if(src[i] == '-') { isAdd = false; } else if(src[i] == '+') { isAdd = true; } else if(isAdd) { ans += src[i] - '0'; } else { ans -= src[i] - '0'; } } return ans; } private string[] DivideKakko(String inText) { List ret = new List(); if(!inText.Contains(')')) { ret.Add(inText); return ret.ToArray(); } int stIdx = 0; for (int i = 0; i < inText.Length; i++) { if(inText[i] == '(' && i>0) { ret.Add(inText.Substring(stIdx, i - stIdx)); stIdx = i; } else if(inText[i] == ')') { ret.Add(inText.Substring(stIdx, i - stIdx + 1)); stIdx = i + 1; } } if(stIdx< inText.Length) { ret.Add(inText.Substring(stIdx, inText.Length - stIdx)); } return ret.ToArray(); } public class Reader { static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 1-(2-1)+(2+2)-3 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }