标签:
fengzhuang.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; public abstract class fengzhuang { public abstract double Cal(double a, double b); } public class Add : fengzhuang { public override double Cal(double a, double b) { double result = 0; result = a + b; return result; } } public class Sub : fengzhuang { public override double Cal(double a, double b) { double result = 0; result = a - b; return result; } } public class Mul :fengzhuang { public override double Cal(double a, double b) { double result = 0; result = a * b; return result;
} } public class Div : fengzhuang { public override double Cal(double a, double b) { double result = 0; result = a / b; return result; } } public class Context { private fengzhuang calculate = null; public Context(Calculator _cal) { this.calculate = _cal; } public double Cal(double a, double b, String symbol) { return this.calculate.Cal(a, b); } }
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Cal_Click(object sender, EventArgs e)
{
string symbol = DropDownList1.SelectedItem.ToString();
double a = Convert.ToDouble(TextBox1.Text);
double b = Convert.ToDouble(TextBox2.Text);
Context contex = null;
if (DropDownList1.SelectedIndex == 1)
{
contex = new Context(new Add());
}
else if (DropDownList1.SelectedIndex == 2)
{
contex = new Context(new Sub());
}
else if (DropDownList1.SelectedIndex == 3)
{
contex = new Context(new Mul());
}
else if (DropDownList1.SelectedIndex == 4)
{
contex = new Context(new Div());
}
string answer = contex.Cal(a, b, symbol).ToString();
string result = TextBox1.Text + DropDownList1.SelectedItem.ToString() + TextBox2.Text;
if (TextBox3.Text == answer)
{
Response.Write("<script>alert(‘回答正确!‘)</script>");
ListBox1.Items.Add(result + "=" + TextBox3.Text.Trim());
}
else
{
Response.Write("<script>alert(‘答题错误!‘)</script>");
ListBox1.Items.Add(result + "=" + TextBox3.Text.Trim() );
}
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
}
总结:看来想做成功还得伙伴。要想走得快一个人走,但要想走得远还得伙伴挽着手一起走。有人帮忙就是好
标签:
原文地址:http://www.cnblogs.com/zj15517225953/p/5023903.html