策略模式,ASP.NET实现

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///Class1 的摘要说明
/// </summary>
interface Iface
{
int Calation(int a, int b); }
public class Add:Iface
{
public int Calation(int a,int b)
{
return a+b;
}
}
public class Sub : Iface
{
public int Calation(int a, int b)
{
return a - b;
}
}
public class Mul : Iface
{
public int Calation(int a, int b)
{
return a * b;
}
}
public class Div : Iface
{
public int Calation(int a, int b)
{
if (b == )
{
throw new Exception("除数不能为零!"); } else
{
return a / b;
} }
}
public class Faction
{
private Iface iface;
public Faction(string operation)
{
switch (operation)
{
case "+":
iface = new Add();
break;
case "-":
iface = new Sub();
break;
case "*":
iface = new Mul();
break;
case "/":
iface = new Div();
break;
}
}
public int Calationss(int a, int b)
{
return iface.Calation(a, b); }
}

Default.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ti();
}
}
public void ti()
{
Random rd = new Random();
TextBox1.Text = rd.Next(, ).ToString();
TextBox2.Text = rd.Next(, ).ToString();
string[] oper = new string[] { "+", "-", "*", "/" };
Random rdd = new Random();
Label1.Text = oper[rdd.Next(, )]; }
protected void Button1_Click(object sender, EventArgs e)
{
Faction faction = new Faction(Label1.Text);
int a = int.Parse(TextBox1.Text);
int b = int.Parse(TextBox2.Text);
string anster = faction.Calationss(a, b).ToString();
if (TextBox3.Text == anster)
{
Response.Write("回答正确!");
}
else
{
Response.Write("回答错误!");
}
TextBox3.Text = "";
ti();
}
}

测试

第一题做的是不是正确,在第二题中提示。

最新文章

  1. proj.4 线程安全
  2. C标准头文件&lt;signal.h&gt;
  3. CSS层模型
  4. MyEclipse去除网上复制下来的来代码带有的行号
  5. 使用Lucene开发自己的搜索引擎
  6. 台大《机器学习基石》课程感受和总结---Part 2 (转)
  7. TFTP网络协议分析---15
  8. MVC和WebApi 使用get和post 传递参数。
  9. memcache分布式部署的原理分析
  10. centos网络安装中的注意点
  11. Adjacent Bit Counts(动态规划 三维的)
  12. if..endif 语法
  13. JSON互转
  14. Ubuntu 下一个disk清理保护
  15. Sublime 3 打造成 Python/Django IDE开发利器
  16. Problem C: 线性表的基本操作
  17. [OpenCV] 编译源程序 2.4.10 以支持 CUDA
  18. python3-列表字典简单练习题
  19. vuex数据持久化插件--指定持久化特定的值
  20. 设计模式のIteratorPattern(迭代器模式)----行为模式

热门文章

  1. wamp 的配置
  2. thinkphp5 toArray()报错
  3. Vi中进行多行指定内容替换
  4. Chip-seq peak annontation
  5. SAS中的宏语言
  6. nodejs module/require
  7. 优步UBER司机全国各地奖励政策汇总 (3月14日-3月20日)
  8. 北京Uber优步司机奖励政策(3月19日)
  9. Python破解压缩包密码问题
  10. 生产环境 tidb部署实践