实现效果:
Form1.cs代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Cal { public partial class Form1 : Form { public double num1; public int temp = 0; //记录存储计算方式 public Form1() { InitializeComponent(); } private void eq_Click( object sender, EventArgs e) { switch (temp) { //加法运算 case 1: try { result.Text = (num1 + double .Parse(result.Text)).ToString(); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { temp = 0; } break ; //减法运算 case 2: try { result.Text = (num1 - double .Parse(result.Text)).ToString(); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { temp = 0; } break ; //乘法运算 case 3: try { result.Text = (num1 * double .Parse(result.Text)).ToString(); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { temp = 0; } break ; //除法运算,讨论分母为零的情况 case 4: if ( double .Parse(result.Text)==0) { MessageBox.Show( "除数不能为零" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); result.ResetText(); } else { result.Text = (num1 / double .Parse(result.Text)).ToString(); temp = 0; } break ; case 5: try { result.Text = (num1 % double .Parse(result.Text)).ToString(); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { temp = 0; } break ; } } private void empty_Click( object sender, EventArgs e) { result.Text = "0" ; num1 = 0; temp = 0; //清除 //if (result.Text.Length > 0) //{ // result.Text = result.Text.Substring(0, result.Text.Length - 1); //} } private void Zero_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "0" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "0" ; } } private void one_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "1" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "1" ; } } private void two_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "2" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "2" ; } } private void three_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "3" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "3" ; } } private void four_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "4" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "4" ; } } private void five_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "5" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "5" ; } } private void six_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "6" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "6" ; } } private void seven_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "7" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "7" ; } } private void eight_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "8" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "8" ; } } private void nine_Click( object sender, EventArgs e) { if (result.Text == "0" ) { result.Text = "9" ; } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "9" ; } } private void point_Click( object sender, EventArgs e) { //小数点按键,初始为空,直接按 . 键则显示为0.多少 if (result.Text == "" ) { result.Text = "0." ; } //如果再次输入.则返回都输入的字符后边并提示信息 else if (result.Text.IndexOf( "." ) >= 0) { MessageBox.Show( "已经输入小数点,无须再次输入" , "提示" ); } else { //前边有数字时,则直接在后边加上. result.Text = result.Text + "." ; } } private void add_Click( object sender, EventArgs e) { temp = 1; try { num1 = double .Parse(result.Text); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { result.Text = "" ; //随后让文本框归空 } } private void mul_Click( object sender, EventArgs e) { temp = 3; try { num1 = double .Parse(result.Text); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { result.Text = "" ; //随后让文本框归空 } } private void sub_Click( object sender, EventArgs e) { temp = 2; try { num1 = double .Parse(result.Text); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { result.Text = "" ; //随后让文本框归空 } } private void Button1_Click( object sender, EventArgs e) { temp = 4; try { num1 = double .Parse(result.Text); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { result.Text = "" ; //随后让文本框归空 } } private void per_Click( object sender, EventArgs e) { temp = 5; try { num1 = double .Parse(result.Text); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { result.Text = "" ; //随后让文本框归空 } } private void opp_Click( object sender, EventArgs e) { //temp = 6; try { result.Text = (- double .Parse(result.Text)).ToString(); } catch (Exception) { MessageBox.Show( "还没输入数字呢" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { temp = 0; } } private void Form1_Load( object sender, EventArgs e) { result.Text = "0" ; } private void result_TextChanged( object sender, EventArgs e) { if (result.Text.Length > 18&& result.Text.Length<36) { result.Font = new Font(result.Font.FontFamily, 15, result.Font.Style); } if (result.Text.Length > 36&&result.Text.Length<45) { result.Font = new Font(result.Font.FontFamily, 10, result.Font.Style); } if (result.Text.Length > 45) { MessageBox.Show( "超出范围,将要清空了!" , "错误提示" , MessageBoxButtons.OK, MessageBoxIcon.Warning); result.ResetText(); result.Font = new Font(result.Font.FontFamily, 20, result.Font.Style); } } } } |
小编已经测试了代码,大家可以跟着做一下看看,感谢大家对服务器之家的支持。
原文链接:https://www.idaobin.com/archives/982.html