码迷,mamicode.com
首页 > Web开发 > 详细

ASP.net之策略模式

时间:2015-12-19 13:44:55      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

设计思路:

用ASP.net设计,调用策略模式。在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判断,进入下一题,同时提示回答是否正确。如果在时间内做完题就单击结束按钮,弹出对话框“答题结束” 总计,正确的个数及正确率显示出来。

页面设计代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Web;
  5 using System.Web.UI;
  6 using System.Web.UI.WebControls;
  7 using System.IO;
  8 
  9 public partial class one : System.Web.UI.Page
 10 {
 11     protected void Page_Load(object sender, EventArgs e)
 12     {
 13 
 14     }
 15     public static int Count = 0;//总计的个数
 16     public static int right = 0;//正确的个数
 17     int n = 0;
 18     
 19     protected void Button8_Click(object sender, EventArgs e)
 20     {  
 21         fh.Text="+";
 22         fh.Visible = true;
 23     }
 24     protected void Button9_Click(object sender, EventArgs e)
 25     {
 26         fh.Text = "-";
 27         fh.Visible = true;
 28     }
 29     protected void Button10_Click(object sender, EventArgs e)
 30     {
 31         fh.Text = "*";
 32         fh.Visible = true;
 33     }
 34     protected void Button11_Click(object sender, EventArgs e)
 35     {
 36         fh.Text = "/";
 37         fh.Visible = true;
 38     }
 39   //以上四个分别是+,-,*,/的单击事件
 40     protected void luti_Click(object sender, EventArgs e)//录题事件
 41     {
 42         StreamWriter n1 = File.AppendText("n1.txt");//第一个数存入第一文档
 43         n1.WriteLine(No1.Text);
 44         n1.Close();
 45         StreamWriter n2 = File.AppendText("n2.txt");//第二个数存入第二个文档
 46         n2.WriteLine(fh.Text);
 47         n2.Close();
 48         StreamWriter n3 = File.AppendText("n3.txt");//结果存入第三个文档
 49         n3.WriteLine(No2.Text);
 50         n3.Close();
 51         No1.Text = "";//清空数一,数二,结果
 52         No2.Text="";
 53         No3.Text="";
 54         Response.Write("录题成功");
 55     }
 56     protected void ready_Click(object sender, EventArgs e)//开始的单击事件
 57     {
 58       
 59         string[] n1 = new string[100];
 60         n1 = File.ReadAllLines("n1.txt");//数值一的文档
 61         No1.Text = n1[n];
 62         string[] n2 = new string[100];
 63         n2 = File.ReadAllLines("n2.txt");
 64         fh.Text = n2[n];
 65         string[] n3 = new string[100];
 66         n3 = File.ReadAllLines("n3.txt");
 67         No2.Text = n3[n];
 68         n++;
 69        
 70     }
 71     protected void over_Click(object sender, EventArgs e)
 72     {
 73 
 74         No3.Enabled = false;
 75        Response.Write("运算结束!");
 76         all.Text = Count.ToString();//题目总数
 77        rt.Text = right.ToString();//正确的个数
 78        rt1.Text = ((right / (double)(Count)) * 100).ToString() + "%";//正确率
 79     }
 80     protected void ifright_Click(object sender, EventArgs e)
 81     {
 82         Class1.mark mark = null;
 83         double a = Convert.ToDouble(No1.Text);//第一个数赋值
 84         double b = Convert.ToDouble(No2.Text);//第二个数赋值
 85         string c = fh.Text;//运算符号
 86         switch (c)
 87         {
 88             case "+":
 89                 mark = new Class1.mark(new Class1.add());//调用策略模式
 90                 break;
 91             case "-":
 92                 mark = new Class1.mark(new Class1.sub());
 93                 break;
 94             case "*":
 95                 mark = new Class1.mark(new Class1.mul());
 96                 break;
 97             case "/":
 98                 mark = new Class1.mark(new Class1.div());
 99                 break;
100 
101             default:
102                 break;
103         }
104         string result = mark.Call(a, b,c).ToString();
105         if (No3.Text == result.ToString())
106         {
107             Response.Write("回答正确!下一题请按开始按钮!");
108             right++;
109             Count++;
110         }
111 
112         else
113         {
114 
115             Response.Write("回答错误!下一题请按开始按钮!");
116             Count++;
117 
118         }
119         //清空
120         No3.Style.Clear();
121     }
122 }

策略模式代码:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 /// <summary>
 7 ///Class1 的摘要说明
 8 /// </summary>
 9 public class Class1
10 {
11   
12     //定义一个接口
13         public interface Calculator
14         {
15             double Call(double a, double b);//定义一个方法用于计算
16         }
17           private double a;
18           private double b;//定义变量
19         public class add : Calculator
20         {
21             public double Call(double a, double b)
22             {
23                 double result;
24                 result = a + b;
25                 return result;
26             }
27         }
28         public class sub : Calculator
29         {
30             public double Call(double a, double b)
31             {
32                 double result;
33                 result = a - b;
34                 return result;
35             }
36         }
37         public class mul : Calculator
38         {
39             public double Call(double a, double b)
40             {
41                 double result;
42                 result = a * b;
43                 return result;
44             }
45         }
46         public class div : Calculator
47         {
48             public double Call(double a, double b)
49             {
50                 double result;
51                 result = a / b;
52                 return result;
53             }
54         }
55         public class mark
56         {
57             private Calculator calculate;
58 
59             public mark(Calculator calculate)
60             {
61                 this.calculate = calculate;
62             }
63             public double Call(double a, double b, string m)
64             {
65                 return this.calculate.Call(a, b);
66             }
67         }
68     }

运行后的图片:

技术分享

技术分享

技术分享

ASP.net之策略模式

标签:

原文地址:http://www.cnblogs.com/xueyanan/p/5059016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!