码迷,mamicode.com
首页 > 其他好文 > 详细

第六章.上机练习4

时间:2017-04-11 00:49:23      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:row   break   ace   ini   数字   img   opened   one   sso   

技术分享
 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 lesson6_3
12 {
13     public partial class frmMain : Form
14     {
15         public frmMain()
16         {
17             InitializeComponent();
18             //默认选中加法
19             cmbOperator.SelectedIndex = 0;
20         }
21         /// <summary>
22         /// 计算单击事件
23         /// </summary>
24         /// <param name="sender"></param>
25         /// <param name="e"></param>
26         private void btnCalc_Click(object sender, EventArgs e)
27         {
28             try
29             {
30                 int a = int.Parse(txtNum1.Text.Trim()) + int.Parse(txtNum2.Text.Trim());
31             }
32             catch (Exception)
33             {
34                 MessageBox.Show("不能输入除数字以外的");
35                 return;
36             }
37             if (string.IsNullOrEmpty(txtNum1.Text.Trim()) || string.IsNullOrEmpty(txtNum2.Text.Trim()))
38             {
39                 MessageBox.Show("请输入数值");
40             }
41             else if (string.IsNullOrEmpty(cmbOperator.Text.Trim()))
42             {
43                 MessageBox.Show("操作符不能为空");
44             }
45             else
46             {
47                 Operation op = new Operation();
48                 switch (cmbOperator.Text.Trim())
49                 {
50                     case "+":
51                         op = new OperationAdd();
52                         break;
53                     case "-":
54                         op = new OperationSub();
55                         break;
56                     case "*":
57                         op = new OperationMul();
58                         break;
59                     case "/":
60                         op = new OperationDiv();
61                         break;
62                 }
63                 op.Num1 = double.Parse(txtNum1.Text.Trim());
64                 op.Num2 = double.Parse(txtNum2.Text.Trim());
65                 lblResult.Text = string.Format("计算结果  {0}", op.GetResult());
66             }
67             
68 
69         }
70     }
71 }
frmMain
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace lesson6_3
 8 {
 9     /// <summary>
10     /// 操作类
11     /// </summary>
12     public class Operation
13     {
14         //第一个数
15         public double Num1 { get; set; }
16         //第二个数
17         public double Num2 { get; set; }
18         /// <summary>
19         /// 定义虚方法
20         /// </summary>
21         /// <returns></returns>
22         virtual public double GetResult()
23         {
24             double result = 0;
25             return result;
26         }
27     }
28 }
操作类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace lesson6_3
 8 {
 9     /// <summary>
10     /// 实现加法类
11     /// </summary>
12     public class OperationAdd:Operation
13     {
14         /// <summary>
15         /// 实现乘法的方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             return Num1 + Num2;
21         }
22     }
23 }
加法类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace lesson6_3
 8 {
 9     /// <summary>
10     /// 实现除法的类
11     /// </summary>
12     public class OperationDiv:Operation
13     {
14         /// <summary>
15         /// 实现除法的方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             if (Num2 == 0)
21             {
22                 throw new Exception("除数不能为0!");
23             }
24             return Num1 / Num2;
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 lesson6_3
 8 {
 9     /// <summary>
10     /// 实现乘法的类
11     /// </summary>
12     public class OperationMul:Operation
13     {
14         /// <summary>
15         /// 实现乘法的方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             return Num1 * Num2;
21         }
22     }
23 }
乘法类
技术分享
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace lesson6_3
 8 {
 9     /// <summary>
10     /// 实现减法的类
11     /// </summary>
12     public class OperationSub:Operation
13     {
14         /// <summary>
15         /// 实现减法的方法
16         /// </summary>
17         /// <returns></returns>
18         public override double GetResult()
19         {
20             return Num1 - Num2;
21         }
22     }
23 }
减法类

 

第六章.上机练习4

标签:row   break   ace   ini   数字   img   opened   one   sso   

原文地址:http://www.cnblogs.com/hanss/p/6691629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!