using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //入力 var input = Console.ReadLine(); string tempSt = input; string tempNum = ""; while (true) { //文字列中に()が有ったらそこを先に計算する var start = input.IndexOf("("); var end = input.IndexOf(")"); if (start > -1) { //カッコ内を抽出 var t = input.Substring(start + 1, ((end - 1) - start)); //計算 tempNum = Calc.Decision(t); //カッコ部分を置き換え input = input.Replace("(" + t + ")", tempNum); input = Calc.NewMethod(input); } else { break; } } //カッコなしで最後の計算 input = Calc.NewMethod(input); Console.WriteLine(Calc.Decision(input)); Console.ReadKey(); } } class Calc { public static string Decision(string str) { var ope = new List(); var opeSt = new[] {'+','-'}; var tempNum = Array.ConvertAll(str.Split(opeSt), s => int.Parse(s)).ToList(); foreach (var st in str) { if (st == '+' || st == '-') ope.Add(st); } foreach (var op in ope) { if (op == '+') { tempNum[0] = tempNum[0] + tempNum[1]; } else if (op == '-') { var answer = tempNum[0] - tempNum[1]; tempNum[0] = answer; } tempNum.RemoveAt(1); } return tempNum[0].ToString(); } public static string NewMethod(string input) { input = input.Replace("++", "+"); input = input.Replace("--", "+"); input = input.Replace("+-", "-"); input = input.Replace("-+", "-"); return input; } } }