public CalculateTree(String exp) { while (true) { int pos = lastStepPosition(exp); if (pos != -1) { value = exp[pos].ToString(); String expLeft = exp.Substring(0, pos); String expRight = exp.Substring(pos + 1, exp.Length - pos - 1); if (expLeft.Length == 0) { leftNode = null; if ((value == "*") || (value == "/")) { throw new Exception("Sign Error"); } } else { leftNode = new CalculateTree(expLeft); } if (expRight.Length == 0) { throw new Exception("Sign Error"); } else { rightNode = new CalculateTree(expRight); } break; } else { if ((exp.Length > 0) && (exp[0] == '(') && (exp[exp.Length - 1] == ')')) { exp = exp.Remove(exp.Length - 1, 1).Remove(0, 1); } else { value = exp; break; } } } }
private void Button_Click(object sender, EventArgs e) { if (update) { taskTextBox.Text = ""; resultTextBox.Text = ""; update = false; } switch (((Button)sender).Name) { case "clearButton": taskTextBox.Text = ""; resultTextBox.Text = ""; break; case "deleteButton": if (taskTextBox.Text.Length > 0) { taskTextBox.Text = taskTextBox.Text.Remove(taskTextBox.Text.Length - 1); } break; case "buttonNumber0": taskTextBox.Text += "0"; break; case "buttonNumber1": taskTextBox.Text += "1"; break; case "buttonNumber2": taskTextBox.Text += "2"; break; case "buttonNumber3": taskTextBox.Text += "3"; break; case "buttonNumber4": taskTextBox.Text += "4"; break; case "buttonNumber5": taskTextBox.Text += "5"; break; case "buttonNumber6": taskTextBox.Text += "6"; break; case "buttonNumber7": taskTextBox.Text += "7"; break; case "buttonNumber8": taskTextBox.Text += "8"; break; case "buttonNumber9": taskTextBox.Text += "9"; break; case "buttonNumberA": taskTextBox.Text += "A"; break; case "leftBracketButton": taskTextBox.Text += "("; break; case "rightBracketButton": taskTextBox.Text += ")"; break; case "multiplicationButton": taskTextBox.Text += "*"; break; case "divisionButton": taskTextBox.Text += "/"; break; case "additionButton": taskTextBox.Text += "+"; break; case "subtractionButton": taskTextBox.Text += "-"; break; case "dotButton": taskTextBox.Text += "."; break; case "resultButton": try { CalculateTree calc = new CalculateTree(taskTextBox.Text); calc.calculate(); resultTextBox.Text = calc.getResult(); } catch (Exception exep) { resultTextBox.Text = exep.Message; } update = true; break; } }