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 WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 标准型ToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("标准型"); } private void btnyi_Click(object sender, EventArgs e) { Button btn = (Button)sender; //如果是刚刚点过运算符或者上面输入框是0,那么就把输入框的值赋成刚刚输入的值 if (isok || txtshuru.Text == "0") { txtshuru.Text = btn.Text; } else//否则就累加 { txtshuru.Text += btn.Text; } isok = false; } private bool isok = true;//用来记录是否刚刚点过运算符 private double sum = 0;//存放上一步运算结果 private string preYunsuanfu = ""; //存放上一步运算符 private string biaodashi;//存放上面显示的表达式前半部分 //txtbiaodashi的内容实际上是由成员变量biaodashi与preyunsuanfu组合而成的 private void btnjia_Click(object sender, EventArgs e) { if (preYunsuanfu == "")//如果是第一次点运算符 { sum = double.Parse(txtshuru.Text); biaodashi = txtshuru.Text; } else//如果不是第一次点运算符 { //是否刚刚点过运算符 if (!isok)//如果不是刚刚点过运算符,那么进去运算 { double shuru = double.Parse(txtshuru.Text); //算结果的话,要根据上一步运算符的符号来算 if (preYunsuanfu == "+") { sum = sum + shuru; } if (preYunsuanfu == "-") { sum -= shuru; } if (preYunsuanfu == "*") { sum *= shuru; } if (preYunsuanfu == "/") { sum /= shuru; } if (preYunsuanfu == "%") { sum %= shuru; } //表达式变成了原来表达式+上一步运算符+刚点的输入数字 biaodashi = biaodashi + preYunsuanfu + txtshuru.Text; } } txtshuru.Text = sum.ToString();//显示运算结果 preYunsuanfu = ((Button)sender).Text;//上一步运算符变成刚点的运算符 txtbiaodashi.Text = biaodashi + preYunsuanfu;//显示式就变成表达式加当点的运算符 isok = true;//点完运算符把isok设置为true } private void btnclear_Click(object sender, EventArgs e) { txtshuru.Text = "0"; isok = true; biaodashi = ""; sum = 0; preYunsuanfu = ""; txtbiaodashi.Text = ""; } private void btndian_Click(object sender, EventArgs e) { if (!txtshuru.Text.Contains(".")) { txtshuru.Text += "."; isok = false; } } private void btndeng_Click(object sender, EventArgs e) { if (preYunsuanfu == "")//如果是第一次点运算符 { sum = double.Parse(txtshuru.Text); biaodashi = txtshuru.Text; } else//如果不是第一次点运算符 { //是否刚刚点过运算符 if (!isok)//如果不是刚刚点过运算符,那么进去运算 { double shuru = double.Parse(txtshuru.Text); //算结果的话,要根据上一步运算符的符号来算 if (preYunsuanfu == "+") { sum = sum + shuru; } if (preYunsuanfu == "-") { sum -= shuru; } if (preYunsuanfu == "*") { sum *= shuru; } if (preYunsuanfu == "/") { sum /= shuru; } if (preYunsuanfu == "%") { sum %= shuru; } //表达式变成了原来表达式+上一步运算符+刚点的输入数字 biaodashi = biaodashi + preYunsuanfu + txtshuru.Text; } } txtshuru.Text = sum.ToString();//显示运算结果 preYunsuanfu = ((Button)sender).Text;//上一步运算符变成刚点的运算符 if (preYunsuanfu == "=") { isok = true; biaodashi = ""; sum = 0; preYunsuanfu = ""; txtbiaodashi.Text = ""; } } } }