程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#計算器(WinForm版),

C#計算器(WinForm版),

編輯:C#入門知識

C#計算器(WinForm版),


1.打開Microsoft Visual Studio 2013, 新建名字為【Calculator】的項目。

2.在Form1窗體上放置合適的控件,進行布局,(PS:建議遵循好的命名規范)。

3.Form1的代碼書寫如下:

 1  public partial class frmMain : Form
 2     {
 3         private string number;
 4         public frmMain()
 5         {
 6             InitializeComponent();
 7         }
 8         /// <summary>
 9         /// 將計算器可輸入字符1~9,+,-,*,/,(,)同時賦值給變量number和文本框txtDisplay
10         /// </summary>
11         /// <param name="sender"></param>
12         /// <param name="e"></param>
13         private void InputHandler(object sender, EventArgs e)
14         {
15             number += ((Button)sender).Text;
16             this.txtDisplay.Text += ((Button)sender).Text;
17         }
18         /// <summary>
19         /// =
20         /// </summary>
21         /// <param name="sender"></param>
22         /// <param name="e"></param>
23         private void btnQual_Click(object sender, EventArgs e)
24         {
25             //如果txtDisplay文本框不為空,則進行計算
26             if (txtDisplay.Text != String.Empty)
27             {
28                 number = this.txtDisplay.Text;
29                 this.txtDisplay.Text += "=";
30                 this.txtDisplay.Text += Calculator.dealWith(number).ToString();
31             }
32         }
33         /// <summary>
34         /// CE
35         /// </summary>
36         /// <param name="sender"></param>
37         /// <param name="e"></param>
38         private void btClear_Click(object sender, EventArgs e)
39         {
40                 number = string.Empty;
41                 txtDisplay.Text = string.Empty;
42         }
43         /// <summary>
44         /// 限制txtDisplay中的字符輸入
45         /// </summary>
46         /// <param name="sender"></param>
47         /// <param name="e"></param>
48         private void txtDisplay_KeyPress(object sender, KeyPressEventArgs e)
49         {
50             //如果輸入的不是數字類別,也不是回車鍵、Backspace鍵、+ - * / ( ),則txtDisplay_KeyPress取消該輸入
51             if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8 && e.KeyChar != (char)40 && e.KeyChar != (char)41 && e.KeyChar != (char)42 && e.KeyChar != (char)43 && e.KeyChar != (char)45 && e.KeyChar != (char)47)
52             {
53                 e.Handled = true;
54             } 
55         } 
56     }

