![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lesson6_4 { /// <summary> ///减法类 /// </summary> public class OperationJian:Operation { public override double GetResult() { double result = NumberA - NumberB; return result; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lesson6_4 { /// <summary> /// 加法类 /// </summary> public class OperationAdd:Operation { public override double GetResult() { double result = NumberA + NumberB; return result; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lesson6_4 { /// <summary> /// 除法类 /// </summary> public class OperationDiv:Operation { public override double GetResult() { if (NumberB == 0) { throw new Exception("除数不能为0!"); } double result = NumberA / NumberB; return result; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lesson6_4 { /// <summary> /// 乘法类 /// </summary> public class OperationChen:Operation { public override double GetResult() { double result = NumberA * NumberB; return result; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lesson6_4 { public class Operation { public double NumberA { get; set; } public double NumberB { get; set; } public virtual double GetResult() { double result = 0; return result; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Lesson6_4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.cmbfu.SelectedIndex = 0;//初始的运算符 } /// <summary> /// 计算数值并判断 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCalc_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtnum1.Text.Trim())) { MessageBox.Show("操作数不能为空!"); this.txtnum1.Focus(); return; } else if (string.IsNullOrEmpty(this.txtnum2.Text.Trim())) { MessageBox.Show("操作数不能为空!"); this.txtnum2.Focus(); return; } else { //计算数据 try { Operation opr = new Operation(); switch (this.cmbfu.SelectedItem.ToString().Trim()) { case "+": opr = new OperationAdd(); break; case "-": opr = new OperationJian(); break; case "*": opr = new OperationChen(); break; case "/": opr = new OperationDiv(); break; } opr.NumberA = double.Parse(this.txtnum1.Text.Trim()); opr.NumberB = double.Parse(this.txtnum2.Text.Trim()); this.lblCalc.Text = opr.GetResult().ToString(); this.lblJieguo.Visible = true; this.lblCalc.Visible = true; } catch (Exception ex) { MessageBox.Show("发生错误!" + ex.Message); } } } } }