策略模式,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();
}
}

测试

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

策略模式,ASP.NET实现的更多相关文章

  1. 计算器软件的代码实现 (策略模式+asp.net)

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  2. 计算器软件实现系列(五)策略模式+asp.net

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  3. ASP.net之策略模式

    设计思路: 用ASP.net设计,调用策略模式.在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判 ...

  4. 封装,策略模式,Asp换脸

    1.简单封装 1>计算类 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  5. ASP.NET四则运算--策略模式

    在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式.只不过是把封装的类.前台代码以及后台的代码分离开来,但同样是要达到功能的实现. Calculator.cs using System; ...

  6. asp.net core 集成JWT(二)token的强制失效,基于策略模式细化api权限

    [前言] 上一篇我们介绍了什么是JWT,以及如何在asp.net core api项目中集成JWT权限认证.传送门:https://www.cnblogs.com/7tiny/p/11012035.h ...

  7. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  8. asp.net—策略模式

    一.什么是策略模式 定义:定义一系列算法,把一个个算法封装成独立类并实现同一个接口,使得它们之间可以相互替换. 二.怎么使用策略模式 首先模拟一个场景:有一个用户想买车.  可以有多种方式买车: (1 ...

  9. 设计模式:策略模式(Strategy)

    定   义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...

随机推荐

  1. Tornado用户指引(一)-----------异步和非阻塞I/O

    摘要:异步和非阻塞I/O实时WEB的特性是经常需要为每个用户端维持一个长时间存活但是大部分时候空闲的连接.在传统的同步式web服务器中,这主要通过为每个用户创建一个线程来实现,这样的代价是十分昂贵的. ...

  2. [].slice.call的理解

    首先要说明[].slice.call()与Array.prototype.slice.call() 有什么区别? [].slice === Array.prototype.slice true []为 ...

  3. ON DUPLICATE KEY UPDATE 插入or更新

    mean:若数据表中存在以相同主键的记录,就更新该条记录.否则就插入一条新的记录. 单条:INSERT INTO tablename (`field1`,`field2`) VALUES(value1 ...

  4. 第5天 Java基础语法

    第5天 Java基础语法 今日内容介绍 方法 方法 方法概述 在我们的日常生活中,方法可以理解为要做某件事情,而采取的解决办法. 如:小明同学在路边准备坐车来学校学习.这就面临着一件事情(坐车到学校这 ...

  5. 论文翻译第二弹--用python(或Markdown)对论文复制文本进行处理

    图中这种论文你想进行文本复制放入翻译软件进行翻译时,会发现是这种形式: 句子之间是断开的,这时普遍的方法,也是我之前一直用的方法就是打开一个文档编辑器,复制上去后一行行地继续调整.昨天不想这样了,就打 ...

  6. C# 调用腾讯云接口获取视频基本信息

    做项目需要上传视频,获取时长,上传教程很多,获取信息很少,官方只有一条请求地址. 找了好久,都没有说这个请求地址怎么用.最后发现需要调用腾讯云SDK 官方地址:https://github.com/Q ...

  7. 破解Wifi

    牛刀小试:Wifi破解的原理. 准备工具:   1:Kali Linux系统 2:一块好用的无线网卡 (推荐免驱版,网上也有推荐,可以去百度上google一下) 3:WPA字典(用来爆破抓获的握手包) ...

  8. [原创]用python检测LVS real server状态实现HTTP高可用

    import httplib import os import time def check_http(i): try: conn=httplib.HTTPConnection(i, 80, time ...

  9. 网易七鱼 Android 高性能日志写入方案

    本文来自网易云社区 作者:网易七鱼 Android 开发团队 前言 网易七鱼作为一款企业级智能客服系统,对于系统稳定性要求很高,不过难保用户在使用中不会出现问题,而 Android SDK 安装在用户 ...

  10. Ruby 基础教程 1-2

    1.数组 创建 arrayname=[] arrayname=["1",12,"23"] 访问 arrayname[index] 更新 arrayname[in ...