ASP.net四则运算《《《策略模式
Calculator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///Calculator 的摘要说明
/// </summary>
public abstract class Calculator
{
public abstract double Cal(double x, double y);
} public class Add : Calculator //接口法运算
{
public override double Cal(double x, double y) //重写
{
double result = ;
result = x + y;
return result;
}
}
public class Sub : Calculator
{
public override double Cal(double x, double y)
{
double result = ;
result = x - y;
return result;
}
}
public class Mul : Calculator
{
public override double Cal(double x, double y)
{
double result = ;
result = x * y;
return result;
}
}
public class Div : Calculator
{
public override double Cal(double x, double y)
{
double result = ;
result = x / y;
return result;
}
}
public class Opear //定义运算符
{
private Calculator calculate = null; //实例化一个基类的引用对象
public Opear(Calculator calculator) //calculator为派生类的一个对象
{
this.calculate = calculator; //把派生类的对象赋给基类的引用对象
}
public double Cal(double x, double y, String op)
{
return this.calculate.Cal(x, y); //返回计算结果
}
}
ASP.net后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string op = DropDownList1.SelectedItem.ToString();
double x = Convert.ToDouble(TextBox1.Text);
double y = Convert.ToDouble(TextBox2.Text); Opear opear =null;
if (DropDownList1.SelectedIndex == )
{
opear = new Opear(new Add());
}
else if (DropDownList1.SelectedIndex == )
{
opear = new Opear(new Sub());
}
else if (DropDownList1.SelectedIndex == )
{
opear = new Opear(new Mul());
}
else if (DropDownList1.SelectedIndex == )
{
opear = new Opear(new Div());
}
string answer = opear.Cal(x, y, op).ToString();
string result = TextBox1.Text + DropDownList1.SelectedItem.ToString() + TextBox2.Text;
if (TextBox4.Text == answer)
{
Response.Write("<script>alert('回答正确!')</script>");
ListBox1.Items.Add(result + "=" + TextBox4.Text.Trim());
} else
{
Response.Write("<script>alert('答题错误!')</script>");
ListBox1.Items.Add(result + "=" + TextBox4.Text.Trim());
}
TextBox1.Text = "";
TextBox2.Text = "";
TextBox4.Text = "";
}
}
运行结果:
运行界面:



ASP.net四则运算《《《策略模式的更多相关文章
- ASP.NET四则运算--策略模式
在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式.只不过是把封装的类.前台代码以及后台的代码分离开来,但同样是要达到功能的实现. Calculator.cs using System; ...
- ASP.NET四则运算--工厂模式
这次是在ASP.NET上实现四则运算,之前用策略模式实现了,所以这次想着用工厂模式实现一下. Calculator.cs using System; using System.Collections. ...
- ASP.net之策略模式
设计思路: 用ASP.net设计,调用策略模式.在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判 ...
- 封装,策略模式,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 ...
随机推荐
- EatWhat 实用的APP
EatWhat最近比较闲开始学习煮粥,买了好多米和豆子,每天煮的都不一样,试了几天发现有的时候记不住要煮什么粥,所以就写了这个APP.每天煮粥的时候拿出来看看就可以了,再也不用思考半天要煮什么粥了. ...
- iview 或 element-ui table 列表表头加样式
table 表头有时候需要加一些小样式比如 必填项 这是我项目中遇到的需求=== 比例,产品, 部门为必填项,这个时候就需要在表头加个红色小星星. 首先在table中绑定:header-cell-c ...
- mysql/mariadb将选择查询的结果重新生成一张新表格
比如想要生成类似如下的表格 mysql> select student.*,sc.cno,course.cname,sc.grade,course.cpno,course.ccredit fro ...
- linux 学习第十八天学习(DNS分离解析、DHCP配置、邮件服务配置)
DNS分离解析技术 yum install bind-chroot systemctl restart named systemctl enable named vim /etc/named.conf ...
- PHP SHA1withRSA加密生成签名及验签
最近公司对接XX第三方支付平台的代付业务,由于对方公司只有JAVA的demo,所以只能根据文档自己整合PHP的签名加密,网上找过几个方法,踩到各种各样的坑,还好最后算是搞定了,话不多说,代码分享出来. ...
- IOLI crackme分析——从应用中学习使用radare2
Crackme0x00 - writeup 我现在开始看radare2book了,现在刚看1/3,有些无聊,因为之前也看过一些radare2的实例讲解,所以现在先试着做一下里面的crackme练习. ...
- MongoDB操作-备份和恢复,导入和导出
mongodb数据备份和恢复主要分为二种:一种是针对库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoimport 1. 常用命令格式 mongod ...
- 洛咕 P3645 [APIO2015]雅加达的摩天楼
暴力连边可以每个bi向i+kdi连边权是k的边. 考虑这样的优化: 然后发现显然是不行的,因为可能还没有走到一个dog的建筑物就走了这个dog的边. 然后就有一个很妙的方法--建一个新的图,和原图分开 ...
- JAVAMAIL 530 Error: A secure connection is requiered(such as ssl)
原因:代码没有开启ssl传输 添加: props.put("mail.smtp.ssl.enable", "true"); // 设置是否 ...
- TortoiseGit版本库中某个文件显示问号或叹号的问题解决办法
这是一个怪问题,原因就是文件名大小写与版本库管理的大小写不一致. 解决办法: 1.先把文件夹中的物理文件名改为版本库浏览器中显示的文件名(版本库浏览器中的文件名不知道怎么改),改了以后这个文件图标就变 ...