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 上机4_计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Operation opr = new Operation();
switch (this.comboBox1.SelectedItem.ToString().Trim())
{
case "+":
opr = new OperationAdd();
break;
case "-":
opr = new Operationjian();
break;
case "*":
opr = new Operationcheng();
break;
case "/":
opr = new OperationDiv();
break;
}
opr.NumberA = double.Parse(this.textBox1.Text.Trim());
opr.NumberB = double.Parse(this.textBox2.Text.Trim());
this.label2.Text = opr.GetResult().ToString();
this.label1.Visible = true;
this.label2.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 上机4_计算器
{
public class Operation
{
public double NumberA { get; set; }
public double NumberB { get; set; }
public virtual double GetResult() {
double result=0;
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 上机4_计算器
{
class OperationAdd:Operation
{
public override double GetResult()
{
double result = NumberA + NumberB;
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 上机4_计算器
{
class Operationcheng : Operation
{
public override double GetResult()
{
double result = NumberA * NumberB;
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 上机4_计算器
{
class OperationDiv:Operation
{
public override double GetResult()
{
if (NumberB == 0) {
throw new Exception("除数不能为0");
}
double result=NumberA/NumberB;
return result;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 上机4_计算器
{
class Operationjian:Operation
{
public override double GetResult()
{
double result = NumberA - NumberB;
return result;
}
}
}