namespace _06_上机四_计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.comb.SelectedIndex = 0;
}
private void Calc_Click(object sender, EventArgs e)
{
try
{
//非空验证
if (string.IsNullOrEmpty(this.text1.Text.Trim()))
{
MessageBox.Show("输入不能为空!!!");
this.text1.Focus();
return;
}
if (string.IsNullOrEmpty(this.text2.Text.Trim()))
{
MessageBox.Show("输入不能为空!!!");
this.text2.Focus();
return;
}
//设置符号
Operation operation = null;
string yun = this.comb.Text.Trim();
switch (yun)
{
case "+":
operation = new Add();
break;
case "-":
operation = new jian();
break;
case "*":
operation = new cheng();
break;
case "/":
operation = new chu();
break;
default:
MessageBox.Show("kkkk");
break;
}
//设置参与计算的数据
operation.NumberA = double.Parse(this.text1.Text.Trim());
operation.NumberB = double.Parse(this.text1.Text.Trim());
this.jieguo.Text = operation.GetResult().ToString();
}
catch (Exception ex)
{
MessageBox.Show("发生错误!!!"+ex.Message);
}
}
namespace _06_上机四_计算器
{
class chu:Operation
{
public override double GetResult()
{
if (NumberB==0)
{
throw new Exception("除数不能为零!");
}
double result = NumberA / NumberB;
return result;
}
}
}
namespace _06_上机四_计算器
{
class cheng:Operation
{
public override double GetResult()
{
if (NumberB == 0)
{
throw new Exception("乘数不能为零!");
}
double result = this.NumberA * this.NumberB;
return result;
}
}
}
namespace _06_上机四_计算器
{
class jian:Operation
{
public override double GetResult()
{
double result = this.NumberA - this.NumberB;
return result;
}
}
}
namespace _06_上机四_计算器
{
public class Add:Operation
{
public override double GetResult()
{
double result = this.NumberA +this.NumberB;
return result;
}
}
}
namespace _06_上机四_计算器
{
public class Operation
{
public double NumberA { get; set; }
public double NumberB { get; set; }
public virtual double GetResult()
{
double result = 0;
return result;
}
}
}