定义一个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的更多相关文章

  1. OpenCASCADE Curve Length Calculation

    OpenCASCADE Curve Length Calculation eryar@163.com Abstract. The natural parametric equations of a c ...

  2. hdu4965 Fast Matrix Calculation (矩阵快速幂 结合律

    http://acm.hdu.edu.cn/showproblem.php?pid=4965 2014 Multi-University Training Contest 9 1006 Fast Ma ...

  3. inconsistent line count calculation in projection snapshot

    1.现象 在vs2013中,按Ctrl + E + D格式化.cshtml代码,vs2013系统崩溃.报:inconsistent line count calculation in projecti ...

  4. 贪心 HDOJ 4726 Kia's Calculation

    题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...

  5. 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 ...

  6. VKP5 Price Calculation – List Variant & KZPBL (Delete site level)

    List Variant: Configuration in Logistic General –> Retail Pricing –> Sales Price Calculation – ...

  7. hdu 2837 Calculation 指数循环节套路题

    Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 3501 Calculation 2(欧拉函数)

    Calculation 2 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  9. Calculation(dfs+状压dp)

    Problem 1608 - Calculation Time Limit: 500MS   Memory Limit: 65536KB    Total Submit: 311  Accepted: ...

随机推荐

  1. js中!!的作用

    js中!!的作用是: !!一般用来将后面的表达式转换为布尔型的数据(boolean) ===表示类型什么的全部相等(自己写一个if测试一下就好了)!==表示要全部不想等包括类型(一样写一个if)||或 ...

  2. node.js使用util实现简单继承

    /** * Created by zzq on 2015/5/15. */ var util = require('util'); var Person = function(){ var myD=' ...

  3. 在CentOS6.5上安装Tomcat6

    Tomcat安装一向方便,linux的比win的更是这样,基本就是拷贝,类似于win中备受青睐的绿色软件,下面只是记录一下过程. 1.从 http://mirrors.cnnic.cn/apache/ ...

  4. Javascript数据类型的一些注意点

    1.字符串类型 substring()返回指定索引区间的子串: var s = 'hello, world' s.substring(0, 5); // 从索引0开始到5(不包括5),返回'hello ...

  5. Java Convert String & Int

    To convert a int to string: int num = 123; String str = String.valueOf(num); To convert a string to ...

  6. thinkphp 对数据库的操作

    查看ThinkPHP完全开发手册3.1 首先编辑配置文件 thinkphp这个数据库就不乱改了 昨天新建了一个 confluence(utf8)数据库 所以就用它学习一下吧,因为就只建立了一个数据库, ...

  7. 号外号外:9月21号关于Speed-BI 《全国人口统计数据分析》开讲了

    引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中?       本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中,通 ...

  8. Bugfree实用心得_转

    转自:http://blog.csdn.net/benkaoya/article/details/8719257 本博下有许多实用技巧 1. 什么是问题跟踪系统 问题跟踪系统(Issue Tracki ...

  9. 逻辑卷管理LVM (Logical Volume Manager)

    什么是LVM? LVM(Logical Volume Manager)逻辑卷管理,是一种将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的 ...

  10. [GDAL]读取HDF格式的calipso数据

    探测地球云层分布的CloudSat和CALIPSO卫星 http://www.nasa.gov/mission_pages/calipso/main/index.html http://www.nas ...