using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace HaoltCalculator ... { public partial class _MainFrm : Form ...{ private double nPrevValue = 0;//上一次值 private bool bAppend = true;//用户按的数字是否连接在显示的数字后面 private string strPrevOpar = "";//上一次的操作符号 public _MainFrm() ...{ InitializeComponent(); } private void NumButton_Click(object sender, EventArgs e) ...{ if (bAppend)...{//在追加状态 Button btn = sender as Button; if (btn.Text=="0")...{ if (textBoxResult.Text == "") ...{ textBoxResult.Text += "0"; } else if(textBoxResult.Text == "0")...{ ; } } else...{ if (textBoxResult.Text=="0")...{ textBoxResult.Text = ""; } textBoxResult.Text += btn.Text; } }else...{ textBoxResult.Text = "";//清空 Button btn = sender as Button; //if (btn.Text!="0"){ textBoxResult.Text += btn.Text; //} bAppend = true; } } private void OperButton_Click(object sender, EventArgs e) ...{ Button btn = sender as Button; if (btn.Text!="="&&btn.Text!="c")...{ strPrevOpar = btn.Text; try...{ nPrevValue = Convert.ToDouble(textBoxResult.Text); } catch (System.Exception ex) ...{ nPrevValue = 0; } bAppend = false; }else if (btn.Text=="c")...{ nPrevValue = 0; bAppend = true; strPrevOpar = ""; textBoxResult.Text = ""; }else...{ double current = 0; try...{ current = Convert.ToDouble(textBoxResult.Text); } catch (System.Exception ex)...{ current = 0; } double result; switch (strPrevOpar) ...{ case "+": result = current + nPrevValue; break; case "-": result = current + nPrevValue; break; case "*": result = current * nPrevValue; break; case "/": result = nPrevValue/current; break; default: result = current; break; } textBoxResult.Text = result.ToString(); nPrevValue = 0; bAppend = false; strPrevOpar = ""; } } }}