private void button1_Click(object sender, EventArgs e) { //this.label1.Text = this.textBox1.Text + comboBox1.SelectedItem + this.textBox2.Text; String str = comboBox1.GetItemText(comboBox1.SelectedItem); Operation oper = OperationFactory.createOperation(str); try { oper.num1 = Convert.ToDouble(this.textBox1.Text); oper.num2 = Convert.ToDouble(this.textBox2.Text); }catch (Exception) { throw; } double result = oper.getResult(); this.label1.Text = Convert.ToString("result =" + result); }
private void btnOperation_Click(object sender, EventArgs e) { Button btn = sender as Button; Operation oper; switch (btn.Name) { case "btnAdd": equalStatus = false; multiDeal(); calShow += "+"; txtResual.Text = calShow; setBtnEnableMethon(false); break; case "btnSub": equalStatus = false; multiDeal(); calShow += "-"; txtResual.Text = calShow; setBtnEnableMethon(false); break; case "btnMul": equalStatus = false; multiDeal(); calShow += "*"; txtResual.Text = calShow; setBtnEnableMethon(false); break; case "btnDiv": equalStatus = false; multiDeal(); calShow += "/"; txtResual.Text = calShow; setBtnEnableMethon(false); break; case "btnEqual": equalStatus = true; multiDeal(); break; case "btnPoint": if (equalStatus) //等于号按过之后点数字则直接清除原来的结果 { equalStatus = false; calShow = ""; resultDouble = 0.0; txtResual.Text = ""; } oper = OperationFactory.createOperation("."); calShow = oper.addPoint(calShow); txtResual.Text = calShow; setBtnEnableMethon(true); break; case "btnDel": if (calShow.Length != 0) { calShow = calShow.Substring(0, calShow.Length - 1); //删除最后一个字符 } txtResual.Text = calShow; setBtnEnableMethon(true); break; case "btnClear": calShow = ""; resultDouble = 0; txtResual.Text = ""; break; case "btnSquareRoot": multiDeal(); //先得出前面表达式的值 oper = OperationFactory.createOperation("SquareRoot"); resultDouble = oper.getResult(); calShow = resultDouble.ToString(); txtResual.Text = calShow; break; case "btnPosAndNeg": multiDeal(); //先计算结果,再来加正负号 oper = OperationFactory.createOperation("+/-"); calShow = oper.posAndNeg(calShow); txtResual.Text = calShow; break; default: break; } }
private void multiDeal() { try { //将前面的表达式的值计算出来 Regex reg = new Regex(@"(\+|\-)?(\d+)(\.\d+)?(\+|\-|\*|\/)(\+|\-)?(\d+)(\.\d+)?"); var res = reg.Match(calShow).Groups; //res[0] 整个匹配的表达式 //res[1] 第一个操作数的符号 //res[2] 第一个操作数的整数部分 //res[3] 第一个操作数的小数部分 //res[4] 操作符 //res[5] 第二个操作数的符号 //res[6] 第二个操作数的整数部分 //res[7] 第而个操作数的小数部分 Operation oper; switch (res[4].ToString()) { case "+": oper = OperationFactory.createOperation("+"); oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString()); oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString()); resultDouble = oper.getResult(); calShow = resultDouble.ToString(); break; case "-": oper = OperationFactory.createOperation("-"); oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString()); oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString()); resultDouble = oper.getResult(); calShow = resultDouble.ToString(); break; case "*": oper = OperationFactory.createOperation("*"); oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString()); oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString()); resultDouble = oper.getResult(); calShow = resultDouble.ToString(); break; case "/": oper = OperationFactory.createOperation("/"); oper.NumberA = Convert.ToDouble(res[1].ToString() + res[2].ToString() + res[3].ToString()); oper.NumberB = Convert.ToDouble(res[5].ToString() + res[6].ToString() + res[7].ToString()); if (oper.NumberB == 0) { throw new Exception("除数不能为0!"); } resultDouble = oper.getResult(); calShow = resultDouble.ToString(); break; default: break; } } catch (Exception ex) { MessageBox.Show(ex.Message, "警告"); } txtResual.Text = calShow; }