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: ...
随机推荐
- Mongo命令行中执行CRUD
在命令行中使用mongo自带的shell命令来执行CRUD操作 首先链接到数据库 增 db.qiao.insert({"qq":1}) db.qiao.save({"qq ...
- 规则html表单对象赋值
function grid_load_callback(data, status) { if (data.rows.length > 0) { ...
- 设计模式:状态模式(State)
定 义:当一个对象内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况, 把状态的判断逻辑转移到表示不同状态的一系列子 ...
- 软件的NABCD----安装部分
N:需求 有些软件安装需要很多的插件,很很多的安装步骤,甚至文件夹移动等麻烦的步骤 A:做法 做成一键安装的程序 B:好处 默认D盘安装,节省C盘空间,没有赘余插件,一键安装,省时省力. C:竞争 和 ...
- Android Lint简介(转)
转载自原文:http://blog.csdn.net/hudashi/article/details/8333349,感谢原作者. 英文原文:http://tools.android.com/tips ...
- Spring框架,如何返回数据给视图(jsp文件)
第一步 准备返回给视图的数据 package com.cwebs.samples; import java.util.LinkedHashMap; import java.util.List; imp ...
- php object转数组示例
原本是这样格式的数据: object(Thrift\Server\PageCards)#32 (3) { ["cards"]=> array(10) { [0]=> o ...
- POJ 1035问题解答
#include <iostream>#include <cstdio>#include <cmath> #include <string>#inclu ...
- MySQL学习笔记——存储引擎的索引特性
- 私有IP地址范围
私有IP地址范围 有一部分的地址专门是用于内网的地址,包括: A类中 10.0.0.0/8 B类中 172.16.0.0/12 ~ 172.31.0.0/12 C类中 192.168.0.0/16 这 ...