深入.NET平台和C#编程.第五章:上机练习4
分类:
IT文章
•
2022-04-27 22:40:20
---------------------------------------------ChengFa类---------------------------------------------
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace JiSji
8 {
9 public class ChengFa : Operation
10 {
11 /// <summary>
12 /// 乘法类
13 /// </summary>
14 /// <returns></returns>
15 public override double GetResult()
16 {
17 double result = NumberA * NumberB;
18 return result;
19 }
20 }
21
22
23 }
View Code
---------------------------------------------ChuFa类-----------------------------------------------
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace JiSji
8 {
9 /// <summary>
10 /// 除法类
11 /// </summary>
12 public class ChuFa : Operation
13 {
14 public override double GetResult()
15 {
16 if (NumberB == 0)
17 {
18 throw new Exception("除数不能为0!");
19 }
20 double result = NumberA / NumberB;
21 return result;
22 }
23 }
24
25
26 }
View Code
---------------------------------------------formJiSji窗口-------------------------------------------
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 JiSji
12 {
13 public partial class formJiSji : Form
14 {
15 public formJiSji()
16 {
17 InitializeComponent();
18 }
19
20 /// <summary>
21 /// 计算
22 /// </summary>
23 /// <param name="sender"></param>
24 /// <param name="e"></param>
25 private void button1_Click(object sender, EventArgs e)
26 {
27 if (string.IsNullOrEmpty(this.txtshu1.Text.Trim()) || string.IsNullOrEmpty(this.txtshu2.Text.Trim()))
28 {
29 MessageBox.Show("操作数不能为空!");
30 this.txtshu1.Focus();
31 }
32 else
33 {
34 try
35 {
36 Operation opr = new Operation();
37 switch (this.cmbfuh.SelectedItem.ToString().Trim())
38 {
39 case "+":
40 {
41 opr = new JiaFa();
42 }
43 break;
44 case "-":
45 {
46 opr = new JianFa();
47 }
48 break;
49 case "*":
50 {
51 opr = new ChengFa();
52 }
53 break;
54 case "/":
55 {
56 opr = new ChuFa();
57 }
58 break;
59
60 default:
61 break;
62 }
63 opr.NumberA = double.Parse(this.txtshu1.Text.Trim());
64 opr.NumberB = double.Parse(this.txtshu2.Text.Trim());
65 this.label2.Text = opr.GetResult().ToString();
66 this.label1.Visible = true;
67 this.label2.Visible = true;
68 }
69 catch (Exception ex)
70 {
71 MessageBox.Show("发生错误!" + ex.Message);
72 }
73 }
74 }
75
76
77 }
78 }
View Code