本文实例讲述了c#实现简单的计算器功能。分享给大家供大家参考,具体如下:
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
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;
using system.collections;
using system.threading;
using system.diagnostics;
namespace calculator
{
public partial class form1 : form
{
public form1()
{
initializecomponent();
}
private string _sign;
private double _first = 10241024;
private double _second = 10241024;
private string _number;
private void button1_click( object sender, eventargs e)
{
_number += "1" ;
this .textbox1.text += "1" ;
}
private void button2_click( object sender, eventargs e)
{
_number += "2" ;
this .textbox1.text += "2" ;
}
private void button3_click( object sender, eventargs e)
{
_number += "3" ;
this .textbox1.text += "3" ;
}
private void button4_click( object sender, eventargs e)
{
_number += "4" ;
this .textbox1.text += "4" ;
}
private void button8_click( object sender, eventargs e)
{
_number += "5" ;
this .textbox1.text += "5" ;
}
private void button7_click( object sender, eventargs e)
{
_number += "6" ;
this .textbox1.text += "6" ;
}
private void button6_click( object sender, eventargs e)
{
_number += "7" ;
this .textbox1.text += "7" ;
}
private void button5_click( object sender, eventargs e)
{
_number += "8" ;
this .textbox1.text += "8" ;
}
private void button9_click( object sender, eventargs e)
{
_number += "9" ;
this .textbox1.text += "9" ;
}
private void button10_click( object sender, eventargs e)
{
_number += "0" ;
this .textbox1.text += "0" ;
}
private void button16_click( object sender, eventargs e)
{
//设置加号计算数
setnumber(convert.todouble(_number));
if (_sign == null || _second == 10241024)
{
_sign = "+" ;
this .textbox1.text += "+" ;
return ;
}
if (_second != 10241024)
{
//开始进行计算
computer compro = new computer(_sign, _first, _second);
this .textbox1.text = compro.comt( out _first).tostring();
//验证除数为零的情况
if (_first == 10241024.10241023)
{
this .textbox1.text = "0" ;
messagebox.show( "除数不能为零!" );
_number = null ;
_first = 10241024;
_second = 10241024;
this .textbox1.text = null ;
_sign = null ;
this .textbox1.text = null ;
}
//开始初始化数据
_sign = "+" ;
this .textbox1.text += "+" ;
_second = 10241024;
_number = null ;
return ;
}
}
private void button15_click( object sender, eventargs e)
{
//设置减号计算数
setnumber(convert.todouble(_number));
if (_sign == null || _second == 10241024)
{
_sign = "-" ;
this .textbox1.text += "-" ;
return ;
}
if (_second != 10241024)
{
//开始进行计算
computer compro = new computer(_sign, _first, _second);
this .textbox1.text = compro.comt( out _first).tostring();
//验证除数为零的情况
if (_first == 10241024.10241023)
{
this .textbox1.text = "0" ;
messagebox.show( "除数不能为零!" );
_number = null ;
_first = 10241024;
_second = 10241024;
this .textbox1.text = null ;
_sign = null ;
this .textbox1.text = null ;
}
//开始初始化数据
_sign = "-" ;
this .textbox1.text += "-" ;
_second = 10241024;
_number = null ;
return ;
}
}
private void button14_click( object sender, eventargs e)
{
//设置乘号计算数
setnumber(convert.todouble(_number));
if (_sign == null || _second == 10241024)
{
_sign = "*" ;
this .textbox1.text += "*" ;
return ;
}
if (_second != 10241024)
{
//开始进行计算
computer compro = new computer(_sign, _first, _second);
this .textbox1.text = compro.comt( out _first).tostring();
//验证除数为零的情况
if (_first == 10241024.10241023)
{
this .textbox1.text = "0" ;
messagebox.show( "除数不能为零!" );
_number = null ;
_first = 10241024;
_second = 10241024;
this .textbox1.text = null ;
_sign = null ;
this .textbox1.text = null ;
}
//开始初始化数据
_sign = "*" ;
this .textbox1.text += "*" ;
_second = 10241024;
_number = null ;
return ;
}
}
/// <summary>
/// 设置除号计算数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_click( object sender, eventargs e)
{
setnumber(convert.todouble(_number));
if (_sign == null || _second == 10241024)
{
_sign = "/" ;
this .textbox1.text += "/" ;
return ;
}
if (_second != 10241024)
{
//开始进行计算
computer compro = new computer(_sign, _first, _second);
this .textbox1.text = compro.comt( out _first).tostring();
//验证除数为零的情况
if (_first == 10241024.10241023)
{
this .textbox1.text = "0" ;
this .textbox1.text = "除数不能为零!" ;
_number = null ;
_first = 10241024;
_second = 10241024;
this .textbox1.text = null ;
_sign = null ;
this .textbox1.text = null ;
}
//开始初始化数据
_sign = "/" ;
this .textbox1.text += "/" ;
_second = 10241024;
_number = null ;
return ;
}
}
/// <summary>
/// 清空输入框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_click( object sender, eventargs e)
{
_number = null ;
_first = 10241024;
_second = 10241024;
this .textbox1.text = null ;
_sign = null ;
this .textbox1.text = null ;
}
private void button13_click( object sender, eventargs e)
{
setnumber(convert.todouble(_number));
//判断是否设置了_second的值
if (_second == 10241024)
{
_second = convert.todouble(_number);
_number = null ;
}
//判断是否设置了_first的值
if (_first == 10241024)
{
_first = convert.todouble(_number);
_number = null ;
}
//判断是否有_sign的值
if (_sign == null )
{
_sign = "+" ;
}
//开始进行计算
computer compro = new computer(_sign, _first, _second);
this .textbox1.text = compro.comt( out _first).tostring();
//验证除数为零的情况
if (_first == 10241024.10241023)
{
this .textbox1.text = "0" ;
messagebox.show( "除数不能为零!" );
_number = null ;
_first = 10241024;
_second = 10241024;
this .textbox1.text = null ;
_sign = null ;
this .textbox1.text = null ;
}
//开始初始化数据
_sign = null ;
_second = 10241024;
_number = null ;
}
public class computer
{
private string _sign;
private double _first;
private double _second;
public computer( string str, double first, double second)
{
_sign = str;
_first = first;
_second = second;
}
//选择相应的运算符进行运算
public double comt( out double first)
{
if (_sign == "+" )
{
first = _first + _second;
return _first + _second;
}
else if (_sign == "-" )
{
first = _first - _second;
return _first - _second;
}
else if (_sign == "*" )
{
first = _first * _second;
return _first * _second;
}
else if (_sign == "/" )
{
if (_second == 0)
{
first = 10241024.10241023;
return 10241024.10241023;
}
else
{
first = _first / _second;
return _first / _second;
}
}
else
{
first = 10241024.10241024;
return 10241024.10241024;
}
}
}
//设置数字函数
public void setnumber( double number)
{
if (_first == 10241024 && _number != null )
{
_first = convert.todouble(_number);
_number = null ;
}
else if (_second == 10241024 && _number != null )
{
_second = convert.todouble(_number);
_number = null ;
}
}
private void timer1_tick( object sender, eventargs e)
{
this .label2.text = datetime.now.tostring();
}
private void button24_click( object sender, eventargs e)
{
if ( this .textbox1.text == " " )
{
messagebox.show( "请输入数据" );
this .textbox1.text = convert.toint64(textbox1.text.tostring(), 2).tostring();
}
else
{
this .textbox1.text = convert.toint64(textbox1.text.tostring(), 2).tostring();
}
}
private void button22_click( object sender, eventargs e)
{
if ( this .textbox1.text == " " )
{
messagebox.show( "请输入数据" );
int64 int64 = convert.toint64(textbox1.text.tostring(), 2);
this .textbox1.text = convert.tostring(int64,16);
}
else
{
int64 int64 = convert.toint64(textbox1.text.tostring(), 2);
this .textbox1.text = convert.tostring(int64, 16);
}
}
private void button23_click( object sender, eventargs e)
{
if ( this .textbox1.text == " " )
{
messagebox.show( "请输入数据" );
int64 int64 = convert.toint64(textbox1.text.tostring(), 2);
this .textbox1.text = convert.tostring(int64, 8);
}
else
{
int64 int64 = convert.toint64(textbox1.text.tostring(), 2);
this .textbox1.text = convert.tostring(int64, 8);
}
}
private void button21_click( object sender, eventargs e)
{
if ( this .textbox1.text == " " )
{
messagebox.show( "请输入数据" );
int64 int64 = convert.toint64(textbox1.text);
this .textbox1.text = convert.tostring(int64, 2);
}
else
{
int64 int64 = convert.toint64(textbox1.text);
this .textbox1.text = convert.tostring(int64, 2);
}
}
private void button27_click( object sender, eventargs e)
{
double buffer1= convert.todouble(textbox1.text);
buffer1 = 1 / buffer1;
this .textbox1.text = buffer1.tostring();
}
private void button17_click( object sender, eventargs e)
{
_number += "." ;
this .textbox1.text += "." ;
}
private void button25_click( object sender, eventargs e)
{
double buffer1 = convert.todouble(textbox1.text);
buffer1 = buffer1 * buffer1;
this .textbox1.text = buffer1.tostring();
}
private void button26_click( object sender, eventargs e)
{
if ( this .textbox1.text == " " )
{
messagebox.show( "请输入数据" );
int64 int64 = convert.toint64( this .textbox1.text);
int64 temp1 = 1;
while (int64 > 1)
{
temp1 = temp1*int64 ;
int64--;
}
this .textbox1.text = temp1.tostring();
}
else
{
int64 int64 = convert.toint64( this .textbox1.text);
int64 temp1 = 1;
while (int64 > 1)
{
temp1 = temp1 * int64;
int64--;
}
this .textbox1.text = temp1.tostring();
}
}
private void button28_click( object sender, eventargs e)
{
//int64 int64 = convert.toint64(this.textbox1.text);
//double temp=math.log10(int64);
//this.textbox1.text = temp.tostring();
double temp1, temp2;
string [] strarry = new string [2];
strarry = this .textbox1.text.split( '.' );
temp1 = convert.todouble(strarry[0]);
temp2 = convert.todouble(strarry[1]);
// double temp1 = convert.todouble(this.textbox1.text);
temp2 = math.log(temp2,temp1 );
this .textbox1.text = temp2.tostring();
}
private void button18_click( object sender, eventargs e)
{
double temp1 = convert.todouble( this .textbox1.text);
double temp2 = math.sin(temp1);
this .textbox1.text = temp2.tostring();
}
private void button19_click( object sender, eventargs e)
{
double temp1 = convert.todouble( this .textbox1.text);
double temp2 = math.cos(temp1);
this .textbox1.text = temp2.tostring();
}
private void button20_click( object sender, eventargs e)
{
double temp1 = convert.todouble( this .textbox1.text);
double temp2 = math.tan(temp1);
this .textbox1.text = temp2.tostring();
}
private void button33_click( object sender, eventargs e)
{
double temp1, temp2;
string [] strarry= new string [2];
strarry= this .textbox1.text.split( '.' );
temp1 = convert.todouble(strarry[0]);
temp2 = convert.todouble(strarry[1]);
// double temp1 = convert.todouble(this.textbox1.text);
temp2 = math.pow(temp1,temp2);
this .textbox1.text = temp2.tostring();
}
/// <summary>
/// 计算e的temp1次幂
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button31_click( object sender, eventargs e)
{
double temp1 = convert.todouble( this .textbox1.text);
double temp2 = math.exp(temp1);
this .textbox1.text = temp2.tostring();
}
private void button34_click( object sender, eventargs e)
{
double temp1 = convert.todouble( this .textbox1.text);
double temp2 = math.sqrt(temp1);
this .textbox1.text = temp2.tostring();
}
private void button32_click( object sender, eventargs e)
{
double temp1, temp2;
string [] strarry = new string [2];
strarry = this .textbox1.text.split( '.' );
temp1 = convert.todouble(strarry[0]);
temp2 = convert.todouble(strarry[1]);
// double temp1 = convert.todouble(this.textbox1.text);
temp2 = math.ieeeremainder(temp1, temp2);
this .textbox1.text = temp2.tostring();
}
/// <summary>
/// 圆的面积
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button30_click( object sender, eventargs e)
{
double buffer1 = convert.todouble(textbox1.text);
buffer1 =3.1415926*buffer1*buffer1;
this .textbox1.text = buffer1.tostring();
}
/// <summary>
/// 三角形面积
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button29_click( object sender, eventargs e)
{
double temp1, temp2,temp3,p,area;
string [] strarry = new string [3];
strarry = this .textbox1.text.split( '.' );
temp1 = convert.todouble(strarry[0]);
temp2 = convert.todouble(strarry[1]);
temp3 = convert.todouble(strarry[2]);
// double temp1 = convert.todouble(this.textbox1.text);
p = (temp1 + temp2 + temp3) / 2;
area= p * (p - temp1) * (p - temp2) * (p - temp3);
area = math.sqrt(area);
this .textbox1.text = area.tostring();
}
private void linklabel1_linkclicked( object sender, linklabellinkclickedeventargs e)
{
string str = e.link.tostring();
process.start(str);
}
private void form1_load( object sender, eventargs e)
{
}
}
}
|
运行效果:
希望本文所述对大家c#程序设计有所帮助。
原文链接:http://blog.csdn.net/microzone/article/details/16904057