Calculation
定义一个Strategy接口,其中定义一个方法,用于计算
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Calculation
{
interface Interface1
{
int calculate(int a, int b);
}
}
定义具体的算法类,实现Strategy接口,算法类中的算法各自不同:加减乘等
1,加法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Calculation
{
class Add:Interface1
{
public int calculate(int a, int b)
{
return a + b;
} }
}
2,减法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Calculation
{
class Subtract : Interface1
{
public int calculate(int a, int b)
{
return a - b;
}
}
}
3,乘法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Calculation
{
class Multiply:Interface1
{
public int calculate(int a, int b)
{ return a * b;
} }
}
4,除法类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Calculation
{
class Except:Interface1
{
public int calculate(int a, int b)
{
return a / b ; }
}
}
定义具体的环境角色
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Calculation
{
class Environment
{
private Interface1 inter;
public Environment(Interface1 face)
{
inter = face;
}
public Interface1 gewrt()
{
return inter;
}
public void setwrt(Interface1 face)
{
inter=face;
}
public int calculate(int a, int b)
{
return inter.calculate(a, b);
} }
}
form1的代码
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; namespace Calculation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
displays();
}
private void displays()
{
Random random = new Random();
int a, b;
a = random.Next(0, 100);
b = random.Next(0, 100);
fasttext.Text = a.ToString();
lasttext.Text = b.ToString();
string[] operjact = new string[] { "+", "-", "*", "/" };
string f = operjact[new Random().Next(0, 4)];
operja.Text = f; } private void textBox3_KeyDown(object sender, KeyEventArgs e)
{
int a = int.Parse(fasttext.Text);
int b = int.Parse(lasttext.Text); if (e.KeyCode == Keys.Enter)
{ int answer = int.Parse(textBox3.Text); string OPter=operja.Text;
switch (OPter)
{
case "+":
Add addss = new Add();
Environment environment = new Environment(addss);
environment.calculate(a, b);
if (answer == environment.calculate(a, b))
{
MessageBox.Show("回答正确!");
}
else
{
MessageBox.Show("回答错误!");
}
break;
case "-":
Subtract subtrss = new Subtract();
Environment environment1 = new Environment(subtrss);
environment1.calculate(a,b);
if (answer == environment1.calculate(a, b))
{
MessageBox.Show("回答正确!");
}
else
{
MessageBox.Show("回答错误!");
}
break;
case "*":
Multiply muli = new Multiply();
Environment environment2 = new Environment(muli);
environment2.calculate(a, b);
if (answer == environment2.calculate(a, b))
{
MessageBox.Show("回答正确!");
}
else
{
MessageBox.Show("回答错误!");
}
break;
case "/":
Except except = new Except();
Environment enviroment3 = new Environment(except);
enviroment3.calculate(a, b);
if(answer==enviroment3.calculate(a,b))
{
MessageBox.Show("回答正确!");
}
else
{
MessageBox.Show("回答错误!");
}
break; }
textBox3.Clear();
displays(); } }
}
}
总结:
两个数是让它随机产生的,运算符是随机产生的,无法保证除以零的情况。总的来说这些代码套来套去的有点发晕。不知道自己写的怎么样。
其中有一部分代码不太明白感觉没什么用?
public Interface1 gewrt()
{
return inter;
}
public void setwrt(Interface1 face)
{
inter=face;
}
就把他们删了试着运行了一下,还是可以正常运行。
Calculation的更多相关文章
- OpenCASCADE Curve Length Calculation
OpenCASCADE Curve Length Calculation eryar@163.com Abstract. The natural parametric equations of a c ...
- hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律
http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...
- inconsistent line count calculation in projection snapshot
1.现象 在vs2013中,按Ctrl + E + D格式化.cshtml代码,vs2013系统崩溃.报:inconsistent line count calculation in projecti ...
- 贪心 HDOJ 4726 Kia's Calculation
题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...
- WARNING: Calls to any function that may require a gradient calculation inside a conditional block may return undefined results
GLES2.0: Some device will give a warning on compling shaders(yet the compling will succeed), and the ...
- VKP5 Price Calculation – List Variant & KZPBL (Delete site level)
List Variant: Configuration in Logistic General –> Retail Pricing –> Sales Price Calculation – ...
- hdu 2837 Calculation 指数循环节套路题
Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3501 Calculation 2(欧拉函数)
Calculation 2 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- Calculation(dfs+状压dp)
Problem 1608 - Calculation Time Limit: 500MS Memory Limit: 65536KB Total Submit: 311 Accepted: ...
随机推荐
- A2DP协议笔记
1.概述 A2DP(Advanced Audio Distribution Profile)是蓝牙的音频传输协议,典型应用为蓝牙耳机.A2DP协议的音频数据在ACL Link上传输,这与SCO ...
- 转 创建 JavaScript XML 文档注释
http://www.cnblogs.com/chenxizhang/archive/2009/07/12/1522058.html 如何:创建 JavaScript XML 文档注释 Visual ...
- websocket nodejs
Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...
- 【Android开发学习笔记】【第一课】初识New Project,工程文件介绍
初学者新建一个Andriod工程后,往往不知道Pakage Explorer区域的每个文件是什么作用,今天学习了一下,自我总结一下. 1.先新建一个工程 2.输入名称,以及支持的SDK版本等(这些可以 ...
- about eclipse
导出javadoc command选择bin下的javadoc.exe 配置编码和文字 英文:-locale en_US -encoding UTF-8 -charset UTF-8 中文:-loca ...
- ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明
原文:ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明 ArcGIS Portal 10.4 本地坐标系的web 3d地形展示制作说明 By 李远祥 ArcGIS Por ...
- MVC中”从客户端检测到有潜在危险的Request.Form值“的解决方法
从客户端检测到有潜在危险的Request.Form值: 在webForm中,可以在aspx页面顶部 <%@ Page Language="C#" AutoEventWireu ...
- cssHack
cssHack 不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不 ...
- SQLServer User and Login Tips
use master IF EXISTS (SELECT * FROM sys.databases WHERE name = 'gpdb83sp')BEGIN DROP DATABASE gpdb83 ...
- 读取XML文档结构并写入内容
1.在项目中新建XML文档结构.xsd文件,在其中添加相应的节点. 2.读取文档结构并写入内容 string initFileName = @"D:\Config.xml"; Da ...