1 using System.Windows.Forms;
  2 
  3 namespace 计算器__
  4 {
  5     public partial class Form1 : Form
  6     {
  7         private int t = 100;
  8         double a, b, c;
  9        double pi = Math.PI;
 10         int j = 0;
 11         int k = 0;
 12         int i = 0;
 13         string[] pre = new string[100];
 14         string[] later = new string[100];
 15         public void Ci(ref int i)
 16         {
 17             i++;
 18         }
 19         public void Ji(ref int i)
 20         {
 21             i--;
 22         }
 23         public void Oi(ref int i)
 24         {
 25             i = 0;
 26         }
 27         public void Ok(ref int k)
 28         {
 29             k = 0;
 30         }
 31         public void Exchange(string[] pre, string[] later, ref int k)
 32         {
 33             try
 34             {
 35                 Stack<string> stack = new Stack<string>();
 36                 stack.Clear();
 37                 int i = 0;
 38                 int j = 0;
 39                 stack.Push("#");
 40                 while (pre[i] != "#")
 41                 {
 42 
 43                     if (pre[i] == "(")
 44                     {
 45                         stack.Push(pre[i]);
 46                     }
 47                     else if (pre[i] == ")")
 48                     {
 49                         while (stack.Peek() != "(")
 50                         {
 51                             later[j++] = stack.Pop();
 52                             k++;
 53                         }
 54                         stack.Pop();
 55                     }
 56                     else if (Isoperator(pre[i]) > 0)
 57                     {
 58                         while (Isoperator(stack.Peek()) >= Isoperator(pre[i]))
 59                         {
 60                             later[j++] = stack.Pop();
 61                             k++;
 62 
 63                         }
 64                         stack.Push(pre[i]);
 65                     }
 66                     else
 67                     {
 68                         later[j++] = pre[i];
 69                         k++;
 70                     }
 71 
 72                     i++;
 73                 }
 74                 while (stack.Peek() != "#")
 75                 {
 76                     later[j++] = stack.Pop();
 77                     k++;
 78                 }
 79             }
 80             catch
 81             {
 82                 MessageBox.Show("有错误,请重新输入!", "错误信息");
 83             }
 84 
 85         }
 86         public int Isoperator(string op)
 87         {
 88             switch (op)
 89             {
 90                 case "+": return 1;
 91                 case "-": return 1;
 92                 case "*": return 2;
 93                 case "/": return 2;
 94                 case "(": return 0;
 95                 case "#": return -1;
 96                 default: return -1;
 97             }
 98         }
 99         public double Calculate(string[] later)
100         {
101             try
102             {
103                 Stack<double> stack = new Stack<double>();
104                 stack.Clear();
105                 stack.Push(0);
106                 int i = 0;
107                 double x1;
108                 double x2;
109                 while (i < k)
110                 {
111                     if (later[i] == "+")
112                     {
113                         x2 = stack.Pop();
114                         x1 = stack.Pop();
115                         stack.Push(x1 + x2);
116                         i++;
117                     }
118                     else if (later[i] == "-")
119                     {
120                         x2 = stack.Pop();
121                         x1 = stack.Pop();
122                         stack.Push(x1 - x2);
123                         i++;
124                     }
125                     else if (later[i] == "*")
126                     {
127                         x2 = stack.Pop();
128                         x1 = stack.Pop();
129                         stack.Push(x1 * x2);
130                         i++;
131                     }
132                     else if (later[i] == "/")
133                     {
134                         x2 = stack.Pop();
135                         x1 = stack.Pop();
136                         stack.Push(x1 / x2);
137                         i++;
138                     }
139                     else
140                     {
141                         stack.Push(double.Parse(later[i]));
142                         i++;
143                     }
144 
145                 }
146                 return stack.Peek();
147             }
148             catch
149             {
150                 MessageBox.Show("有错误,请重新输入!", "错误信息");
151                 return 0;
152             }
153 
154         }
155         public double kxfh() //特殊符号
156         {
157             switch (j)
158             {
159                 case 1: c = Math.Cos(a * pi / 180);
160                     break;
161                 case 2:
162                     if (((a / 90) == (int)(a / 90)))
163                     {
164                         if (((a / 90) % 2) != 0)
165                         {
166                             MessageBox.Show("输入的角度不正确", "错误信息");
167                         }
168                         else
169                         { c = Math.Tan(a * pi / 180); }
170 
171                     }
172                     else
173                     {
174                         c = Math.Tan(a * pi / 180);
175                     }
176                     break;
177                 case 3: c = Math.Sin(a * pi / 180);
178                     break;
179                 case 4: c = 1 / a;
180                     break;
181                 case 5: c = Math.Log(a);
182                     break;
183                 case 6: c = System.Math.Sqrt(a);
184                     break;
185                 case 7:
186                     double i = 1;
187                     if (a == 0)
188                     {
189                         c = 1;
190                     }
191                     else if ((int)a != a)
192                     {
193                         MessageBox.Show("输入的数不能是小数或者负数", "错误信息");
194                     }
195                     else
196                     {
197                         for (; a != 1; a--)
198                         {
199                             i = i * a;
200                         }
201 
202                         c = i;
203                     }
204                     break;
205                 case 8: c = a * a;
206                     break;
207                 case 9: c = Math.Log10(a);
208                     break;
209                 case 10: c = Math.Pow(b, a);
210                     break;
211             }
212             return c;
213         }
214         private void jcfh(string f)//基础符号
215         {
216             if (textBox2.Text == "")
217             {
218                 pre[i] = f;
219                 Ci(ref i);
220                 textBox1.Text += f;
221             }
222             else
223             {
224                 if (j > 0)
225                 {
226                     a = Convert.ToDouble(textBox2.Text);
227                     kxfh();
228                     pre[i] = Convert.ToString(c);
229                     Ci(ref i);
230                     j = 0;
231                     pre[i] = f;
232                     Ci(ref i);
233                     textBox2.Text = "";
234                     textBox1.Text += textBox2.Text;
235                     textBox1.Text += f;
236                 }
237                 else
238                 {
239                     pre[i] = textBox2.Text;
240                     Ci(ref i);
241                     pre[i] = f;
242                     Ci(ref i);
243                     textBox2.Text = "";
244                     textBox1.Text += textBox2.Text;
245                     textBox1.Text += f;
246 
247                 }
248             }
249         }
250         public void clear()
251         {
252             if (textBox3.Text != "")
253             {
254                 a = 0;
255                 b = 0;
256                 c = 0;
257                 j = 0;
258                 textBox2.Text = "";
259                 textBox1.Text = "";
260                 textBox3.Text = "";
261                 Array.Clear(pre, 0, pre.Length);
262                 Array.Clear(later, 0, later.Length);
263                 Oi(ref i);
264                 Ok(ref k);
265             }
266         }
267         public Form1()
268         {
269             InitializeComponent();
270         }
271 
272         private void timer1_Tick(object sender, EventArgs e)
273         {
274             if (t <= 0)
275             {
276                 timer1.Enabled = false;
277                 textBox4.Enabled = false;
278                 MessageBox.Show("时间到!!!");
279                 textBox4.Enabled = false;
280                 //  txtTime.Enblaed = false;
281                 //Form2 frm2 = new Form2();
282                 //frm2.ShowDialog();
283             }
284             t = t - 1;
285             label7.Text = t.ToString();
286            //this.label1.Text = "当前时间是: " + DateTime.Now.Hour.ToString() + ":"
287            //     + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
288         }
289 
290         private void button3_Click(object sender, EventArgs e)
291         {
292               clear();
293             textBox2.Text += "3";
294             textBox1.Text += "3";
295         }
296 
297         private void Form1_Load(object sender, EventArgs e)
298         {
299            
300             for (double d = 0.01; d < 1; d += 0.04)
301             {
302                 System.Threading.Thread.Sleep(1);
303                 Application.DoEvents();
304                 this.Opacity = d;
305                 this.Refresh();
306             }
307         }
308 
309         private void button2_Click(object sender, EventArgs e)
310         {
311              clear();
312             textBox2.Text += "2";
313             textBox1.Text += "2";
314         }
315 
316         private void button4_Click(object sender, EventArgs e)
317         {
318               clear();
319             textBox2.Text += "4";
320             textBox1.Text += "4";
321         }
322 
323         private void button5_Click(object sender, EventArgs e)
324         {
325               clear();
326             textBox2.Text += "5";
327             textBox1.Text += "5";
328         }
329 
330         private void button6_Click(object sender, EventArgs e)
331         {
332               clear();
333             textBox2.Text += "6";
334             textBox1.Text += "6";
335         }
336 
337         private void button7_Click(object sender, EventArgs e)
338         {
339               clear();
340             textBox2.Text += "7";
341             textBox1.Text += "7";
342         }
343 
344         private void button8_Click(object sender, EventArgs e)
345         {
346               clear();
347             textBox2.Text += "8";
348             textBox1.Text += "8";
349         }
350 
351         private void button9_Click(object sender, EventArgs e)
352         {
353               clear();
354             textBox2.Text += "9";
355             textBox1.Text += "9";
356         }
357 
358         private void button10_Click(object sender, EventArgs e)
359         {
360               clear();
361             textBox2.Text += "0";
362             textBox1.Text += "0";
363         }
364 
365         private void button11_Click(object sender, EventArgs e)//.
366         {
367             if (textBox2.Text.IndexOf(".") < 0)
368             {
369                 textBox2.Text += ".";
370                 textBox1.Text += ".";
371             }
372         }
373 
374         private void button1_Click(object sender, EventArgs e)
375         {
376               clear();
377             textBox2.Text += "1";
378             textBox1.Text += "1";
379         }
380 
381         private void button12_Click(object sender, EventArgs e)//=
382         {
383           
384             
385            
386             int o = 0, p = 0;
387             foreach (string g in pre)
388             {
389                 if (g == "(")
390                 {
391                     o++;
392                 }
393                 if (g == ")")
394                 {
395                     p++;
396                 }
397 
398             }
399             if (o != p)
400             {
401                 MessageBox.Show("输1入的括号不匹配", "错误信息");
402             }
403             else
404             {
405                 if (textBox2.Text == "")
406                 {
407                     pre[i] = "#";
408                     Exchange(pre, later, ref k);
409                     textBox3.Text = Convert.ToString(Calculate(later));
410                 }
411                 else
412                 {
413                     if (j > 0)
414                     {
415                         a = Convert.ToDouble(textBox2.Text);
416                         pre[i] = Convert.ToString(kxfh());
417                         Ci(ref i);
418                         j = 0;
419                     }
420                     else
421                     {
422                         pre[i] = textBox2.Text;
423                         Ci(ref i);
424                     }
425                     pre[i] = "#";
426                     Exchange(pre, later, ref k);
427                     textBox3.Text = Convert.ToString(Calculate(later));
428 
429                 }
430             }
431            
432             textBox5.Text += textBox1.Text + "=" + Environment.NewLine;
433             textBox6.Text += textBox3.Text + Environment.NewLine;
434         }
435         private void RandomNum()
436         {
437             Random ran = new Random();
438            
439         }
440 
441         private void button13_Click(object sender, EventArgs e)
442         {
443            
444             jcfh("+");
445           
446         }
447 
448         private void button14_Click(object sender, EventArgs e)
449         {
450           
451             jcfh("-");
452            
453         }
454 
455         private void button15_Click(object sender, EventArgs e)
456         {
457           
458             jcfh("*");
459           
460         }
461 
462         private void button16_Click(object sender, EventArgs e)
463         {
464             
465             jcfh("/");
466            
467         }
468 
469         private void button18_Click(object sender, EventArgs e)
470         {
471             
472             pre[i] = "(";
473             Ci(ref i);
474             textBox1.Text += "(";
475            
476         }
477 
478         private void button19_Click(object sender, EventArgs e)
479         {
480          
481             pre[i] = textBox2.Text;
482             Ci(ref i);
483             pre[i] = ")";
484             Ci(ref i);
485             textBox2.Text = "";
486             textBox1.Text += textBox2.Text;
487             textBox1.Text += ")";
488             
489         }
490 
491         private void button30_Click(object sender, EventArgs e)
492         {
493           Application.Exit();
494             //Form2 frm2 = new Form2();
495             //frm2.ShowDialog();
496         }
497 
498         private void button32_Click(object sender, EventArgs e)
499         {
500             if (textBox1.Text == "")
501             {
502 
503                 MessageBox.Show("算式为空,不能退格", "错误信息");
504             }
505             else if (textBox3.Text != "")
506             {
507                 MessageBox.Show("已经算出结果,不能退格", "错误信息");
508 
509             }
510             else
511             {
512                 textBox1.Text = "";
513                 if (textBox2.Text == "")
514                 {
515                     int l = 0;
516                     foreach (string h in pre)
517                     {
518                         if (l < i - 1)
519                             textBox1.Text += h;
520                         l++;
521                     }
522                     pre[i - 1] = "";
523                     Ji(ref i);
524                 }
525                 else
526                 {
527                     foreach (string h in pre)
528                         textBox1.Text += h;
529                     textBox2.Text = "";
530 
531                 }
532             }
533         }
534 
535         private void textBox2_TextChanged(object sender, EventArgs e)
536         {
537 
538         }
539 
540         private void label3_Click(object sender, EventArgs e)
541         {
542 
543         }
544 
545         private void button21_Click(object sender, EventArgs e)
546         {
547 
548             textBox1.Text  = "";
549             textBox2.Text = "";
550             textBox3.Text = ""; 
551             textBox5.Text = "";
552             textBox6.Text = "";
553            
554         }
555 
556         private void textBox6_TextChanged(object sender, EventArgs e)
557         {
558 
559         }
560 
561         private void button20_Click(object sender, EventArgs e)
562         {
563             try
564             {
565                 t = int.Parse(textBox4.Text);
566                 if (t <= 0)
567                 {
568                     MessageBox.Show("时间不能为负数");
569                     return;
570                 }
571 
572                 textBox4.Enabled = false;
573             }
574             catch (Exception)
575             {
576                 MessageBox.Show("请输入正确的时间");
577                 return;
578 
579             }
580 
581 
582             timer1.Enabled = true;
583             timer1.Interval = 1000;
584             timer1.Start();
585             RandomNum();
586         }

计应142 王斌