msdn library2005中计算器例子的逻辑实现
msdn中 [演练:Windows 窗体设计器入门]设计了一个计算器界面,我把计算器逻辑简单实现了一下。作为练手的第一个c# windows程序。
我把里面的TextBox控件换成Label,这样去掉鼠标点击时闪烁的光标。
DemoCalculator.cs如下,
Code
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Text;
7using System.Windows.Forms;
8
9namespace DemoCalculatorLib
10{
11 public enum Suanshu { add, sub, mul, div }
12 public partial class DemoCalculator : UserControl
13 {
14 private string sshow;
15 private decimal operand;
16 private decimal result;
17 private bool blDenghao;
18 private bool blYunsuan;
19 private Suanshu suan;
20 private string shizi;
21 public DemoCalculator()
22 {
23 InitializeComponent();
24 sshow = "0";
25 operand = 0m;
26 result = 0m;
27 blDenghao = false;
28 blYunsuan = false;
29 suan = Suanshu.add;
30 }
31
32 private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
33 {
34
35 }
36
37 private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
38 {
39
40 }
41
42 private void listView1_SelectedIndexChanged(object sender, EventArgs e)
43 {
44
45 }
46 // 1
47 private void button11_Click(object sender, EventArgs e)
48 {
49 if (!blDenghao)
50 {
51 if (sshow == "0")
52 {
53 sshow = "";
54 }
55 sshow += "1";
56 }
57 else
58 {
59 sshow = "1";
60 blDenghao = false;
61 }
62 label1.Text = sshow;
63 }
64 //2
65 private void button12_Click(object sender, EventArgs e)
66 {
67 if (!blDenghao)
68 {
69 if (sshow == "0")
70 {
71 sshow = "";
72 }
73 sshow += "2";
74 }
75 else
76 {
77 sshow = "2";
78 blDenghao = false;
79 }
80 label1.Text = sshow;
81 }
82 //3
83 private void button13_Click(object sender, EventArgs e)
84 {
85 if (!blDenghao)
86 {
87 if (sshow == "0")
88 {
89 sshow = "";
90 }
91 sshow += "3";
92 }
93 else
94 {
95 sshow = "3";
96 blDenghao = false;
97 }
98 label1.Text = sshow;
99 }
100 //4
101 private void button6_Click(object sender, EventArgs e)
102 {
103 if (!blDenghao)
104 {
105 if (sshow == "0")
106 {
107 sshow = "";
108 }
109 sshow += "4";
110 }
111 else
112 {
113 sshow = "4";
114 blDenghao = false;
115 }
116 label1.Text = sshow;
117 }
118 //5
119 private void button7_Click(object sender, EventArgs e)
120 {
121 if (!blDenghao)
122 {
123 if (sshow == "0")
124 {
125 sshow = "";
126 }
127 sshow += "5";
128 }
129 else
130 {
131 sshow = "5";
132 blDenghao = false;
133 }
134 label1.Text = sshow;
135 }
136 //6
137 private void button8_Click(object sender, EventArgs e)
138 {
139 if (!blDenghao)
140 {
141 if (sshow == "0")
142 {
143 sshow = "";
144 }
145 sshow += "6";
146 }
147 else
148 {
149 sshow = "6";
150 blDenghao = false;
151 }
152 label1.Text = sshow;
153 }
154 //7
155 private void button1_Click(object sender, EventArgs e)
156 {
157 if (!blDenghao)
158 {
159 if (sshow == "0")
160 {
161 sshow = "";
162 }
163 sshow += "7";
164 }
165 else
166 {
167 sshow = "7";
168 blDenghao = false;
169 }
170 label1.Text = sshow;
171 }
172 //8
173 private void button2_Click(object sender, EventArgs e)
174 {
175 if (!blDenghao)
176 {
177 if (sshow == "0")
178 {
179 sshow = "";
180 }
181 sshow += "8";
182 }
183 else
184 {
185 sshow = "8";
186 blDenghao = false;
187 }
188 label1.Text = sshow;
189 }
190 //9
191 private void button3_Click(object sender, EventArgs e)
192 {
193 if (!blDenghao)
194 {
195 if (sshow == "0")
196 {
197 sshow = "";
198 }
199 sshow += "9";
200 }
201 else
202 {
203 sshow = "9";
204 blDenghao = false;
205 }
206 label1.Text = sshow;
207 }
208 // 0
209 private void button16_Click(object sender, EventArgs e)
210 {
211 if (!blDenghao)
212 {
213 if (sshow == "0")
214 {
215 sshow = "";
216 }
217 sshow += "0";
218 }
219 else
220 {
221 sshow = "0";
222 blDenghao = false;
223 }
224 label1.Text = sshow;
225 }
226 // dot
227 private void button18_Click(object sender, EventArgs e)
228 {
229 if (!blDenghao)
230 {
231 if (sshow.IndexOf(".") == -1)
232 {
233 sshow += ".";
234 }
235 }
236 else
237 {
238 sshow = "0.";
239 blDenghao = false;
240 }
241 label1.Text = sshow;
242 }
243
244 //sign
245 private void button17_Click(object sender, EventArgs e)
246 {
247 if (sshow == "0")
248 {
249 return;
250 }
251 if (sshow[0] != '-')
252 {
253 sshow = "-" + sshow;
254 }
255 else
256 {
257 sshow = sshow.Substring(1);
258 }
259 label1.Text = sshow;
260 }
261
262 // op =
263 private void button15_Click(object sender, EventArgs e)
264 {
265 blDenghao = true;
266 if (sshow.IndexOf(".") != -1)
267 {
268 sshow = sshow.TrimEnd(new char[] { '0' });
269 sshow = sshow.TrimEnd(new char[] { '.' });
270 }
271 if (sshow == "-0")
272 {
273 sshow = "0";
274 }
275 if (!blYunsuan)
276 {
277 label1.Text = sshow;
278 result = decimal.Parse(sshow);
279 }
280 else
281 {
282 operand = decimal.Parse(sshow);
283 result = Op(result, operand, suan);
284 sshow = result.ToString();
285 label1.Text = sshow;
286 blYunsuan = false;
287 listView1.Items.Add(shizi, 0);
288 }
289 }
290
291 // op +
292 private void button19_Click(object sender, EventArgs e)
293 {
294 //op =
295 button15_Click(sender, e);
296 blYunsuan = true;
297 suan = Suanshu.add;
298
299 }
300
301 //op -
302 private void button14_Click(object sender, EventArgs e)
303 {
304 //op =
305 button15_Click(sender, e);
306 blYunsuan = true;
307 suan = Suanshu.sub;
308 }
309
310 //op *
311 private void button9_Click(object sender, EventArgs e)
312 {
313 //op =
314 button15_Click(sender, e);
315 blYunsuan = true;
316 suan = Suanshu.mul;
317 }
318
319 //op /
320 private void button4_Click(object sender, EventArgs e)
321 {
322 //op =
323 button15_Click(sender, e);
324 blYunsuan = true;
325 suan = Suanshu.div;
326 }
327
328 //clear
329 private void button5_Click(object sender, EventArgs e)
330 {
331 sshow = "0";
332 label1.Text = sshow;
333 }
334
335 private void label1_Click(object sender, EventArgs e)
336 {
337
338 }
339
340 public decimal Op(decimal r, decimal o, Suanshu s)
341 {
342 decimal ret = 0m;
343 switch (s)
344 {
345 case Suanshu.add:
346 ret = r + o;
347 shizi = r.ToString() + "+" + o.ToString() + "=" + ret.ToString();
348 break;
349 case Suanshu.sub:
350 ret = r - o;
351 shizi = r.ToString() + "-" + o.ToString() + "=" + ret.ToString();
352 break;
353 case Suanshu.mul:
354 ret = r * o;
355 shizi = r.ToString() + "*" + o.ToString() + "=" + ret.ToString();
356 break;
357 case Suanshu.div:
358 ret = r / o;
359 shizi = r.ToString() + "/" + o.ToString() + "=" + ret.ToString();
360 break;
361 default:
362 ret = 0;
363 break;
364 }
365 return ret;
366 }
367 }
368}