首页 > 代码库 > (深入.Net平台和C#编程)第六章上机练习4.李向阳.20170411

(深入.Net平台和C#编程)第六章上机练习4.李向阳.20170411

技术分享
 1 ==============加法类================
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj4.Entity
 9 {
10     /// <summary>
11     /// 加法
12     /// </summary>
13     public class Jiafa : Operation
14     {
15         //计算方法
16         public override double GetResult()
17         {
18             if (NumberB == 0)
19             {
20                 throw new Exception("不能为0");
21             }
22             double result = NumberA + NumberB;
23             return result;
24         }
25     }
26 }
View Code
技术分享
 1 ============减法类============
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj4.Entity
 9 {
10     /// <summary>
11     /// 减法
12     /// </summary>
13     public class Jianfa : Operation
14     {
15         //计算方法
16         public override double GetResult()
17         {
18             double result = NumberA - NumberB;
19             return result;
20         }
21     }
22 }
View Code
技术分享
 1 =========乘法类===============
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj4.Entity
 9 {
10     /// <summary>
11     /// 乘法类
12     /// </summary>
13     public class Cehng : Operation
14     {
15         //计算方法
16         public override double GetResult()
17         {
18             if (NumberA == 0)
19             {
20                 throw new Exception("不能为0");
21             }
22             double result = NumberA * NumberB;
23             return result;
24         }
25     }
26 }
View Code
技术分享
 1 ===============除法类==============
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace Sj4.Entity
 9 {
10     /// <summary>
11     /// 除法
12     /// </summary>
13     public class Chufa : Operation
14     {
15         //计算方法
16         public override double GetResult()
17         {
18             if (NumberB == 0)
19             {
20                 throw new Exception("不能为0");
21             }
22             double result = NumberA / NumberB;
23             return result;
24         }
25     }
26 }
View Code
技术分享
 1 ================测试类==================
 2 using System;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.Linq;
 8 using System.Text;
 9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11 using Sj4.Entity;
12 
13 namespace Sj4
14 {
15     public partial class FrmMain : Form
16     {
17         public FrmMain()
18         {
19             InitializeComponent();
20         }
21         //加载事件
22         private void FrmMain_Load(object sender, EventArgs e)
23         {
24             cmbAdd();
25         }
26         /// <summary>
27         /// 添加运算符
28         /// </summary>
29         public void cmbAdd()
30         {
31             cmbList.Text = "+";
32             cmbList.Items.Add("-");
33             cmbList.Items.Add("*");
34             cmbList.Items.Add("/");
35         }
36         /// <summary>
37         /// 计算点击事件
38         /// </summary>
39         /// <param name="sender"></param>
40         /// <param name="e"></param>
41         private void btnJiSuan_Click(object sender, EventArgs e)
42         {
43             if (string.IsNullOrEmpty(txt1.Text.Trim()) && string.IsNullOrEmpty(txt2.Text.Trim()))
44             {
45                 MessageBox.Show("不能为空");
46             }
47             Operation bb = new Operation();
48             switch (cmbList.Text.Trim())
49             {
50                 case "+":
51                     bb = new Jiafa();
52                     break;
53                 case "-":
54                     bb = new Jianfa();
55                     break;
56                 case "/":
57                     bb = new Chufa();
58                     break;
59                 case "*":
60                     bb = new Cehng();
61                     break;
62             }
63             bb.NumberA = double.Parse(txt1.Text.Trim());
64             bb.NumberB = double.Parse(txt2.Text.Trim());
65             this.lblJg.Text = bb.GetResult().ToString();
66         }
67     }
68 }
View Code

技术分享

(深入.Net平台和C#编程)第六章上机练习4.李向阳.20170411