private void button1_Click(object sender, EventArgs e) { int combinedSum = 0; for (char letter = 'A'; letter <= 'D'; letter++) { int sum = 0; var stack = new Stack<string>(); int stackSum = 0; foreach (string line in textBox1.Lines) { string temp = line.Replace(".", "").Replace(" ", ""); // remove dots and spaces if (temp == "") continue; // ignore blank line bool containsLetter = (line.IndexOf(letter) > -1); char first = temp[0]; char last = temp[temp.Length - 1]; if (Char.IsDigit(first)) { string numStr = temp.Split('-')[0]; if (!containsLetter) numStr = "0"; if (stack.Count == 0) { sum += int.Parse(numStr); } else { stack.Push(numStr); } if (Char.IsDigit(last)) { int multiplier = last - 48; char bracket = temp[temp.Length - 2]; int total = 0; char openChar = '\0'; string tempStr; if (bracket == '}') openChar = '{'; else if (bracket == ']') openChar = '['; else if (bracket == ')') openChar = '('; else if (bracket == '>') openChar = '<'; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum = (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } else { stack.Push(first.ToString()); int index = 1; while (true) { if (Char.IsDigit(temp[index])) break; stack.Push(temp[index].ToString()); index++; } string numStr2 = temp.Substring(index).Split('-')[0]; if (!containsLetter) numStr2 = "0"; stack.Push(numStr2); if (Char.IsDigit(last)) { int multiplier = last - 48; char bracket = temp[temp.Length - 2]; int total = 0; char openChar = '\0'; string tempStr; if (bracket == '}') openChar = '{'; else if (bracket == ']') openChar = '['; else if (bracket == ')') openChar = '('; else if (bracket == '>') openChar = '<'; do { tempStr = stack.Pop(); int num; if (int.TryParse(tempStr, out num)) total += num; } while (tempStr[0] != openChar); stackSum += (stackSum + total) * multiplier; if (stack.Count == 0) { sum += stackSum; stackSum = 0; } } } } switch (letter) { case 'A': textBox3.Text = String.Format("A = {0}", sum); break; case 'B': textBox4.Text = String.Format("B = {0}", sum); break; case 'C': textBox5.Text = String.Format("C = {0}", sum); break; case 'D': textBox6.Text = String.Format("D = {0}", sum); break; } combinedSum += sum; } textBox2.Text = String.Format("Sum = {0}", combinedSum); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)