设计思路:

用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之策略模式的更多相关文章

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

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

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

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

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

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

  4. 策略模式,ASP.NET实现

    策略模式,ASP.NET实现 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

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

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

  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. ajax循环读取json多维数组

    test.json: { "one": [ { "name": "黑默丁格", "car": "鲁LLL608 ...

  2. CrashMonkey4IOS App测试

    下载地址:https://github.com/vigossjjj/CrashMonkey4IOS 根据下载地址里面的说明安装一下,以下进行配置 1.进入CrashMonkey4IOS-master/ ...

  3. sqlserver 中数据导入到mysql中的方法以及注意事项

    数据导入从sql server 到mysql (将数据以文本格式从sqlserver中导出,注意编码格式,再将文本文件导入mysql中): 1.若从slqserver中导出的表中不包含中文采用: bc ...

  4. 名词含义阅读 todolist

    1.node webkit 2.C#设计模式 3.算法导论 4.SQLSERVER RowNum() 5.图片文字识别 6.tuple 7.yield 8.Web语义化 (多用 p ul ol li ...

  5. Python 修改电脑DNS

    Pc电脑一般连网都是动态DHCP获取局域的IP地址,而且DNS服务器为自动获取地址.要修改DNS就要打开本地网络-本地连接- 属性- TCP/IP 手动修改DNS. 其实Python也可以通过WMI接 ...

  6. 数学系列:XXX

    Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  7. SQLServer2005如何批量修改架构名 - wuxiaokaixinguo的专栏

    原文地址:http://blog.csdn.net/wuxiaokaixinguo/article/details/8523093 ) BEGIN SET @name='原构架名.' + @name ...

  8. 关于handler 和 looper 的问题

    重新去学习回顾looper和handler ,还是需要重新认识这个经常使用的机制. 我首先是看任玉刚老师的书<android的开发艺术探索>的第十章. 里面一句话开始说出了我们大概的理解— ...

  9. mysql存不了中文的解决办法

    driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEnco ...

  10. web-inf目录和meta-inf目录

    /WEB-INF/web.xml Web应用程序配置文件,描述了 servlet 和其他的应用组件配置及命名规则. /WEB-INF/classes/ 包含了站点所有用的 class 文件,包括 se ...