ASP.net之策略模式
设计思路:
用ASP.net设计,调用策略模式。在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判断,进入下一题,同时提示回答是否正确。如果在时间内做完题就单击结束按钮,弹出对话框“答题结束” 总计,正确的个数及正确率显示出来。
页面设计代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO; public partial class one : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
public static int Count = ;//总计的个数
public static int right = ;//正确的个数
int n = ; protected void Button8_Click(object sender, EventArgs e)
{
fh.Text="+";
fh.Visible = true;
}
protected void Button9_Click(object sender, EventArgs e)
{
fh.Text = "-";
fh.Visible = true;
}
protected void Button10_Click(object sender, EventArgs e)
{
fh.Text = "*";
fh.Visible = true;
}
protected void Button11_Click(object sender, EventArgs e)
{
fh.Text = "/";
fh.Visible = true;
}
//以上四个分别是+,-,*,/的单击事件
protected void luti_Click(object sender, EventArgs e)//录题事件
{
StreamWriter n1 = File.AppendText("n1.txt");//第一个数存入第一文档
n1.WriteLine(No1.Text);
n1.Close();
StreamWriter n2 = File.AppendText("n2.txt");//第二个数存入第二个文档
n2.WriteLine(fh.Text);
n2.Close();
StreamWriter n3 = File.AppendText("n3.txt");//结果存入第三个文档
n3.WriteLine(No2.Text);
n3.Close();
No1.Text = "";//清空数一,数二,结果
No2.Text="";
No3.Text="";
Response.Write("录题成功");
}
protected void ready_Click(object sender, EventArgs e)//开始的单击事件
{ string[] n1 = new string[];
n1 = File.ReadAllLines("n1.txt");//数值一的文档
No1.Text = n1[n];
string[] n2 = new string[];
n2 = File.ReadAllLines("n2.txt");
fh.Text = n2[n];
string[] n3 = new string[];
n3 = File.ReadAllLines("n3.txt");
No2.Text = n3[n];
n++; }
protected void over_Click(object sender, EventArgs e)
{ No3.Enabled = false;
Response.Write("运算结束!");
all.Text = Count.ToString();//题目总数
rt.Text = right.ToString();//正确的个数
rt1.Text = ((right / (double)(Count)) * ).ToString() + "%";//正确率
}
protected void ifright_Click(object sender, EventArgs e)
{
Class1.mark mark = null;
double a = Convert.ToDouble(No1.Text);//第一个数赋值
double b = Convert.ToDouble(No2.Text);//第二个数赋值
string c = fh.Text;//运算符号
switch (c)
{
case "+":
mark = new Class1.mark(new Class1.add());//调用策略模式
break;
case "-":
mark = new Class1.mark(new Class1.sub());
break;
case "*":
mark = new Class1.mark(new Class1.mul());
break;
case "/":
mark = new Class1.mark(new Class1.div());
break; default:
break;
}
string result = mark.Call(a, b,c).ToString();
if (No3.Text == result.ToString())
{
Response.Write("回答正确!下一题请按开始按钮!");
right++;
Count++;
} else
{ Response.Write("回答错误!下一题请按开始按钮!");
Count++; }
//清空
No3.Style.Clear();
}
}
策略模式代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///Class1 的摘要说明
/// </summary>
public class Class1
{ //定义一个接口
public interface Calculator
{
double Call(double a, double b);//定义一个方法用于计算
}
private double a;
private double b;//定义变量
public class add : Calculator
{
public double Call(double a, double b)
{
double result;
result = a + b;
return result;
}
}
public class sub : Calculator
{
public double Call(double a, double b)
{
double result;
result = a - b;
return result;
}
}
public class mul : Calculator
{
public double Call(double a, double b)
{
double result;
result = a * b;
return result;
}
}
public class div : Calculator
{
public double Call(double a, double b)
{
double result;
result = a / b;
return result;
}
}
public class mark
{
private Calculator calculate; public mark(Calculator calculate)
{
this.calculate = calculate;
}
public double Call(double a, double b, string m)
{
return this.calculate.Call(a, b);
}
}
}
运行后的图片:



