您的位置:首页 > 编程语言 > ASP

ASP.net之策略模式

2015-12-19 13:25 465 查看
设计思路:

用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);
}
}
}


运行后的图片:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: