策略模式,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. 了解jQuery并掌握jQuery对象和DOM对象的区别

    jQuery的优势: 开源--开放源代码 轻量级 强大的选择器 出色的DOM操作(对DOM元素的一个增删改查) 完善的Ajax,出色的浏览器兼容性,丰富的插件支持,完善的文档(说明书) 链式操作方式, ...

  2. iOS 打包常见问题处理

    Cannot proceed with delivery: an existing transporter instance is currently uploading this package 原 ...

  3. qq空间认证教程:借助企鹅媒体平台认证QQ公众空间

    年轻人,最近你是否眼看众多新开的QQ空间认证成功,自己却一筹莫展,而心情极度狂躁焦虑,别急,以下是详细教程,不能保证100%,但老夫已认证成功几个号. 不废话,直接上流程: 方法大致有2种: 1. 通 ...

  4. apache上.htaccess转向nginx上配置.htaccess伪静态规则

    nginx上配置.htaccess伪静态规则 在apache上.htaccess转向,只要apache编译的时候指明支持rewrite模块即可. 但是换到nginx上方法会有不同,有人说把.htacc ...

  5. 第6天 Java基础语法

    第6天 Java基础语法 今日内容介绍 自定义类 ArrayList集合 引用数据类型(类) 引用数据类型分类 提到引用数据类型(类),其实我们对它并不陌生,如使用过的Scanner类.Random类 ...

  6. 嘿,C语言(持续更新中...)

    ---恢复内容开始--- 上次简单介绍了一下C语言,这次说说数据与计算程序,那么话不多说,进来看看. 第二章      数据与简单的计算程序 一:数据 既然说到了数据,那么说说什么是写数据呢? 表面意 ...

  7. NUCLEO-L053R8 TIM定时器 PWM输出

    TIM2 PWM输出测试 今天给大伙分享一个TIM2 PWM输出小实验. 实验开发板:Nucleo-L053R8,即STM32L053R8T6. 开发环境:MDK5 图1 - 工程界面 本次实验测试的 ...

  8. Hadoop端口

    本文转自:<Hadoop默认端口应用一览> Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边 ...

  9. 20145226夏艺华 逆向及Bof基础实践

    逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串,如图所示: 该程序同时 ...

  10. windows 设置tomcat为自动启动服务

    1.下载免安装tomcat包,解压 2.配置环境变量: 点击新建,创建一个 变量名为:CATALINA_HOME 变量值为:tomcat解压文件的位置, 例如     F:\apache-tomcat ...