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折.......等不同收费 ...
随机推荐
- centos7.0 下安装jdk1.8
centos7.0这里安装jdk1.8采用yum安装方式,非常简单. 1.查看yum库中jdk的版本 [root@localhost ~]# yum search java|grep jdk 2.选择 ...
- postgres索引创建、 存储过程的创建以及在c#中的调用
postgres创建索引参考 http://www.cnblogs.com/stephen-liu74/archive/2012/05/09/2298182.html CREATE TABLE tes ...
- LeetCode之263. Ugly Number
------------------------------------------------------------- 如果一个数的质因子只包括2,3,5,那么这个数n可以表示为:n=2x+3y+ ...
- startUML破解方式
StarUML官方下载地址:http://staruml.io/download StarUML是一个非常好用的画UML图的工具,但是它是收费软件,以下是破解方法: 1.使用Editplus或者N ...
- Git版本控制管理学习笔记5-提交
这个标题其实有些让人费解,因为会想这个提交是动词还是名称? 提交动作是通过git commit命令来实现的,提交之后会在对象库中新增一个提交对象.提交过程中会发生哪些变化,在上一篇笔记 ...
- 【转】Android 底层开发的几点
我干了3年Android sdk开发,觉得到了瓶劲没法更进一步,于是花了一年多点时间,大概摸到点门径.根据前辈的经验,Android底层完全入门需要两年. 先说下我的入门过程:第零步,下载源码,我下的 ...
- StartCom 申请 SSL 证书及 Nginx HTTPS 支持配置全攻略
来源:https://www.williamyao.com/index.php/archives/1397/ 前言 最近收到 StartCom 的邮件,数字证书即将过期,想到去年在 StartSSL ...
- iOS之 清理缓存
作为一个开发者,对于缓存的清理也是理所应当的需要的.这次就简单的谈一下iOS中对于缓存的清理方法. 我们清理缓存通常是在这三种方式下进行的: (1)项目中的清理缓存按钮 (2)点击退出app按钮时清理 ...
- iOS Hit-Test应用
最近又看了遍苹果的官方文档<Event Handling Guide for iOS>,对事件响应链中的hit-test view 又多了些理解,个人觉的官方文档对这块讲的非常简单,很多东 ...
- XSL学习笔记4 XSLT模式匹配的语法
模板规则通过使用模式来匹配文档树的节点.模式指定一组条件,用于选择要处理的节点. 模式匹配的语法不光可以在<xsl:template>元素的match属性中使用,还可以在<xsl ...