1 bool a; //判断上一次按键是加减乘除还是数字
2 string c = ""; //判断加减乘除操作
3 decimal f;//记录第一个值以及接收运算结果
4 /// <summary>
5 /// 数字按键
6 /// </summary>
7 /// <param name="sender"></param>
8 /// <param name="e"></param>
9 private void button1_Click(object sender, EventArgs e)
10 {
11 Button b = sender as Button;
12 if (textBox2.Text == "0" || a == false)
13 {
14 textBox2.Text = b.Text;
15 }
16 else
17 {
18 textBox2.Text += b.Text;
19 }
20 if (textBox1.Text == "")
21 {
22 f = Convert.ToDecimal(textBox2.Text);
23 }
24 a = true;
25 }
26 /// <summary>
27 /// 加减乘除
28 /// </summary>
29 /// <param name="sender"></param>
30 /// <param name="e"></param>
31 private void button4_Click(object sender, EventArgs e)
32 {
33 Button b = sender as Button;
34 if (textBox2.Text.LastIndexOf(".") == (textBox2.Text.Length - 1))
35 {
36 textBox2.Text = textBox2.Text.Substring(0, (textBox2.Text.Length - 1));
37 }
38 if (a == false)
39 {
40 textBox1.Text = textBox1.Text.Substring(0, (textBox1.Text.Length - 1)) + b.Text;
41 }
42 else
43 {
44 textBox1.Text += textBox2.Text + b.Text;
45 if (c == "1")
46 {
47 f = f + Convert.ToDecimal(textBox2.Text);
48 }
49 else if (c == "2")
50 {
51 f = f - Convert.ToDecimal(textBox2.Text);
52 }
53 else if (c == "3")
54 {
55 f = f * Convert.ToDecimal(textBox2.Text);
56 }
57 else if (c == "4")
58 {
59 f = f / Convert.ToDecimal(textBox2.Text);
60 }
61 textBox2.Text = f.ToString();
62 }
63 if (b.Text == "+")
64 {
65 c = "1";
66 }
67 else if (b.Text == "-")
68 {
69 c = "2";
70 }
71 else if (b.Text == "*")
72 {
73 c = "3";
74 }
75 else if (b.Text == "/")
76 {
77 c = "4";
78 }
79 a = false;
80 }
81 /// <summary>
82 /// 点
83 /// </summary>
84 /// <param name="sender"></param>
85 /// <param name="e"></param>
86 private void button17_Click(object sender, EventArgs e)
87 {
88 if (textBox2.Text.Contains(".") == false)
89 {
90 textBox2.Text += ".";
91 }
92 }
93 /// <summary>
94 /// 等于
95 /// </summary>
96 /// <param name="sender"></param>
97 /// <param name="e"></param>
98 private void button15_Click(object sender, EventArgs e)
99 {
100 if (textBox2.Text.LastIndexOf(".") == (textBox2.Text.Length - 1))
101 {
102 textBox2.Text = textBox2.Text.Substring(0, (textBox2.Text.Length - 1));
103 }
104 if (c == "1")
105 {
106 f = f + Convert.ToDecimal(textBox2.Text);
107 }
108 else if (c == "2")
109 {
110 f = f - Convert.ToDecimal(textBox2.Text);
111 }
112 else if (c == "3")
113 {
114 f = f * Convert.ToDecimal(textBox2.Text);
115 }
116 else if (c == "4")
117 {
118 f = f / Convert.ToDecimal(textBox2.Text);
119 }
120 textBox1.Text = "";
121 textBox2.Text = f.ToString();
122 }
123 /// <summary>
124 /// 清空
125 /// </summary>
126 /// <param name="sender"></param>
127 /// <param name="e"></param>
128 private void button14_Click(object sender, EventArgs e)
129 {
130 textBox1.Text = "";
131 textBox2.Text = "0";
132 a = true;
133 c = "";
134 }
![复制代码](https://common.cnblogs.com/images/copycode.gif)
![](https://images2015.cnblogs.com/blog/943857/201606/943857-20160627224107109-1256468882.png)