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四则运算《《《策略模式的更多相关文章

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

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

  2. ASP.NET四则运算--工厂模式

    这次是在ASP.NET上实现四则运算,之前用策略模式实现了,所以这次想着用工厂模式实现一下. Calculator.cs using System; using System.Collections. ...

  3. ASP.net之策略模式

    设计思路: 用ASP.net设计,调用策略模式.在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判 ...

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

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

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

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

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

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

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

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

  8. asp.net core 集成JWT(二)token的强制失效,基于策略模式细化api权限

    [前言] 上一篇我们介绍了什么是JWT,以及如何在asp.net core api项目中集成JWT权限认证.传送门:https://www.cnblogs.com/7tiny/p/11012035.h ...

  9. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  10. asp.net—策略模式

    一.什么是策略模式 定义:定义一系列算法,把一个个算法封装成独立类并实现同一个接口,使得它们之间可以相互替换. 二.怎么使用策略模式 首先模拟一个场景:有一个用户想买车.  可以有多种方式买车: (1 ...

随机推荐

  1. java面向对象之个人总结

    面向对象有三大特性:继承,封装,多态 1.继承: (1)继承的特点:A,java支持单根继承,不支持多根继承 B,java支持多层继承(继承体系) (2)细节注意:A.子类只能继承父类的非私有成员(成 ...

  2. JOOQ快速上手(基于springboot 和 postgresql)

    是什么 全称Java Object Oriented Querying,基于java开发出来的工具包,主要用于访问关系型数据库. 为什么用 Hibernate对SQL的操作太抽象 JDBC使用太过繁琐 ...

  3. 20155236 2016-2017-2 《Java程序设计》第九周学习总结

    20155236 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC入门 1.JDBC简介 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标 ...

  4. 2017-2018-1 20155306 《信息安全系统设计基础》嵌入式C语言———提取设置时分秒

    2017-2018-1 20155306 <信息安全系统设计基础>嵌入式C语言---提取设置时分秒 要求:根据下图,完成对时分秒的设置和提取. 示例及思路分析: 思路分析:以分钟为例,根据 ...

  5. 【转载】MFC的Main函数跑哪去了

    原文:http://blog.csdn.net/weiwenhp/article/details/8455471 习惯的思维 用习惯了C的人要看一个程序时首先会想到找到那个main函数在哪,然后再顺着 ...

  6. 02-分页器,自定义分页器,解耦函数分页器,分页器class

    1 .批量数据导入 主url from django.contrib import admin from django.urls import path, re_path, include urlpa ...

  7. 11 stark组件之delete按钮、filter过滤

    1.构建批量删除按钮 1.admin中每个页面默认都有 2.stark之构建批量删除 3.coding {% extends 'base.html' %} {% block title %} < ...

  8. CF 1138 F. Cooperative Game

    F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...

  9. Gitlab+Jenkins学习之路(二)之gitlab部署

    1.安装依赖及gitlab [root@linux-node1 ~]# yum install -y curl policycoreutils openssh-server openssh-clien ...

  10. spring在service层获取session和request

    首先要在web.xml增加如下代码: <listener> <listener-class>org.springframework.web.context.request.Re ...