策略模式,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. js滚动监听

    下边代码,是监听滚动条只要移动,下方的返回顶部的div显示与隐藏的代码 ? 1 2 3 4 5 6 7 8 window.onscroll = function () {  var t = docum ...

  2. 分享一个hybrid框架ionic

    ionic 是一个 HTML5 应用程序开发框架. 可以使用 HTML.CSS 和 Javascript 构建接近原生体验的移动应用程序.具有速度快,界面现代化.美观等特点.下面一起看一下如何使用 安 ...

  3. [翻译]Hystrix wiki–How it Works

    注:本文并非是精确的文档翻译,而是根据自己理解的整理,有些内容可能由于理解偏差翻译有误,有些内容由于是显而易见的,并没有翻译,而是略去了.本文更多是学习过程的产出,请尽量参考原官方文档. 流程图 下图 ...

  4. PHP批量清理MIP-cache缓存(内附在线mipcache清理工具)

    MIP是什么?我就不多说了把. MIPCache 又是什么? 科普一下:MIPCache 是一套基于代理的 CDN 缓存系统.可用于缓存所有被某度相关页面引用或者从百度相关服务点出的 MIP 页面.当 ...

  5. python remove跟pop的区别

    remove根据值来删除 li=[1,2,3,4] li.pop[0] li.remove['] 或者是a=li[-1]li.remove(a) pop是根据索引来删除

  6. python学习笔记:第16天 面向对象02--对象中的成员

    目录 ⼀.类的成员介绍: 二.类的成员-变量 三.类的成员-方法 四.类的成员-属性 五.私有属性 ⼀.类的成员介绍: ⾸先, 什么是类的成员. 很简单. 我么能在类中写什么? 写的内容就是成员. 到 ...

  7. django-models 数据库取值

    django.shortcuts import render,HttpResponse from app01.models import * # Create your views here. def ...

  8. 关于 ssh 连接较慢的解决办法

    1. 概述 使用 ssh 软件连接 linux 时, 会碰到很慢的情况 2. 场景 概述 ssh 连接很慢 大概步骤 使用 xshell6 免费版, 连接 主机 一个小等待, 弹出 用户名 输入框 输 ...

  9. shell之lvm

    #!/bin/bash #this script for LVM echo "Initial a disk..."  echo -e "\033[31mWarning: ...

  10. SQL条件判断中字符串后面有空格的问题

    也不知何时才有的概念,还是以前一直没有注意,从哪也没有听说过的定义,今天又遇见了一个小坑,特记录下来,防止再陷坑! 才疏学浅,文笔有限,简单点说吧,就是在写SQL Server语句时,以前使用了 WH ...