C#实现的简单整数四则运算计算器功能示例

时间:2021-10-12 11:31:46

本文实例讲述了c#实现的简单整数四则运算计算器功能。分享给大家供大家参考,具体如下:

运行效果图如下:

C#实现的简单整数四则运算计算器功能示例

具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
namespace 计算器
{
  public partial class form1 : form
  {
    public form1()
    {
      initializecomponent();
    }
    public string num;
    public int flag;//用于判断输入的操作符
    public double num1, num2;
    private void num0_button_click(object sender, eventargs e)
    {
      num = num + "0";
      num2 = convert.todouble(num);
      textbox.text = num;
    }
    private void num1_button_click(object sender, eventargs e)//重点算法1
    {
      if (textbox.text == "0")
      {
        num = "1";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "1";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num2_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "2";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "2";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num3_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "3";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "3";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num4_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "4";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "4";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num5_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "5";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "5";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num6_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "6";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "6";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num7_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "7";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "7";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num8_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "8";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "8";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void num9_button_click(object sender, eventargs e)
    {
      if (textbox.text == "0")
      {
        num = "9";
        textbox.text = convert.tostring(num);
      }
      else
      {
        num = num + "9";
        num2 = convert.todouble(num);
        textbox.text = num;
      }
    }
    private void add_button_click(object sender, eventargs e)//重点算法2
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox .text);
        num = "";
        flag = 1;
        textbox.text = "";
        textbox.focus();
      }
    }
    private void dev_button_click(object sender, eventargs e)
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox.text);
        num = "";
        flag = 2;
        textbox.text = "";
        textbox.focus();
      }
    }
    private void mul_button_click(object sender, eventargs e)
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox.text);
        num = "";
        flag = 3;
        textbox.text = "";
        textbox.focus();
      }
    }
    private void chu_button_click(object sender, eventargs e)
    {
      if (textbox.text.length > 0)
      {
        num1 = convert.todouble(textbox.text);
        num = "";
        flag = 4;
        // textbox.text = "";
        textbox.focus();
      }
    }
    private void equ_button_click(object sender, eventargs e)
    {
      switch (flag)
      {
        case 1:
          textbox.text = convert.tostring(num1+convert .todouble(num));//重点算法3
          num2 = convert.todouble(textbox .text);
          break;
        case 2:
          textbox.text = convert.tostring(num1 - convert.todouble(num));
          num2 = convert.todouble(textbox.text);
          break;
        case 3:
          textbox.text = convert.tostring(num1 * convert.todouble(num));
          num2 = convert.todouble(textbox.text);
          break;
        case 4:
          textbox.text = convert.tostring(num1 / convert.todouble(num));
          num2 = convert.todouble(textbox.text);
          break;
      }
    }
    private void re_button_click(object sender, eventargs e)
    {
      num = "";
      textbox.text = "0";
    }
  }
}

希望本文所述对大家c#程序设计有所帮助。

原文链接:http://blog.csdn.net/lovequanxin/article/details/5527694