码迷,mamicode.com
首页 > Windows程序 > 详细

C#入门之简单计算器的实现。

时间:2015-12-27 21:51:38      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

在用C#学习的时候,师父让我先试着写一个计算器,就像任何电脑都自带的那种,想起来是觉得小case,但是实际上操作起来还是发现了不少问题的。下面的代码就是目前实现的情况,相关变量的意义也都有说明,在次就不再赘述。目前的想法是解决剩下的几个问题,后期再考虑一下优化的问题。

祭出源码:

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace 计算器
 12 {
 13     public partial class Form1 : Form
 14     {
 15         static double a = 0;//显示器变量
 16         static double b = 0;//存储值变量
 17         static int p = 0;//判断当前为整数还是小数
 18         static int l = 1;//小数位
 19         static int y = 1;//qingkongwei
 20         static double q = 0;//小数位的单位值
 21         static double x = 0;
 22         char s=+;//运算符
 23 
 24         class array_calc
 25         {
 26             public double shuru(int m, int n)
 27             {
 28                 if (y == 0)
 29                 {
 30                     a = 0;
 31                     if (n != 0)
 32                     {
 33                         a = a * 10 + m;
 34                         int count = 0;
 35                     }
 36                     else
 37                     {
 38                         q = Math.Pow(0.1, l);
 39                         a = a + (q * m);
 40                         l++;
 41                         //                    MessageBox.Show("‘q是’" + q + "");
 42                     }
 43                 }
 44                 else
 45                     if (n != 0)
 46                     {
 47                         a = a * 10 + m;
 48                         int count = 0;
 49                     }
 50                     else
 51                     {
 52                         q = Math.Pow(0.1, l);
 53                         a = a + (q * m);
 54                         l++;
 55                         //                    MessageBox.Show("‘q是’" + q + "");
 56                     };
 57                 y = 1;
 58                 return a;
 59 
 60             }
 61             public char fuhao(char s)
 62             {
 63                 b = a;
 64                 a = 0;
 65                 p = 0;
 66                 l = 1;
 67  //               MessageBox.Show("‘b是’" + b + "");
 68                 return s;
 69             }
 70             
 71         }
 72         public Form1()
 73         {
 74             InitializeComponent();
 75         }
 76 
 77         private void button12_Click(object sender, EventArgs e)
 78         {
 79             a = 0;
 80             textBox1.Text = "0";
 81             b = 0;
 82             p = 0;
 83             l = 1;
 84             
 85         }
 86 
 87         private void toolTip1_Popup(object sender, PopupEventArgs e)
 88         {
 89 
 90         }
 91 
 92         private void numericUpDown1_ValueChanged(object sender, EventArgs e)
 93         {
 94 
 95         }
 96 
 97         private void textBox1_TextChanged(object sender, EventArgs e)
 98         {
 99 
100 
101         }
102 
103         private void label1_Click(object sender, EventArgs e)
104         {
105 
106         }
107 
108         private void button1_Click(object sender, EventArgs e)
109         {
110             array_calc n1 = new array_calc();
111             if(p==0)
112                 textBox1.Text="" + n1.shuru(1, 1) + " ";
113             else
114                 textBox1.Text = "" + n1.shuru(1, 0) + " ";
115         }
116 
117         private void button2_Click(object sender, EventArgs e)
118         {
119             array_calc n2 = new array_calc();
120             if (p == 0)
121                 textBox1.Text = "" + n2.shuru(2, 1) + " ";
122             else
123                 textBox1.Text = "" + n2.shuru(2, 0) + " ";
124         }
125 
126         private void Form1_Load(object sender, EventArgs e)
127         {
128 
129         }
130 
131         private void button21_Click(object sender, EventArgs e)
132         {
133             array_calc jia = new array_calc();
134 
135             s = jia.fuhao(+);
136             textBox2.Text = "+";
137         }
138 
139         private void button17_Click(object sender, EventArgs e)
140         {
141             string result=string.Empty;
142             switch (s)
143             {
144                 case +:
145                     a = b + a;
146                     result = "" + a.ToString() + "";
147                     s = 0;
148                     break;
149                 case -:
150                     a = b - a;
151                     result = "" + a.ToString() + "";
152                     s = 0;
153                     break;
154                 case /:
155                     if (a == 0)
156                         result = "除数不能为0!";
157                     else
158                         {
159                         a = b / a;
160                         result = "" + a.ToString() + "";
161                         }
162 
163                     s = 0;
164                     break;
165                 case *:
166                     a = b * a;
167                     //MessageBox.Show("‘b是’" + b + "");
168                     //MessageBox.Show("‘a是’" + a + "");
169                     result = "" + a.ToString() + "";
170 
171                     s = 0;
172                     break;
173                 case %:
174                     a = b % a;
175                     result = "" + a.ToString() + "";
176                     break;
177                 case 0:
178                     result = "" + a.ToString() + "";
179                     break;
180                 default:
181                     break;
182               }
183             this.textBox1.Text = result;
184 
185             y = 0;
186             //l++;
187 
188             //if (a == Math.Ceiling(a))
189             //    p = 0;
190             //else
191             //{
192             //    p = 1;
193             //    x = a;
194             //    while (x != Math.Ceiling(x))
195             //    {
196             //        x = a * 10;
197             //        l++;
198             //    }
199                 
200             //}
201          
202             
203         }
204 
205         private void button13_Click(object sender, EventArgs e)
206         {
207             array_calc jan = new array_calc();
208 
209             s = jan.fuhao(-);
210             textBox2.Text = "-";
211         }
212 
213         private void button14_Click(object sender, EventArgs e)
214         {
215             array_calc ceg = new array_calc();
216             s = ceg.fuhao(*);
217             textBox2.Text = "*";
218         }
219 
220         private void button15_Click(object sender, EventArgs e)
221         {
222             array_calc chu = new array_calc();
223             s = chu.fuhao(/);
224             textBox2.Text = "/";
225         }
226 
227         private void button3_Click(object sender, EventArgs e)
228         {
229             array_calc n3 = new array_calc();
230             if (p == 0)
231                 textBox1.Text = "" + n3.shuru(3, 1) + " ";
232             else
233                 textBox1.Text = "" + n3.shuru(3, 0) + " ";
234         }
235 
236         private void button4_Click(object sender, EventArgs e)
237         {
238             array_calc n4 = new array_calc();
239             if (p == 0)
240                 textBox1.Text = "" + n4.shuru(4, 1) + " ";
241             else
242                 textBox1.Text = "" + n4.shuru(4, 0) + " ";
243         }
244 
245         private void button5_Click(object sender, EventArgs e)
246         {
247             array_calc n5 = new array_calc();
248             if (p == 0)
249                 textBox1.Text = "" + n5.shuru(5, 1) + " ";
250             else
251                 textBox1.Text = "" + n5.shuru(5, 0) + " ";
252         }
253 
254         private void button6_Click(object sender, EventArgs e)
255         {
256             array_calc n6 = new array_calc();
257             if (p == 0)
258                 textBox1.Text = "" + n6.shuru(6, 1) + " ";
259             else
260                 textBox1.Text = "" + n6.shuru(6, 0) + " ";
261         }
262 
263         private void button7_Click(object sender, EventArgs e)
264         {
265             array_calc n7 = new array_calc();
266             if (p == 0)
267                 textBox1.Text = "" + n7.shuru(7, 1) + " ";
268             else
269                 textBox1.Text = "" + n7.shuru(7, 0) + " ";
270         }
271 
272         private void button8_Click(object sender, EventArgs e)
273         {
274             array_calc n8 = new array_calc();
275             if (p == 0)
276                 textBox1.Text = "" + n8.shuru(8, 1) + " ";
277             else
278                 textBox1.Text = "" + n8.shuru(8, 0) + " ";
279         }
280 
281         private void button9_Click(object sender, EventArgs e)
282         {
283             array_calc n9 = new array_calc();
284             if (p == 0)
285                 textBox1.Text = "" + n9.shuru(9, 1) + " ";
286             else
287                 textBox1.Text = "" + n9.shuru(9, 0) + " ";
288         }
289 
290         private void button23_Click(object sender, EventArgs e)
291         {
292             array_calc n0 = new array_calc();
293             if (p == 0)
294                 textBox1.Text = "" + n0.shuru(0, 1) + " ";
295             else
296                 textBox1.Text = "" + n0.shuru(0, 0) + " ";
297         }
298 
299         private void button11_Click(object sender, EventArgs e)
300         {
301             a = 0;
302             b = 0;
303             p = 0;
304             l = 1;
305             textBox1.Text = "0";
306             textBox2.Text = " ";
307         }
308 
309         private void textBox2_TextChanged(object sender, EventArgs e)
310         {
311 
312         }
313 
314         private void button16_Click(object sender, EventArgs e)
315         {
316             a = 0 - a;
317             textBox1.Text =""+a+"";
318         }
319 
320         private void button18_Click(object sender, EventArgs e)
321         {
322             array_calc qyu = new array_calc();
323             s = qyu.fuhao(%);
324             textBox2.Text = "%";
325             y = 0;
326         }
327 
328         private void button19_Click(object sender, EventArgs e)
329         {
330              a = Math.Sqrt(a);
331              textBox1.Text = "" + a + "";
332              textBox2.Text = "";
333              y = 0;
334         }
335 
336         private void button22_Click(object sender, EventArgs e)
337         {
338             if (a == 0)
339                 textBox1.Text = "分母不能为0!";
340             else
341             {
342                 a = 1 / a;
343                 textBox1.Text = "" + a + "";
344                 textBox2.Text = "1/X";
345             }
346             y = 0;
347 
348         }
349 
350         private void button20_Click(object sender, EventArgs e)
351         {
352             array_calc nn = new array_calc();
353             p = 1;
354 //            textBox1.Text = "" + nn.shuru(1, 0) + " ";
355         }
356     }
357 }
View Code

接下来想要解决的问题是:

符号重新输入的问题:例如:1*+1=
C键、栅格键的问题

 

感觉自己的代码并没有怎么用到类和方法,似乎还是函数的思维。这个思想观点的转变比代码本身还要重要。

C#入门之简单计算器的实现。

标签:

原文地址:http://www.cnblogs.com/uestc-hujun/p/5080802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!