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 SJ
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 /// <summary>
21 /// 加载事件
22 /// </summary>
23 /// <param name="sender"></param>
24 /// <param name="e"></param>
25 private void Form1_Load(object sender, EventArgs e)
26 {
27 cmbAdd();
28 }
29 /// <summary>
30 /// 添加运算符
31 /// </summary>
32 public void cmbAdd()
33 {
34 comboBox1.Items.Add("+");
35 comboBox1.Items.Add("-");
36 comboBox1.Items.Add("*");
37 comboBox1.Items.Add("/");
38 }
39
40 /// <summary>
41 /// 点击事件
42 /// </summary>
43 /// <param name="sender"></param>
44 /// <param name="e"></param>
45 private void btn_Click(object sender, EventArgs e)
46 {
47 if (string.IsNullOrEmpty(textBox1.Text.Trim()) && string.IsNullOrEmpty(textBox2.Text.Trim()))
48 {
49 MessageBox.Show("不能为空");
50 }
51 Operation op = new Operation();
52 switch (comboBox1.Text.Trim())
53 {
54 case "+":
55 op = new jia();
56 break;
57 case "-":
58 op = new Jiang();
59 break;
60 case "/":
61 op = new cu();
62 break;
63 case"*":
64 op = new che();
65 break;
66 }
67 op.NumberA =double.Parse(textBox1.Text.Trim());
68 op.NumberB= double.Parse(textBox2.Text.Trim());
69 this.label2.Text = op.GetResult().ToString();
70 }
71 }
72 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace SJ
8 {
9 /// <summary>
10 /// 父类
11 /// </summary>
12 public class Operation
13 {
14 //数字1
15 public double NumberA { get; set; }
16 //数字2
17 public double NumberB { get; set; }
18 //计算方法
19 public virtual double GetResult()
20 {
21 double result = 0;
22 return result;
23 }
24
25
26 }
27 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace SJ
8 {
9 /// <summary>
10 /// 乘法
11 /// </summary>
12 public class che:Operation
13 {
14 //计算方法
15 public override double GetResult()
16 {
17 if (NumberA == 0)
18 {
19 throw new Exception("不能为0");
20 }
21 double result = NumberA - NumberB;
22 return result;
23 }
24 }
25 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace SJ
8 {
9 /// <summary>
10 /// 除法方法
11 /// </summary>
12 public class cu : Operation
13 {
14 //计算方法
15 public override double GetResult()
16 {
17 if (NumberB == 0)
18 {
19 throw new Exception("除法不能为0");
20 }
21 double result = NumberA - NumberB;
22 return result;
23 }
24 }
25 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace SJ
8 {
9 /// <summary>
10 /// 加法类
11 /// </summary>
12 public class jia:Operation
13 {
14 //计算方法
15 public override double GetResult()
16 {
17 double result = NumberA + NumberB;
18 return result;
19 }
20
21 }
22 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace SJ
8 {
9 /// <summary>
10 /// 减法
11 /// </summary>
12 public class Jiang:Operation
13 {
14 //计算方法
15 public override double GetResult()
16 {
17 double result = NumberA - NumberB;
18 return result;
19 }
20 }
21 }