服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现简易计算器功能(2)(窗体应用)

C#实现简易计算器功能(2)(窗体应用)

2022-12-24 14:44迎迎一笑 C#

这篇文章主要为大家详细介绍了C#实现简易计算器功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现简易计算器功能第二部分的具体代码,供大家参考,具体内容如下

初始化,实现四则运算

C#实现简易计算器功能(2)(窗体应用)

?
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
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 WindowsFormsAppCalculator
{
    public partial class Form1 : Form
    {
        double number1 = 0;
        double number2 = 0;
        double result;
        int inputNumber;
 
        enum Operator { none, plus, minus, multiplication, division}//判断运算法则
        Operator mode = Operator.none;
        bool isequal = false;
 
        public Form1()
        {
            InitializeComponent();
        }
 
 
        private void num1_Click(object sender, EventArgs e)
        {
 
            inputNumber = 1;
            WindowsFormsAppCalculator(inputNumber);
            //number1 = number1 * 10 + 1;
            //labelout.Text = Convert.ToString(number1);
          
        }
 
        private void num2_Click(object sender, EventArgs e)
        {
            inputNumber = 2;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num3_Click(object sender, EventArgs e)
        {
            inputNumber = 3;
            WindowsFormsAppCalculator(inputNumber);
        }
        private void num4_Click(object sender, EventArgs e)
        {
            inputNumber = 4;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num5_Click(object sender, EventArgs e)
        {
            inputNumber = 5;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num6_Click(object sender, EventArgs e)
        {
            inputNumber = 6;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num7_Click(object sender, EventArgs e)
        {
            inputNumber = 7;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num8_Click(object sender, EventArgs e)
        {
            inputNumber = 8;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num9_Click(object sender, EventArgs e)
        {
            inputNumber = 9;
            WindowsFormsAppCalculator(inputNumber);
        }
 
        private void num0_Click(object sender, EventArgs e)
        {
            inputNumber = 0;
            WindowsFormsAppCalculator(inputNumber);
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            labelout.Text = Convert.ToString(number1);
        }
 
        private void clean_Click(object sender, EventArgs e)
        {
            cleanAll();
        }
        public void WindowsFormsAppCalculator(int an) //不懂这段怎么来的
        {
            if (mode == Operator.none)
            {
                number1 = number1 * 10 + an;
                labelout.Text = Convert.ToString(number1);
            }
            else
            {
                number2 = number2 * 10 + an;
                labelout.Text = Convert.ToString(number2);
            }
        }
 
        private void plus_Click(object sender, EventArgs e)
        {
            mode = Operator.plus;
            switchmode();
            //isequal = true;
         }
 
        private void minus_Click(object sender, EventArgs e)
        {
            mode = Operator.minus;
            switchmode();
        }
 
        private void multiplication_Click(object sender, EventArgs e)
        {
            mode = Operator.multiplication;
            switchmode();
        }
 
        private void division_Click(object sender, EventArgs e)
        {
            mode = Operator.division;
            switchmode();
        }
 
        private void equal_Click(object sender, EventArgs e)
        {
            switch (mode)
            {
                case Operator.plus:
                     result = number1 + number2;
                    break;
                case Operator.minus:
                     result = number1 - number2;
                    break;
                case Operator.multiplication:
                     result = number1 * number2;
                    break;
                case Operator.division:
                     result = number1 / number2;
                    break;
            }
            
            isequal = true;
            labelbefore.Text = "";
            labelmode.Text = "";
            labelout.Text = Convert.ToString(result);
            
        }
        public void cleanAll()
        {
            number1 = 0;
            number2 = 0;
            labelout.Text = Convert.ToString(number1);
            labelbefore.Text = "";
            labelmode.Text = "";
            isequal = false;
            mode = Operator.none;
        }
        public void switchmode()
        {
            switch (mode)
            {
                case Operator.plus:
                    labelmode.Text = "+";
                    break;
                case Operator.minus:
                    labelmode.Text = "-";
                    break;
                case Operator.multiplication:
                    labelmode.Text = "*";
                    break;
                case Operator.division:
                    labelmode.Text = "/";
                    break;
            }
            //isequal = true;
            //cleanAll();
 
           
            labelbefore.Text = Convert.ToString(number1);
            labelout.Text = Convert.ToString(number2);
        }
    }
}
?
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
namespace WindowsFormsAppCalculator
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows 窗体设计器生成的代码
 
        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.labelout = new System.Windows.Forms.Label();
            this.labelbefore = new System.Windows.Forms.Label();
            this.labelmode = new System.Windows.Forms.Label();
            this.num1 = new System.Windows.Forms.Button();
            this.num6 = new System.Windows.Forms.Button();
            this.num8 = new System.Windows.Forms.Button();
            this.num7 = new System.Windows.Forms.Button();
            this.num5 = new System.Windows.Forms.Button();
            this.num4 = new System.Windows.Forms.Button();
            this.num9 = new System.Windows.Forms.Button();
            this.num2 = new System.Windows.Forms.Button();
            this.num3 = new System.Windows.Forms.Button();
            this.num0 = new System.Windows.Forms.Button();
            this.clean = new System.Windows.Forms.Button();
            this.minus = new System.Windows.Forms.Button();
            this.multiplication = new System.Windows.Forms.Button();
            this.division = new System.Windows.Forms.Button();
            this.plus = new System.Windows.Forms.Button();
            this.equal = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // labelout
            // 
            this.labelout.Font = new System.Drawing.Font("宋体", 25.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.labelout.Location = new System.Drawing.Point(26, 111);
            this.labelout.Name = "labelout";
            this.labelout.Size = new System.Drawing.Size(463, 49);
            this.labelout.TabIndex = 0;
            this.labelout.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // labelbefore
            // 
            this.labelbefore.Font = new System.Drawing.Font("宋体", 22.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.labelbefore.Location = new System.Drawing.Point(27, 9);
            this.labelbefore.Name = "labelbefore";
            this.labelbefore.Size = new System.Drawing.Size(463, 43);
            this.labelbefore.TabIndex = 1;
            this.labelbefore.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // labelmode
            // 
            this.labelmode.Font = new System.Drawing.Font("宋体", 22.2F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.labelmode.Location = new System.Drawing.Point(28, 61);
            this.labelmode.Name = "labelmode";
            this.labelmode.Size = new System.Drawing.Size(463, 39);
            this.labelmode.TabIndex = 2;
            this.labelmode.TextAlign = System.Drawing.ContentAlignment.BottomRight;
            // 
            // num1
            // 
            this.num1.Location = new System.Drawing.Point(35, 199);
            this.num1.Name = "num1";
            this.num1.Size = new System.Drawing.Size(71, 41);
            this.num1.TabIndex = 3;
            this.num1.Text = "1";
            this.num1.UseVisualStyleBackColor = true;
            this.num1.Click += new System.EventHandler(this.num1_Click);
            // 
            // num6
            // 
            this.num6.Location = new System.Drawing.Point(218, 253);
            this.num6.Name = "num6";
            this.num6.Size = new System.Drawing.Size(71, 41);
            this.num6.TabIndex = 6;
            this.num6.Text = "6";
            this.num6.UseVisualStyleBackColor = true;
            this.num6.Click += new System.EventHandler(this.num6_Click);
            // 
            // num8
            // 
            this.num8.Location = new System.Drawing.Point(130, 312);
            this.num8.Name = "num8";
            this.num8.Size = new System.Drawing.Size(71, 41);
            this.num8.TabIndex = 7;
            this.num8.Text = "8";
            this.num8.UseVisualStyleBackColor = true;
            this.num8.Click += new System.EventHandler(this.num8_Click);
            // 
            // num7
            // 
            this.num7.Location = new System.Drawing.Point(35, 312);
            this.num7.Name = "num7";
            this.num7.Size = new System.Drawing.Size(71, 41);
            this.num7.TabIndex = 8;
            this.num7.Text = "7";
            this.num7.UseVisualStyleBackColor = true;
            this.num7.Click += new System.EventHandler(this.num7_Click);
            // 
            // num5
            // 
            this.num5.Location = new System.Drawing.Point(130, 253);
            this.num5.Name = "num5";
            this.num5.Size = new System.Drawing.Size(71, 41);
            this.num5.TabIndex = 9;
            this.num5.Text = "5";
            this.num5.UseVisualStyleBackColor = true;
            this.num5.Click += new System.EventHandler(this.num5_Click);
            // 
            // num4
            // 
            this.num4.Location = new System.Drawing.Point(35, 253);
            this.num4.Name = "num4";
            this.num4.Size = new System.Drawing.Size(71, 41);
            this.num4.TabIndex = 10;
            this.num4.Text = "4";
            this.num4.UseVisualStyleBackColor = true;
            this.num4.Click += new System.EventHandler(this.num4_Click);
            // 
            // num9
            // 
            this.num9.Location = new System.Drawing.Point(218, 312);
            this.num9.Name = "num9";
            this.num9.Size = new System.Drawing.Size(71, 41);
            this.num9.TabIndex = 11;
            this.num9.Text = "9";
            this.num9.UseVisualStyleBackColor = true;
            this.num9.Click += new System.EventHandler(this.num9_Click);
            // 
            // num2
            // 
            this.num2.Location = new System.Drawing.Point(130, 199);
            this.num2.Name = "num2";
            this.num2.Size = new System.Drawing.Size(71, 41);
            this.num2.TabIndex = 12;
            this.num2.Text = "2";
            this.num2.UseVisualStyleBackColor = true;
            this.num2.Click += new System.EventHandler(this.num2_Click);
            // 
            // num3
            // 
            this.num3.Location = new System.Drawing.Point(218, 199);
            this.num3.Name = "num3";
            this.num3.Size = new System.Drawing.Size(71, 41);
            this.num3.TabIndex = 13;
            this.num3.Text = "3";
            this.num3.UseVisualStyleBackColor = true;
            this.num3.Click += new System.EventHandler(this.num3_Click);
            // 
            // num0
            // 
            this.num0.Location = new System.Drawing.Point(130, 372);
            this.num0.Name = "num0";
            this.num0.Size = new System.Drawing.Size(71, 41);
            this.num0.TabIndex = 14;
            this.num0.Text = "0";
            this.num0.UseVisualStyleBackColor = true;
            this.num0.Click += new System.EventHandler(this.num0_Click);
            // 
            // clean
            // 
            this.clean.Location = new System.Drawing.Point(387, 199);
            this.clean.Name = "clean";
            this.clean.Size = new System.Drawing.Size(71, 41);
            this.clean.TabIndex = 15;
            this.clean.Text = "清除";
            this.clean.UseVisualStyleBackColor = true;
            this.clean.Click += new System.EventHandler(this.clean_Click);
            // 
            // minus
            // 
            this.minus.Location = new System.Drawing.Point(305, 253);
            this.minus.Name = "minus";
            this.minus.Size = new System.Drawing.Size(71, 41);
            this.minus.TabIndex = 16;
            this.minus.Text = "-";
            this.minus.UseVisualStyleBackColor = true;
            this.minus.Click += new System.EventHandler(this.minus_Click);
            // 
            // multiplication
            // 
            this.multiplication.Location = new System.Drawing.Point(305, 312);
            this.multiplication.Name = "multiplication";
            this.multiplication.Size = new System.Drawing.Size(71, 41);
            this.multiplication.TabIndex = 17;
            this.multiplication.Text = "*";
            this.multiplication.UseVisualStyleBackColor = true;
            this.multiplication.Click += new System.EventHandler(this.multiplication_Click);
            // 
            // division
            // 
            this.division.Location = new System.Drawing.Point(305, 372);
            this.division.Name = "division";
            this.division.Size = new System.Drawing.Size(71, 41);
            this.division.TabIndex = 18;
            this.division.Text = "/";
            this.division.UseVisualStyleBackColor = true;
            this.division.Click += new System.EventHandler(this.division_Click);
            // 
            // plus
            // 
            this.plus.Location = new System.Drawing.Point(305, 199);
            this.plus.Name = "plus";
            this.plus.Size = new System.Drawing.Size(71, 41);
            this.plus.TabIndex = 19;
            this.plus.Text = "+";
            this.plus.UseVisualStyleBackColor = true;
            this.plus.Click += new System.EventHandler(this.plus_Click);
            // 
            // equal
            // 
            this.equal.Location = new System.Drawing.Point(218, 372);
            this.equal.Name = "equal";
            this.equal.Size = new System.Drawing.Size(71, 41);
            this.equal.TabIndex = 20;
            this.equal.Text = "=";
            this.equal.UseVisualStyleBackColor = true;
            this.equal.Click += new System.EventHandler(this.equal_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(502, 466);
            this.Controls.Add(this.equal);
            this.Controls.Add(this.plus);
            this.Controls.Add(this.division);
            this.Controls.Add(this.multiplication);
            this.Controls.Add(this.minus);
            this.Controls.Add(this.clean);
            this.Controls.Add(this.num0);
            this.Controls.Add(this.num3);
            this.Controls.Add(this.num2);
            this.Controls.Add(this.num9);
            this.Controls.Add(this.num4);
            this.Controls.Add(this.num5);
            this.Controls.Add(this.num7);
            this.Controls.Add(this.num8);
            this.Controls.Add(this.num6);
            this.Controls.Add(this.num1);
            this.Controls.Add(this.labelmode);
            this.Controls.Add(this.labelbefore);
            this.Controls.Add(this.labelout);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.Label labelout;
        private System.Windows.Forms.Label labelbefore;
        private System.Windows.Forms.Label labelmode;
        private System.Windows.Forms.Button num1;
        private System.Windows.Forms.Button num6;
        private System.Windows.Forms.Button num8;
        private System.Windows.Forms.Button num7;
        private System.Windows.Forms.Button num5;
        private System.Windows.Forms.Button num4;
        private System.Windows.Forms.Button num9;
        private System.Windows.Forms.Button num2;
        private System.Windows.Forms.Button num3;
        private System.Windows.Forms.Button num0;
        private System.Windows.Forms.Button clean;
        private System.Windows.Forms.Button minus;
        private System.Windows.Forms.Button multiplication;
        private System.Windows.Forms.Button division;
        private System.Windows.Forms.Button plus;
        private System.Windows.Forms.Button equal;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_43069920/article/details/119059670

延伸 · 阅读

精彩推荐
  • C#一篇文章教会你用Unity制作网格地图生成组件

    一篇文章教会你用Unity制作网格地图生成组件

    网格地图这个功能在策略型游戏中应用比较广泛,基本情况下会将地图分割成正方形网格或者六边形网格,这篇文章主要给大家介绍了如何通过一篇文章学会...

    心之凌儿9182022-11-28
  • C#详解三种C#实现数组反转方式

    详解三种C#实现数组反转方式

    本篇文章主要介绍了详解三种C#实现数组反转方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    11111111221f9182021-12-31
  • C#结合Visual C#开发环境讲解C#中事件的订阅和取消订阅

    结合Visual C#开发环境讲解C#中事件的订阅和取消订阅

    这篇文章主要介绍了C#中事件的订阅和取消订阅,结合Visual C#开发环境来进行讲解,Visual C#被集成在微软的IDE程序Visual Studio中,需要的朋友可以参考下...

    C#教程网7232021-11-11
  • C#C#排序算法之快速排序解析

    C#排序算法之快速排序解析

    这篇文章主要为大家详细介绍了C#排序算法之快速排序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    mlovelcottage6362022-08-20
  • C#C#的TimeSpan案例详解

    C#的TimeSpan案例详解

    这篇文章主要介绍了C#的TimeSpan案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    十年河东,十年河西,莫4172022-11-30
  • C#c# 如何使用 My 命名空间

    c# 如何使用 My 命名空间

    这篇文章主要介绍了c# 如何使用 My 命名空间,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    olprod4642022-10-11
  • C#在WinForm中发送HTTP请求的实现方法

    在WinForm中发送HTTP请求的实现方法

    下面小编就为大家带来一篇在WinForm中发送HTTP请求的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网8702021-11-24
  • C#C#使用JavaScriptSerializer序列化时的时间类型处理

    C#使用JavaScriptSerializer序列化时的时间类型处理

    这篇文章主要为大家详细介绍了C#使用JavaScriptSerializer序列化时的时间类型处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    车伊洛10642022-01-20