ASP.net之策略模式的更多相关文章
- ASP.NET四则运算--策略模式
在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式.只不过是把封装的类.前台代码以及后台的代码分离开来,但同样是要达到功能的实现. Calculator.cs using System; ...
- 封装,策略模式,Asp换脸
1.简单封装 1>计算类 using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 计算器软件的代码实现 (策略模式+asp.net)
一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...
- 策略模式,ASP.NET实现
策略模式,ASP.NET实现 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- 计算器软件实现系列(五)策略模式+asp.net
一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...
- asp.net core 集成JWT(二)token的强制失效,基于策略模式细化api权限
[前言] 上一篇我们介绍了什么是JWT,以及如何在asp.net core api项目中集成JWT权限认证.传送门:https://www.cnblogs.com/7tiny/p/11012035.h ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- asp.net—策略模式
一.什么是策略模式 定义:定义一系列算法,把一个个算法封装成独立类并实现同一个接口,使得它们之间可以相互替换. 二.怎么使用策略模式 首先模拟一个场景:有一个用户想买车. 可以有多种方式买车: (1 ...
- 设计模式:策略模式(Strategy)
定 义:它定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化, 不会影响到使用算法的客户. 示例:商场收银系统,实现正常收费.满300返100.打8折.......等不同收费 ...
随机推荐
- Mittag-Leffler定理,Weierstrass因子分解定理和插值定理
Mittag-Leffler定理 设$D\subset\mathbb C$为区域,而$\{a_{n}\}$为$D$中互不相同且无极限点的点列,那么对于任意给定的一列自然数$\{k_{n}\}$, ...
- 高性能缓存系统Redis安装与使用
在互联网后台架构中,需要应付高并发访问数据库,很多时候都会在数据库上层增加一个缓存服务器来保存经常读写的数据以减少数据库压力,可以使用LVS.Memcached或Redis,Memcached和Red ...
- H5(三)
Canvas(画布) 基本内容 简单来说,HTML5提供的新元素<canvas> Canvas在HTML页面提供画布的功能 在画布中绘制各种图形 C ...
- Oracle SQL的硬解析和软解析
我们都知道在Oracle中每条SQL语句在执行之前都需要经过解析,这里面又分为软解析和硬解析.在Oracle中存在两种类型的SQL语句,一类为 DDL语句(数据定义语言),他们是从来不会共享使用的,也 ...
- linux 下UGet闪退问题
安装UGet,开始使用正常,后来打开时会闪退,估计是软件配置错误,但软件重装也没用,用dpkg --purge也无法删除配置文件. 后来想到是在下载eclipse时,将eclipse文件删除,导致软件 ...
- [翻译]Telnet简单介绍及在windows 7中开启Telnet客户端
文章翻译自 http://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-clien ...
- HashMap两种遍历方式的深入研究
转自:http://swiftlet.net/archives/1259 HashMap的遍历有两种方式,如下所示:第一种利用entrySet的方式: 1 2 3 4 5 6 7 Map map ...
- 进击的Python【第三章】:Python基础(三)
Python基础(三) 本章内容 集合的概念与操作 文件的操作 函数的特点与用法 参数与局部变量 return返回值的概念 递归的基本含义 函数式编程介绍 高阶函数的概念 一.集合的概念与操作 集合( ...
- <HTML>菜鸟入门基础须知
将持续更新-- 一,基础常用标签and属性 既然要学习这门知识,那必须得先知道这是什么能做什么,HTML:是一种超文本标记语言,什么意思呢,我拆开看一下,超(超链接)文本(犹如TXT)标记(改变成自己 ...
- javascript的对象
简介: JavaScript 中的所有事物都是对象:字符串.数字.数组.日期,等等.在 JavaScript 中,对象是拥有属性和方法的数据. 一.对象的类型 本地对象:就是ECMA定义好的一些对象, ...