4.我們注意到 btnQual_Click事件中有一個方法Calculator.dealWith(),同樣我們將Calculator代碼書寫如下:

  1  public static class Calculator
  2     {
  3        public static float dealWith(string number)
  4         {
  5             string operand1="", opreand2="";
  6             float result = 0;
  7             char opera = ' ', operandOrOera = ' ';      
  8             string[,] opreandArray = new string[50, 2];
  9             Queue numberQueue = new Queue();
 10 
 11             //循環字符串中的所有字符並賦值給numberQueue隊列 
 12             foreach (char c in number)
 13             {
 14 
 15                 numberQueue.Enqueue(c); 
 16             }
 17 
 18             //拆分隊列中的字符,構成一個數字與“+”或“-”的組合,然後逐個放入二維數組opreandArray中
 19             while (numberQueue.Count != 0)
 20             {
 21                 operandOrOera = Convert.ToChar(numberQueue.Peek());
 22                 if (operandOrOera == '(')
 23                 {
 24                     numberQueue.Dequeue();
 25                     string inside = null;
 26                     while (Convert.ToChar(numberQueue.Peek()) != ')')
 27                     {
 28                         inside += (numberQueue.Dequeue()).ToString();
 29                     }
 30                     numberQueue.Dequeue();
 31                     operand1 = dealWith(inside).ToString();
 32                 }
 33                 while (Convert.ToInt32(operandOrOera) > 47 && Convert.ToInt32(operandOrOera) < 58)//ASCII48-57對應0-9
 34                 {
 35                     numberQueue.Dequeue();
 36                     operand1 += operandOrOera.ToString();
 37                     if (numberQueue.Count != 0)
 38                     {
 39                         operandOrOera = Convert.ToChar(numberQueue.Peek());
 40                     }
 41                     else
 42                     {
 43                         break;
 44                     }
 45                 }
 46                 int j = 0;
 47                 if (operandOrOera == '+' || operandOrOera == '-' || operandOrOera == '*' || operandOrOera == '/')
 48                 {
 49                     numberQueue.Dequeue();
 50                     opera = operandOrOera;
 51                     //如果是"+"或"-"
 52                     if (opera == '+' || opera == '-')
 53                     {
 54                         opreandArray[j, 0] = operand1;
 55                         opreandArray[j, 1] = opera.ToString();
 56                         j++;
 57                         operand1 = null;
 58                     }
 59                     //如果是"*"或"/"
 60                     else
 61                     {
 62                         char n = Convert.ToChar(numberQueue.Peek());
 63                         if (n == '(')
 64                         {
 65 
 66                             numberQueue.Dequeue();
 67                             string inside = null;
 68                             while (Convert.ToChar(numberQueue.Peek()) != ')')
 69                             {
 70                                 inside += (numberQueue.Dequeue()).ToString();
 71                             }
 72                             numberQueue.Dequeue();
 73                             opreand2 = dealWith(inside).ToString();
 74                         }
 75                         while (Convert.ToInt32(n) > 47 && Convert.ToInt32(n) < 58)
 76                         {
 77                             opreand2 += n.ToString();
 78                             numberQueue.Dequeue();
 79                             if (numberQueue.Count != 0)
 80                             {
 81                                 n = Convert.ToChar(numberQueue.Peek());
 82                             }
 83                             else
 84                             {
 85                                 break;
 86                             }
 87                         }
 88 
 89                         switch (opera)
 90                         {
 91                             case ('*'):
 92                                 {
 93                                     operand1 = (Convert.ToInt32(operand1) * Convert.ToInt32(opreand2)).ToString();
 94                                     break;
 95                                 }
 96                             case ('/'):
 97                                 {
 98                                     try
 99                                     {
100                                         operand1 = (Convert.ToInt32(operand1) / Convert.ToInt32(opreand2)).ToString();
101                                     }
102                                     catch(Exception) { 
103                                         
104                                     }
105                                     break;
106                                 }
107 
108                         }
109                         opreand2 = null;
110                     }
111                 }
112             }
113 
114 
115             //把二維數組中的數計算,賦值result
116             int count = 0;
117             for (int i = 0; opreandArray[i, 0] != null; i++)
118             {
119                 count++;
120             }
121             for (int i = 0; i < count; i++)
122             {
123                 if (i == 0)
124                 {
125                     result += Convert.ToInt32(opreandArray[i, 0]);
126 
127                 }
128                 else
129                 {
130                     if (opreandArray[i - 1, 1] == "+")
131                     {
132                         result += Convert.ToInt32(opreandArray[i, 0]);
133                     }
134                     else
135                     {
136                         result -= Convert.ToInt32(opreandArray[i, 0]);
137                     }
138                 }
139             }
140 
141 
142 
143             //最後把沒有放進數組中的加上或者減掉
144             if (count != 0)
145             {
146                 if (opreandArray[count - 1, 1] == "+")
147                 {
148                     return result + Convert.ToInt32(operand1);
149                 }
150                 else
151                 {
152                     return result - Convert.ToInt32(operand1);
153                 }
154             }
155             else
156             {
157                 return Convert.ToInt32(operand1);
158             }
159         }
160     }

5.O(∩_∩)O哈哈~,基本的代碼書寫我們就完成啦。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved