ASP.net之策略模式
2015-12-19 13:25
465 查看
设计思路:
用ASP.net设计,调用策略模式。在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判断,进入下一题,同时提示回答是否正确。如果在时间内做完题就单击结束按钮,弹出对话框“答题结束” 总计,正确的个数及正确率显示出来。
页面设计代码如下:
策略模式代码:
运行后的图片:
用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 = 0;//总计的个数 public static int right = 0;//正确的个数 int n = 0; 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[100]; n1 = File.ReadAllLines("n1.txt");//数值一的文档 No1.Text = n1 ; string[] n2 = new string[100]; n2 = File.ReadAllLines("n2.txt"); fh.Text = n2 ; string[] n3 = new string[100]; n3 = File.ReadAllLines("n3.txt"); No2.Text = n3 ; 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)) * 100).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 MVC3 局部页面@RENDERBODY @RENDERPAGE@RENDERSECTION使用方法详细说明
- 防止站外提交
- ASP.NET 大文件下载的实现思路及代码
- 了解ASP.NET5 Web应用程序结构
- ASP.NET 使用MVC4的EF5 Code First 入门(一):创建数据库
- ASP.NET 页面访问控制
- ASP.NET 生命周期及管道事件
- ASP.NET发布WebService
- aspcms标签使用经验
- aspose 解决插入html后字体问题
- JqGrid知识总结
- 使用asp.net mvc里面的area让网站更有条理
- .NET中<asp:MultiView>选项卡控件的用法
- asp.net 页面导出EXCEL
- ASP.NET中Web.Config配置文件详解与应用
- [转]asp.net解决高并发的方案.
- 我学ASP.Net---新手(分页显示)
- ASP.NET SingalR 点对点聊天实现思路总结
- iis 7 asp.net ajax post 请求字节过大报错问题解决办法
- [Python爬虫] 在Windows下安装PhantomJS和CasperJS及入门介绍(上)