本文实例为大家分享了C#实现计算器精简版的具体实现代码,供大家参考,具体内容如下
计算器需求分析
一、界面设计
1.做一个显示屏
2.17个按钮(0-9,±×÷%=,CE)
二、需要实现的功能
1.输入第一个数字
2.选择运算类型
3.输入第二个数字
4.按下等号计算出结果,结果显示在显示屏上
三、实现步骤
1.先做界面
a.显示屏 textbox、listbox、label
b.使用17个button,button上的文本改成对应的数字符号
2.补充:申请两个int类型变量,第一个num1装第一个数字
第二个num2装第二个数字
(1).输入第一个数字,当点一个数字按钮,屏幕上显示一个,之前显示的数字在前面呆着
a1.添加按钮的cilck事件
a2.事件触发,将按钮代表的数字显示textbox1的text
(2).当输入符号的时候,清除屏幕,但是后台必须记录好第一个数字
b1.添加符号按钮的click事件
b2.当点任何一个符号按钮用一个变量num1装刚才输入的textbox1中的数字
(3).输入第二个数字
c1. 当点任何一个符号按钮用一个变量num2装刚才输入的textbox1中的数字
(4).按下等号按钮,显示屏上面的文本改变成两个数字的运算结果
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
|
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 简单的计算器制作 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load( object sender, EventArgs e) { //计算窗口加载居中的位置 int left = Screen.PrimaryScreen.Bounds.Width / 2 - this .Width / 2; int top = Screen.PrimaryScreen.Bounds.Height / 2 - this .Height / 2; this .Location = new Point(left,top); //加载的时候获取焦点 button1.TabIndex = 0; } //当我们输入完第一个数字之后 在输入运算符的时候 我们要记下第一个数字num1 //当我们输入完第二个数字之后 在输入等号的时候 我们要记下第二个数字num1 double num1 = 0; double num2 = 0; bool iskey = false ; //ce private void button1_Click( object sender, EventArgs e) { //设置清空 textBox1.Text = "" ; } //1 private void button4_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "1" ; } //2 private void button5_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "2" ; } //3 private void button6_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "3" ; } //4 private void button8_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "4" ; } //5 private void button9_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "5" ; } //6 private void button10_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "6" ; } //7 private void button12_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "7" ; } //8 private void button13_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "8" ; } //9 private void button14_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "9" ; } //0 private void button17_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "9" ; } //. private void button16_Click( object sender, EventArgs e) { if (iskey) { textBox1.Text = "" ; iskey = false ; } textBox1.Text += "." ; } //定义一个空的来接收符号 string type= " " ; //+ private void button15_Click( object sender, EventArgs e) { if (textBox1.Text != "" ) { //获取运算的第一个数字(前一个数字);将字符串类型转换为int类型(int.parse()) // num1 = int.Parse(textBox1.Text); // num1 = Convert.ToInt32(textBox1.Text); // 第二种转换方式convert num1 = Convert.ToDouble(textBox1.Text); } type = "+" ; // textBox1.Text = ""; iskey = true ; } //- private void button3_Click( object sender, EventArgs e) { if (textBox1.Text != "" ){ num1 = Convert.ToDouble(textBox1.Text); } type = "-" ; // textBox1.Text = ""; iskey = true ; } //* private void button7_Click( object sender, EventArgs e) { if (textBox1.Text != "" ) { num1 = Convert.ToDouble(textBox1.Text); } type = "*" ; // textBox1.Text = ""; iskey = true ; } //÷ private void button11_Click( object sender, EventArgs e) { if (textBox1.Text != "" ) { num1 = Convert.ToDouble(textBox1.Text); } type = "/" ; //textBox1.Text = ""; iskey = true ; } //% private void button18_Click( object sender, EventArgs e) { iskey = true ; if (textBox1.Text != "" ) { num1 = Convert.ToDouble(textBox1.Text); } type = "%" ; //textBox1.Text = ""; } //= private void button2_Click( object sender, EventArgs e) { if (iskey) { return ; } iskey = true ; if (textBox1.Text != "" ) { num2 = Convert.ToDouble(textBox1.Text); } switch (type) { case "+" : //括号里进行计算,计算的结果转化为string类型,并显示在屏幕(textbox1)里; textBox1.Text = (num1 + num2).ToString(); break ; case "-" : textBox1.Text = (num1 - num2).ToString(); break ; case "*" : textBox1.Text = (num1 * num2).ToString(); break ; case "/" : textBox1.Text = (num1 / num2).ToString(); break ; case "%" : textBox1.Text = (num1 % num2).ToString(); break ; } } } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_44870681/article/details/90760613