第六章,上机练习4(计算机)

本文介绍了一个简易计算器的设计与实现过程,该计算器使用C#语言开发,能够进行基本的数学运算,如加、减、乘、除等操作。文章详细展示了如何通过继承与多态来实现不同类型的运算,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 }

 

转载于:https://www.cnblogs.com/yjjh/p/6691745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值