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)); //計算 var cal = new Calc(); tempNum = cal.Decision(t); if (int.Parse(tempNum) < 0) { input = input.Remove(start-1,1); } //カッコ部分を置き換え input = input.Replace("(" + t + ")", tempNum); } else { break; } } //カッコなしで最後の計算 var cal2 = new Calc(); Console.WriteLine(cal2.Decision(input)); Console.ReadKey(); } } class Calc { private List tempNum = new List(); private List ope = new List(); public string Decision(string str) { foreach (var st in str) { if (st == '+' || st == '-') { ope.Add(st); } else { tempNum.Add(int.Parse(st.ToString())); } } 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(); } } }