Calculate计算类;
public abstract class Calculate //抽象类 Calculate { private double a;//第一个数 public double A//封装字段 { get { return a; } set { a = value; } } private double b;//第二个数字段封装 public double B { get { return b; } set { b = value; } } public double result; public virtual double Result() { return result; } } public class Add : Calculate { public override double Result() { result = A + B; return result; } } public class Sub : Calculate { public override double Result() { result = A - B; return result; } } public class Mul : Calculate { public override double Result() { result = A * B; return result; } } public class Div : Calculate { public override double Result() { if (A == 0) { MessageBox.Show("分母不能为零"); } result = A / B; return result; } } public class Mod : Calculate { public override double Result() { if (A == 0) { MessageBox.Show("分母不能为零"); } result = A % B; return result; } } public class calculate { public static Calculate Cal(string type) { Calculate cal = null; switch (type) { case "+": cal = new Add(); //加法策略模式 break; case "-": cal = new Sub(); break; case "*": cal = new Mul(); break; case "/": cal = new Div(); break; case "%": cal = new Mod(); break; default: break; } return cal; } } }Form1代码
private void txtresult_KeyDown(object sender, KeyEventArgs e) { Calculate cal=null; string typ = typ1.Text.ToString();//调用类成员 cal = calculate.Cal(typ); cal.A = Convert.ToDouble(txtone.Text); cal.B= Convert.ToDouble(txttwo.Text); string result = Convert.ToString(cal.Result()); //判断计算情况 if (e.KeyCode==Keys.Enter) { if (txtresult.Text ==result.ToString()) { right++; MessageBox.Show("回答正确"); txtone.Text = ""; txttwo.Text = ""; //txtthree.Text = ""; txtresult.Text = ""; typ1.Text = ""; //typ2.Text = ""; } else { MessageBox.Show("回答错误"); txtone.Text = ""; txttwo.Text = ""; //txtthree.Text = ""; txtresult.Text = ""; typ1.Text = ""; //typ2.Text = ""; } question++; } }截图
ASP.net
Calculate计算类;
public abstract class Calculate //抽象类 Calculate { private double a;//第一个数 public double A//封装字段 { get { return a; } set { a = value; } } private double b;//第二个数字段封装 public double B { get { return b; } set { b = value; } } public double result; public virtual double Result() { return result; } } public class Add : Calculate { public override double Result() { result = A + B; return result; } } public class Sub : Calculate { public override double Result() { result = A - B; return result; } } public class Mul : Calculate { public override double Result() { result = A * B; return result; } } public class Div : Calculate { public override double Result() { if (A == 0) { MessageBox.Show("分母不能为零"); } result = A / B; return result; } } public class Mod : Calculate { public override double Result() { if (A == 0) { MessageBox.Show("分母不能为零"); } result = A % B; return result; } } public class calculate { public static Calculate Cal(string type) { Calculate cal = null; switch (type) { case "+": cal = new Add(); //加法策略模式 break; case "-": cal = new Sub(); break; case "*": cal = new Mul(); break; case "/": cal = new Div(); break; case "%": cal = new Mod(); break; default: break; } return cal; } } } 后台代码
protected void calcilate_Click(object sender, EventArgs e) { Calculate cal = null; string typ = DDLtype.SelectedValue.ToString();//调用类成员 cal = calculate.Cal(typ);//运算类型 cal.A = Convert.ToDouble(one.Text);//第一个数 cal.B = Convert.ToDouble(two.Text);//第二个数 string answer = Convert.ToString(cal.Result());//答案 if (result.Text == answer.ToString())//判断计算结果 { Response.Write("<script>alert(‘回答正确‘)</script>"); Panel1.GroupingText = one.Text + DDLtype.SelectedValue + two.Text + Label1.Text + result.Text;//将题放入panel中 one.Text = ""; two.Text = ""; result.Text = ""; DDLtype.SelectedValue = "请选择计算类型"; } else { Response.Write("<script>alert(‘回答错误‘)</script>"); one.Text = " "; two.Text = " "; result.Text = " "; DDLtype.SelectedValue = "请选择计算类型"; } Panel1.Visible = true; } 截图
时间: 2024-10-24 03:37:27