国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - C# - C#實現簡單計算器功能

C#實現簡單計算器功能

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

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

實現效果:

C#實現簡單計算器功能

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;//記錄存儲計算方式
    public Form1()
    {
      InitializeComponent();
    }
    
    private void eq_Click(object sender, EventArgs e)
    {
      switch (temp)
      {
        //加法運算
        case 1:
          try
          {
            result.Text = (num1 + double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數字呢", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //減法運算
        case 2:
          try
          {
            result.Text = (num1 - double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數字呢", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //乘法運算
        case 3:
          try
          {
            result.Text = (num1 * double.Parse(result.Text)).ToString();
          }
          catch (Exception)
          {
            MessageBox.Show("還沒輸入數字呢", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
          }
          finally
          {
            temp = 0;
          }
          break;
        //除法運算,討論分母為零的情況
        case 4:
          if (double.Parse(result.Text)==0)
          {
            MessageBox.Show("除數不能為零", "錯誤提示", 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("還沒輸入數字呢", "錯誤提示", 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
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "0";
      }
    }
 
    private void one_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "1";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "1";
      }
    }
 
    private void two_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "2";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "2";
      }
    }
    private void three_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "3";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "3";
      }
    }
 
    private void four_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "4";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "4";
      }
    }
 
    private void five_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "5";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "5";
      }
    }
 
    private void six_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "6";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "6";
      }
    }
 
    private void seven_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "7";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "7";
      }
    }
 
    private void eight_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "8";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "8";
      }
    }
 
    private void nine_Click(object sender, EventArgs e)
    {
      if (result.Text == "0")
      {
        result.Text = "9";
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + "9";
      }
    }
 
    private void point_Click(object sender, EventArgs e)
    {
      //小數點按鍵,初始為空,直接按 . 鍵則顯示為0.多少
      if (result.Text == "")
      {
        result.Text = "0.";
      }
      //如果再次輸入.則返回都輸入的字符后邊并提示信息
      else if (result.Text.IndexOf(".") >= 0)
      {
        MessageBox.Show("已經輸入小數點,無須再次輸入", "提示");
      }
      else
      {
        //前邊有數字時,則直接在后邊加上.
        result.Text = result.Text + ".";
      }
    }
 
    private void add_Click(object sender, EventArgs e)
    {
      temp = 1;
      try
      {
        num1 = double.Parse(result.Text);
      }
      catch (Exception)
      {
        MessageBox.Show("還沒輸入數字呢", "錯誤提示", 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("還沒輸入數字呢", "錯誤提示", 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("還沒輸入數字呢", "錯誤提示", 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("還沒輸入數字呢", "錯誤提示", 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("還沒輸入數字呢", "錯誤提示", 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("還沒輸入數字呢", "錯誤提示", 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("超出范圍,將要清空了!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        result.ResetText();
        result.Font = new Font(result.Font.FontFamily, 20, result.Font.Style);
      }
    }
  }
}

小編已經測試了代碼,大家可以跟著做一下看看,感謝大家對服務器之家的支持。

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

延伸 · 閱讀

精彩推薦
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
主站蜘蛛池模板: 亚洲精品日韩激情在线电影 | 黄色片免费观看网站 | 国产精品不卡 | 91丁香婷婷综合久久欧美 | 久久久在线| 国产在线一区不卡 | 免费观看电视在线高清视频 | 人人射在线观看 | 人人射视频 | 色天堂影院 | 国产成人av一区二区三区 | 欧美久久久网站 | 欧美国产日韩一区二区三区 | 在线观看日韩精品 | 亚洲国产精品久久久久 | 免费毛片a线观看 | 天堂在线www | 黄色av电影 | 四虎在线观看 | 91高清在线 | 黄色片免费观看网站 | 欧美日韩视频一区二区 | 精品中文一区 | 国产精品99 | 国产欧美精品一区二区三区 | 亚洲国产精品视频一区 | 久久久国产视频 | 国产成人精品免费 | 亚州成人 | 国产成人av一区 | 日韩电影免费观看 | 亚洲电影第二页 | 超碰国产在线 | 久久精品久久综合 | 国产毛片18片毛一级特黄日韩a | 九九国产 | 日韩大片播放器 | 国产视频一区二区 | 欧美不卡| 综合另类 | 免费看黄色av |