国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#計算器(WinForm版)

2019-11-17 02:20:55
字體:
來源:轉載
供稿:網友

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和文本框txtDisplay10         /// </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         /// CE35         /// </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             //把二維數組中的數計算,賦值result116             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                 else129                 {130                     if (opreandArray[i - 1, 1] == "+")131                     {132                         result += Convert.ToInt32(opreandArray[i, 0]);133                     }134                     else135                     {136
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仙居县| 枣阳市| 新河县| 城口县| 营口市| 惠州市| 井研县| 安阳市| 东兰县| 高青县| 梨树县| 柳州市| 新密市| 合作市| 湟中县| 清涧县| 台南县| 高淳县| 丽水市| 西城区| 井陉县| 宜城市| 牙克石市| 舒城县| 富顺县| 江安县| 海南省| 临朐县| 祁门县| 高碑店市| 黑水县| 青海省| 景德镇市| 穆棱市| 潼南县| 乳源| 南靖县| 浦江县| 新营市| 繁昌县| 鹤山市|