C#四则运算之策略模式
Calculator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace stratage
{
public abstract class Calculator //抽象类 Calculator
{
public abstract double Cal(double a, double b); //抽象方法Cal
}
public class Add : Calculator //派生类Add继承抽象类Calculator
{
public override double Cal(double a, double b)//并重写了抽象方法Cal
{
;
result = a + b;
return result;
}
}
public class Sub : Calculator
{
public override double Cal(double a, double b)
{
;
result = a - b;
return result;
}
}
public class Mul : Calculator
{
public override double Cal(double a, double b)
{
;
result = a * b;
return result;
}
}
public class Div : Calculator
{
public override double Cal(double a, double b)
{
;
result = a / b;
return result;
}
}
public class Context //上下文
{
private Calculator calculate = null;//实例化一个基类的引用对象
public Context(Calculator _cal)//_cal为派生类的一个对象
{
this.calculate = _cal; //把派生类的对象赋给基类的引用对象
}
public double Cal(double a, double b, String symbol)
{
return this.calculate.Cal(a, b);//返回计算结果
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace stratage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
;
;
public static String AddSymbol = "+"; //加法
public static String SubSymbol = "-"; //减法
public static String MulSymbol = "*"; //乘法
public static String DivSymbol = "/"; //除法
private void textBox4_KeyDown(object sender, KeyEventArgs e)
{
double a = double.Parse(textBox1.Text); //用来存第一个数
string symbol = textBox2.Text; //用来存运算符
double b = double.Parse(textBox3.Text); //用来存第二个数
Context contex = null; //上下文
if (symbol.Equals(AddSymbol)) //若为加号
{
contex = new Context(new Add()); //加法策略
}
else if (symbol.Equals(SubSymbol)) //如果减号
{
contex = new Context(new Sub()); //减法策略
}
else if (symbol.Equals(MulSymbol)) //若为乘号
{
contex = new Context(new Mul()); //乘法策略
}
else if (symbol.Equals(DivSymbol)) //若为除号
{
contex = new Context(new Div()); //除法策略
}
string answer = contex.Cal(a, b, symbol).ToString(); //用answer来存计算出来的答案,此时已经计算出a,b两个数的运算结果。
if (e.KeyCode == Keys.Enter) //回车操作
{
string result = textBox1.Text + textBox2.Text + textBox3.Text;//把运算式子存在result里面
Count++; //出题总数加一
if (textBox4.Text == answer) //如果输入答案与计算出的answer相等
{
MessageBox.Show("回答正确!"); //弹出回答正确
listBox1.Items.Add(result + "=" + textBox4.Text.Trim() + "√");//并把运算式子存在listbox里
Right++; //答对题数加一
}
else //如果答错
{
MessageBox.Show("答题错误!"); //弹出答题错误
listBox1.Items.Add(result + "=" + textBox4.Text.Trim() +"×");//同样把运算式子放在listbox
}
label3.Text = ).PadRight(, , ) + "%";//统计正确率
textBox1.Clear();//把文本框清空,进行下一次出题
textBox3.Clear();
textBox4.Clear();
}
}
private void button1_Click(object sender, EventArgs e)//保存按钮,把已答题目放在txt文件里
{
sfd.Filter = "(*.txt)|*.txt";
if (sfd.ShowDialog() == DialogResult.OK)
{
string sPath = sfd.FileName;
FileStream fs = new FileStream(sPath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
;
; i <= iCount; i++)
{
sw.WriteLine(listBox1.Items[i].ToString());
}
sw.Flush();
sw.Close();
fs.Close();
}
}
}
}
测试截图:



C#四则运算之策略模式的更多相关文章
- ASP.NET四则运算--策略模式
在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式.只不过是把封装的类.前台代码以及后台的代码分离开来,但同样是要达到功能的实现. Calculator.cs using System; ...
- 策略模式(Strategy)
行为型模式:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式.命令模式.备忘录模式.状态模式.访问者模式.中介者模式.解释器模式 策略模式(Strategy) 策略模式定义了一系列算法,并将 ...
- 设计模式のStrategyPattern(策略模式)----行为模式
一.问题产生背景 当我们进行一系列处理时(员工工资核算,会员管理,计算器,优惠活动),会有很多相似的算法和处理过程,只是由于具体的算法的差异,导致必须不同处理.这些处理和客户端无关,我们可以把这些算法 ...
- javascript设计模式:策略模式
前言 策略模式有效利用组合.委托.多态等技术和思想,可以有效避免多重条件选择语句. 策略模式对开放-封闭原则提供了很好的支持,将算法封装在strategy中,使得他们易于切换.理解.扩展. 策略模式中 ...
- StrategyPattern (策略模式)
/** * 策略模式 * @author TMAC-J * 根据环境的不同选择不同的策略,把策略用接口抽象出来 */ public class StrategyPattern { interface ...
- JAVA 设计模式之策略模式
定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 类型:行为类模式 策略模式是对算法的封装,把一系列的算法分别封装到对应的类中,并且这些类实现相同的接口,相互之间可以替换.在前面说过 ...
- Java设计模式之策略模式(Strategy)
前言: 最近一直在学习基于okHttp网络请求,学习的过程中就想起了之前项目中有这么一个需求不同的接口要采用不同的加密方式,比如登录之前要采用RSA加密,登录之后要采用AES加密,当时是采用靠传递一个 ...
- 设计模式(一):“穿越火线”中的“策略模式”(Strategy Pattern)
在前段时间呢陆陆续续的更新了一系列关于重构的文章.在重构我们既有的代码时,往往会用到设计模式.在之前重构系列的博客中,我们在重构时用到了“工厂模式”.“策略模式”.“状态模式”等.当然在重构时,有的地 ...
- 《Head First 设计模式》之策略模式
作者:Grey 原文地址:http://www.cnblogs.com/greyzeng/p/5915202.html 模式名称 策略模式(Strategy Pattern) 需求 模拟鸭子游戏,游戏 ...
随机推荐
- 69道Java Spring 面试&笔试题
目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP) Spring MVC Spring 概述 1. 什么是spri ...
- android 中 listview 设置自动匹配高度
1.布局文件 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:l ...
- weblogic的jar包冲突
异常: Error creating bean with name 'sessionFactoryWrite' defined in class path resource [applicationC ...
- org.apache.commons.httpclient
org.apache.commons.httpclient /** * post 方法 * @param url * @param params * @return */ public static ...
- WebService工作原理
1.WebService工作原理-SOAP 当客户端调用一个WebService的方法时,首先将方法名称和需要传递的参数包装成XML,也就是SOAP包,通过HTTP协议传递到服务器端,然后服务器端解析 ...
- HDU 5025:Saving Tang Monk(BFS + 状压)
http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Problem Description <Journey to ...
- 20145227 《Java程序设计》实验五实验报告
20145227 <Java程序设计>实验五实验报告 实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 实验过程 1.先运行TCP代码,一人服务 ...
- 简单选择排序(Java)
简单选择排序: 每一趟在整个记录中找到最小的那个作为有序序列的第i个记录. class SelectSort{ public void p(int[] a){ for(int i=0;i<a.l ...
- Python模块学习
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...
- c# UDP通信 列子
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...