一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - C# - C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

2022-02-22 13:27彬菌 C#

給大家分享用C#寫出一個(gè)計(jì)算機(jī)功能的全部代碼分享,有興趣的朋友可以跟著做一下。

實(shí)現(xiàn)效果:

C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能

Form1.cs代碼:

?
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Cal
{
  public partial class Form1 : Form
  {
    public double num1;
    public int temp = 0;//記錄存儲(chǔ)計(jì)算方式
    public Form1()
    {
      InitializeComponent();
    }
    
    private void eq_Click(object sender, EventArgs e)
    {
      switch (temp)
      {
        //加法運(yùn)算
        case 1:
          try
          {
            result.Text = (num1 + double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //減法運(yùn)算
        case 2:
          try
          {
            result.Text = (num1 - double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //乘法運(yùn)算
        case 3:
          try
          {
            result.Text = (num1 * double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //除法運(yùn)算,討論分母為零的情況
        case 4:
          if (double.Parse(result.Text)==0)
          {
            MessageBox.Show("除數(shù)不能為零", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            result.ResetText();
          }
          else
          {
            result.Text = (num1 / double.Parse(result.Text)).ToString(); temp = 0;
          }
          break;
        case 5:
          try
          {
            result.Text = (num1 % double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
      }
    }
 
    private void empty_Click(object sender, EventArgs e)
    {
      result.Text = "0";
      num1 = 0;
      temp = 0;
      //清除
      //if (result.Text.Length > 0)
      //{
      //  result.Text = result.Text.Substring(0, result.Text.Length - 1);
      //}
    }
 
    private void Zero_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "0";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "0";
      }
    }
 
    private void one_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "1";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "1";
      }
    }
 
    private void two_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "2";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "2";
      }
    }
    private void three_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "3";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "3";
      }
    }
 
    private void four_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "4";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "4";
      }
    }
 
    private void five_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "5";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "5";
      }
    }
 
    private void six_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "6";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "6";
      }
    }
 
    private void seven_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "7";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "7";
      }
    }
 
    private void eight_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "8";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "8";
      }
    }
 
    private void nine_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "9";
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + "9";
      }
    }
 
    private void point_Click(object sender, EventArgs e)
    {
      //小數(shù)點(diǎn)按鍵,初始為空,直接按 . 鍵則顯示為0.多少
      if (result.Text == "")
      {
        result.Text = "0.";
      }
      //如果再次輸入.則返回都輸入的字符后邊并提示信息
      else if (result.Text.IndexOf(".") >= 0)
      {
        MessageBox.Show("已經(jīng)輸入小數(shù)點(diǎn),無須再次輸入", "提示");
      }
      else
      {
        //前邊有數(shù)字時(shí),則直接在后邊加上.
        result.Text = result.Text + ".";
      }
    }
 
    private void add_Click(object sender, EventArgs e)
    {
      temp = 1;
      try
      {
        num1 = double.Parse(result.Text);
      }
      catch (Exception)
      {
        MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
      finally
      {
        result.Text = "";//隨后讓文本框歸空
      }
    }
 
    private void mul_Click(object sender, EventArgs e)
    {
      temp = 3;
      try
      {
        num1 = double.Parse(result.Text);
      }
      catch (Exception)
      {
        MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
      finally
      {
        result.Text = "";//隨后讓文本框歸空
      }
    }
 
    private void sub_Click(object sender, EventArgs e)
    {
      temp = 2;
      try
      {
        num1 = double.Parse(result.Text);
      }
      catch(Exception)
      {
        MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
      finally
      {
      result.Text = "";//隨后讓文本框歸空
      }
    }
 
    private void Button1_Click(object sender, EventArgs e)
    {
      temp = 4;
      try
      {
        num1 = double.Parse(result.Text);
      }
      catch (Exception)
      {
        MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
      finally
      {
        result.Text = "";//隨后讓文本框歸空
      }
    }
 
    private void per_Click(object sender, EventArgs e)
    {
      temp = 5;
      try
      {
        num1 = double.Parse(result.Text);
      }
      catch (Exception)
      {
        MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
      }
      finally
      {
        result.Text = "";//隨后讓文本框歸空
      }
    }
 
    private void opp_Click(object sender, EventArgs e)
    {
      //temp = 6;
      try
        {
         result.Text = (-double.Parse(result.Text)).ToString();
        }
      catch (Exception)
        {
          MessageBox.Show("還沒輸入數(shù)字呢", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
      finally
          {
        temp = 0;
          }
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
      result.Text = "0";
    }
 
    private void result_TextChanged(object sender, EventArgs e)
    {
      if (result.Text.Length > 18&& result.Text.Length<36)
      {
        result.Font = new Font(result.Font.FontFamily, 15, result.Font.Style);
      }
      if (result.Text.Length > 36&&result.Text.Length<45)
      {
        result.Font = new Font(result.Font.FontFamily, 10, result.Font.Style);
      }
      if (result.Text.Length > 45)
      {
        MessageBox.Show("超出范圍,將要清空了!", "錯(cuò)誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        result.ResetText();
        result.Font = new Font(result.Font.FontFamily, 20, result.Font.Style);
      }
    }
  }
}

小編已經(jīng)測(cè)試了代碼,大家可以跟著做一下看看,感謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:https://www.idaobin.com/archives/982.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人免费淫片95视频观看网站 | 久久免费看少妇高潮A片JA | 继攵催眠女乱h调教 | 91九色麻豆 | 日本五级床片全都免费播放 | 国产亚洲精品自在线亚洲情侣 | 99r8这里精品热视频免费看 | 嫩交18xxxx| 国产区综合另类亚洲欧美 | 国产精品香蕉在线观看不卡 | 男人摸女人下面 | 办公室的秘密在线观看 | 男人搡女人视频免费看 | wwwav在线| 风间由美vec399 | 亚洲精品福利一区二区在线观看 | 亚洲欧美日韩国产一区二区精品 | 女子张腿让男人桶免费 | 四虎国产成人免费观看 | 美妇在男人胯下哀求 | 欧美s级人做人爱c视频 | 免费在线看片网站 | 久久AV喷吹AV高潮欧美 | 国产成人综合久久精品红 | 亚洲国产成人久久综合一区 | 91麻豆精东果冻天美传媒老狼 | 青青青青青操 | 校花的第一次好紧好爽 | 国产免费成人在线视频 | 苍井空色欲迷墙 | 久久久免费观成人影院 | 国产一区在线免费观看 | 国产真实乱子伦xxxxchina | 免费jizz在在线播放国产 | 亚洲国产在线午夜视频无 | 91精品啪在线观看国产老湿机 | 国产精品日韩欧美一区二区 | 欧美伊香蕉久久综合类网站 | yjzz视频| 亚洲AV 中文字幕 国产 欧美 | 亚洲免费黄色网 |