C#计算器(WinForm版)

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哈哈~,基本的代码书写我们就完成啦。

你可能感兴趣的:(WinForm)