策略模式
2015-12-06 17:12
288 查看
fengzhuang.cs
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 = "";
}
}
总结:看来想做成功还得伙伴。要想走得快一个人走,但要想走得远还得伙伴挽着手一起走。有人帮忙就是好
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 = "";
}
}
总结:看来想做成功还得伙伴。要想走得快一个人走,但要想走得远还得伙伴挽着手一起走。有人帮忙就是好
相关文章推荐
- Android SQlite
- 雷观(二十四):谈谈我对国家事务“二胎”和“教育”的一些看法
- 如何用Python实现八大排序算法
- c++动态联编与静态联编
- ELK 日志分析平台搭建
- 雷观(二十四):谈谈我对国家事务“二胎”和“教育”的一些看法
- 雷观(二十四):谈谈我对国家事务“二胎”和“教育”的一些看法
- 主键约束和唯一性约束的区别
- hdu 5529 ZYB's Premutation
- oops
- 博客终于上线了,好高兴!
- 织梦5.7bug修复:后台联动不能添加二级栏目的解决
- RFID 知识的学习
- 第十二章 并发编程
- hdu 1285 确定比赛名次
- 美团技术相关学习
- NOIP2015 day2t2 子串
- Android学习问题
- [置顶] Shrio登陆验证实例详细解读
- 联动菜单:javaScript