private string biaodashi="";//存放表达式 private string preyunxuanfu="";//存放上一步的运算符 private double sum = 0; private bool isok = false;//用于判断刚才点过的是数字还是运算符,初始化的时候是false private void btn1_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (!isok||txtshuru.Text=="0")//刚刚初始化或者刚刚点过运算符 { txtshuru.Text = btn.Text; } else//刚刚点过数字或者小数点 { txtshuru.Text += btn.Text; } isok = true; } private void btnjia_Click(object sender, EventArgs e) { Button btn = (Button)sender; if (isok)//刚刚初始化或者刚刚点过运算符 { if (biaodashi == "" || preyunxuanfu == "")//刚刚初始化 { sum = double.Parse(txtshuru.Text); biaodashi = txtshuru.Text; } else { if (preyunxuanfu == "+") { sum = sum + double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "-") { sum = sum - double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "*") { sum = sum * double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "/") { sum = sum / double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "%") { sum = sum % double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } } } preyunxuanfu = btn.Text;//将刚刚点过的运算符存到preyunsuanfu 里面 txtbiaodashi.Text = biaodashi + btn.Text; txtshuru.Text = sum.ToString(); isok = false; } private void button12_Click(object sender, EventArgs e) { if (preyunxuanfu == "+") { sum = sum + double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "-") { sum = sum - double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "*") { sum = sum * double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "/") { sum = sum / double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } if (preyunxuanfu == "%") { sum = sum % double.Parse(txtshuru.Text); biaodashi = biaodashi + preyunxuanfu + txtshuru.Text; } txtbiaodashi.Text = ""; txtshuru.Text = sum.ToString(); preyunxuanfu = ""; isok = false; biaodashi = ""; } private void button15_Click(object sender, EventArgs e) { txtbiaodashi.Text = ""; txtshuru.Text = "0"; preyunxuanfu = ""; isok = false; biaodashi = ""; } private void btnback_Click(object sender, EventArgs e) { if (txtshuru.Text.Length > 1) { txtshuru.Text = txtshuru.Text.Substring(0, txtshuru.Text.Length - 1); } else { txtshuru.Text = "0"; } } private void btndian_Click(object sender, EventArgs e) { if (isok)//刚刚点过数字 { if (!txtshuru.Text.Contains("."))//判断输入放入数字中是否有小数点 { txtshuru.Text = txtshuru.Text + "."; } } else//刚刚初始化、点过运算符、归零或者等号 { txtshuru.Text = "0."; } isok = true; }