首页 > 代码库 > 计算器

计算器

技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace partice04
 8 {
 9     /// <summary>
10     /// 父类
11     /// </summary>
12     public class Operation
13     {
14         /// <summary>
15         /// 分别2个数字
16         /// </summary>
17         public double NumA { get; set; }
18         public double NumB { get; set; }
19 
20         /// <summary>
21         /// 虚方法
22         /// </summary>
23         /// <returns></returns>
24         public virtual double GetResult()
25         {
26             double result = 0;
27             return result;
28         }
29 
30     }
31 }
Operation 父类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace partice04
 8 {
 9     /// <summary>
10     /// 加法类
11     /// </summary>
12     public class Operation_Add:Operation
13     {
14         /// <summary>
15         /// 重写父类GetResult()方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             double result = NumA + NumB;
21             return result;
22         }
23     }
24 }
Operation-Add 加法类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace partice04
 8 {
 9     /// <summary>
10     /// 减法类
11     /// </summary>
12     public class Operation_Minus : Operation
13     {
14         /// <summary>
15         /// 重写父类GetResult()方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             double result = NumA - NumB;
21             return result;
22         }
23     }
24 }
Operation-Minus 减法类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace partice04
 8 {
 9     /// <summary>
10     /// 乘法类
11     /// </summary>
12     public class Operation_Multiply:Operation
13     {
14         /// <summary>
15         /// 重写父类GetResult()方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             double result = NumA * NumB;
21             return result;
22         }
23     }
24 }
Operation-Multiply 乘法类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace partice04
 8 {
 9     /// <summary>
10     /// 除法类
11     /// </summary>
12     public class Operation_Divide:Operation
13     {
14         /// <summary>
15         /// 重写父类GetResult()方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             //判断数字是否为0
21             if (NumB == 0)
22             {
23                 throw new Exception("除数不能为0!");
24             }
25             double result = NumA / NumB;
26             return result;
27         }
28     }
29 }
Operation-Divide 除法类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Threading.Tasks;
 9 using System.Windows.Forms;
10 
11 namespace partice04
12 {
13     public partial class FrmMain : Form
14     {
15         public FrmMain()
16         {
17             InitializeComponent();
18             BindOperation();
19         }
20 
21         /// <summary>
22         /// 设定运算符号
23         /// </summary>
24         public void BindOperation()
25         {
26             this.cbmCount.Items.Clear();
27             this.cbmCount.Items.Add("请选择");
28             this.cbmCount.Items.Add("");
29             this.cbmCount.Items.Add("");
30             this.cbmCount.Items.Add("×");
31             this.cbmCount.Items.Add("÷");
32         }
33 
34         /// <summary>
35         /// 点击计算按钮
36         /// </summary>
37         /// <param name="sender"></param>
38         /// <param name="e"></param>
39         private void btnCalcu_Click(object sender, EventArgs e)
40         {
41             //判断文本框是否为空
42             if (string.IsNullOrEmpty(txtNum1.Text.Trim()) || string.IsNullOrEmpty(txtNum2.Text.Trim()))
43             {
44                 MessageBox.Show("操作数不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
45                 txtNum1.Focus();
46                 return;
47             }
48             try
49             {
50                 Operation opr = new Operation();
51                 //switch功能,调用不同的方法
52                 switch (cbmCount.Text.Trim())
53                 {
54                     case "":
55                         opr = new Operation_Add();
56                         break;
57                     case "":
58                         opr = new Operation_Minus();
59                         break;
60                     case "×":
61                         opr = new Operation_Multiply();
62                         break;
63                     case "÷":
64                         opr = new Operation_Divide();
65                         break;
66                     case "请选择":
67                         MessageBox.Show("请选择运算符号!");
68                         break;
69                 }
70                 opr.NumA = double.Parse(txtNum1.Text.Trim());
71                 opr.NumB = double.Parse(txtNum2.Text.Trim());
72 
73                 lblResult.Text = opr.GetResult().ToString();
74 
75             }
76             catch (Exception ex)
77             {
78                 MessageBox.Show("发生错误!" + ex.Message);
79             }
80         }
81     }
82 }
FrmMain 主窗体

 

窗体布局图:

技术分享

 

效果视频:

  请移步至 --> http://www.bilibili.com/video/av9753359/

计算器