定义一个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;
}

就把他们删了试着运行了一下,还是可以正常运行。

05-11 02:42