1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Shangji_4 8 { 9 /// <summary> 10 /// 父类 11 /// </summary> 12 public class Operation 13 { 14 public double NumberA { get; set; } 15 public double NumberB { get; set; } 16 public virtual double GetResult() 17 { 18 double result = 0; 19 return result; 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 Shangji_4 8 { 9 class Additive:Operation 10 { 11 public override double GetResult() 12 { 13 double result = NumberA + NumberB; 14 return result; 15 } 16 } 17 }
加法类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Shangji_4 8 { 9 class Subtraction:Operation 10 { 11 public override double GetResult() 12 { 13 double result = NumberA - NumberB; 14 return result; 15 } 16 } 17 }
减法类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Shangji_4 8 { 9 class Multiplication:Operation 10 { 11 public override double GetResult() 12 { 13 double result = NumberA * NumberB; 14 return result; 15 } 16 } 17 }
乘法类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Shangji_4 8 { 9 class Division:Operation 10 { 11 public override double GetResult() 12 { 13 if (NumberB == 0) 14 { 15 throw new Exception("除数不能为0"); 16 } 17 double result = NumberA / NumberB; 18 return result; 19 } 20 } 21 }
除法类
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 Shangji_4 12 { 13 public partial class MainFrm : Form 14 { 15 public MainFrm() 16 { 17 InitializeComponent(); 18 this.cmbOperator.SelectedIndex = 0; 19 } 20 21 private void btnArithmetic_Click(object sender, EventArgs e) 22 { 23 //验证 24 //txtNo使是用户输入的操作数 25 if (string.IsNullOrEmpty(this.txtNo1.Text.Trim()) || string.IsNullOrEmpty(this.txtNo2.Text.Trim()) || string.IsNullOrEmpty(this.cmbOperator.Text.Trim())) 26 { 27 MessageBox.Show("操作数不能为空!"); 28 return; 29 } 30 //设置符号 31 try 32 { 33 Operation opr = new Operation(); 34 switch (this.cmbOperator.SelectedItem.ToString()) 35 { 36 case "+": 37 opr = new Additive(); 38 break; 39 case "-": 40 opr = new Subtraction(); 41 break; 42 case "*": 43 opr = new Multiplication(); 44 break; 45 case "/": 46 opr = new Division(); 47 break; 48 } 49 //设置参与计算的数据 50 opr.NumberA = double.Parse(this.txtNo1.Text.Trim()); 51 opr.NumberB = double.Parse(this.txtNo2.Text.Trim()); 52 //计算 53 this.lblResult.Text = opr.GetResult().ToString(); 54 this.label1.Visible = true; 55 this.lblResult.Visible = true; 56 } 57 catch (Exception) 58 { 59 60 throw; 61 } 62 } 63 64 } 65 }
Main
时间: 2024-10-25 09:27:46