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: ...
随机推荐
- MySQL- 锁(1)
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许多用户共享的资源.如何保证数据并发访问的一致性.有效性是所有数 ...
- TCP粘包
一.通信协议TCP/UDP: TCP(transport control protocol,传输控制协议)是面向连接的,面向流的,提供高可靠性服务.客户端和服务器端都要有一一成对的socket, 因此 ...
- 一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现。
一张表有三个字段:id(城市id) Cityname(城市名) Privence(所属省份)如果要统计每个省份有多少城市请用SQL实现.
- ASP.NET MVC 利用ActionFilterAttribute来做权限等
ActionFilterAttribute是Action过滤类,该属于会在执行一个action之前先执行.而ActionFilterAttribute是 MVC的一个专门处理action过滤的类.基于 ...
- 面试&笔试常见题,你了解多少?
HTML:1. 什么是语义化的HTML?有何意义?为什么要做到语义化?(高频率考题)2. 行内元素和块元素分别有哪些?(高频率)3. 严格模式与混杂模式的区分?如何触发这两种模式?(高频率)4. ...
- Hand 3D Pose Estimation
https://cvarlab.icg.tugraz.at/projects/hand_detection/
- VMware Workstation linux 问题
1.can't find /mnt/cdrom in etc/fstab or /etc/mtab mkdir /mnt/cdrom 2.80端口被占 一.查看哪些端口被打开 netstat -an ...
- Power-BI费用分析
费用分析主要从财务三大费用入手,剖析费用的结构.用途.占用等情况,从三大费用到明细费用.部门.职员的层层钻取,从而有效地进行费用管理和控制.Power-BI前端展示:图1<ignore_js_o ...
- proguard使用
Proguard用于混淆java代 码,使代码变为由难懂的,无规律的字符命名的各种方法和类,保护自己的劳动成果.个人认为proguard混淆纯java项目比较理想,比如j2me的 MIDLET,如 ...
- Java遇见HTML——JSP篇之JSP内置对象(上)
一.JSP内置对象简介 1.1.内置对象简介 JSP内置对象是WEB容器中创建的一组对象,可以直接使用不需要new,如截图中的out 对象. JSP有九大内置对象: 五大常用对象: out.reque ...