构建之法第二次作业

 GIT地址 https://github.com/15881450273 GIT用户名 15881450273 学号后五位 62518 博客地址 https://www.cnblogs.com/Slow-Walker/ 作业链接 https://www.cnblogs.com/harry240/p/11515697.html

13+17-1=29

11*15-5=160

3+10+4-16=1

15÷5+3-2=4

三、克隆项目

Step1：进入阿超所放代码的仓库

Step2：点击Fork拷贝到自己的同名仓库

Step3：检查是否复制成功

Step4：安装git

Step5：利用git将项目克隆岛本地

Step6：创建项目文件夹

四、算法设计

工厂方法模式重构图

4.2部分代码：

Equality（工厂类）：

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace calculation
{
public partial class From1 : Form
{
//ArrayList shizi = new ArrayList();
String[] shizi = new String[10000];
String[] shizi1 = new String[10000];
String[] shizi2 = new String[10000];
public From1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text == "")
{
textBox1.Text = "";
MessageBox.Show("未输入生成计算题数量！");
}
else
{
textBox1.Text = "";
int num;
Makequality gen = new Makequality();
Makefour m = new Makefour();
num = Convert.ToInt32(textBox2.Text);
int n1 = num / 2;
int n2 = num / 2;
shizi1 = gen.fun3(n1);
shizi2 = m.fun4(n2);
shizi1.CopyTo(shizi, 0);
shizi2.CopyTo(shizi, shizi1.Length);
if (checkBox1.CheckState == CheckState.Checked)
for (int i = 0; i < num; i++)
textBox1.Text = textBox1.Text + (i + 1).ToString() + ": " + shizi[i] + Environment.NewLine;
else
{
for (int i = 0; i < num; i++)
{
String[] str = shizi[i].Split('=');
textBox1.Text = textBox1.Text + (i + 1).ToString() + ": " + str[0] + '=' + Environment.NewLine;
}
}

}

}
public void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(Char.IsNumber(e.KeyChar)) && e.KeyChar != (char)13 && e.KeyChar != (char)8)
e.Handled = true;
}

public void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
MessageBox.Show("无计算题，无法导出！");
else
{
FileStream fs = new FileStream(".\\text.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write(textBox1.Text);
sw.Flush();
sw.Close();
fs.Close();
MessageBox.Show("您的四则运算题目生成成功并导出，请注意查收计算题导出成功！");
}
}

public void checkBox1_CheckedChanged(object sender, EventArgs e)
{
try {
textBox1.Text = "";
int num;
num = Convert.ToInt32(textBox2.Text);
if (checkBox1.CheckState == CheckState.Checked)
for (int i = 0; i < num; i++)
textBox1.Text = textBox1.Text + (i + 1).ToString() + ": " + shizi[i] + Environment.NewLine;
else
{
for (int i = 0; i < num; i++)
{
String[] str = shizi[i].Split('=');
textBox1.Text = textBox1.Text + (i + 1).ToString() + ": " + str[0] + '=' + Environment.NewLine;
}
}
}
catch (Exception ) {
MessageBox.Show("请先输入题目");
}

}

public void button3_Click(object sender, EventArgs e)
{
}

public void From1_Load(object sender, EventArgs e)
{

}
}
}

Makequality（具体产生两个符号等式类）：

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace calculation
{
public class Makequality
{
public String[] fun3(int num)
{
char[] opt = new char[4] { '+', '-', '×', '÷'};
int a, b, c, opt1, opt2, res1=0, res2=0, temp;
bool flag1, flag2, changed;
ArrayList shizi = new ArrayList();
Random rd = new Random();
for(int i = 0; i < num; i++)
{
opt1 = rd.Next(1, 5);
a = rd.Next(1, 100);
b = rd.Next(1, 100);
flag1 = false;
while (true)
{
// 如果第一部分能够符合标准，确定第一个式子
if (flag1)
break;
else
{
a = rd.Next(1, 100);
b = rd.Next(1, 100);
}
switch (opt1)
{
case 1:
if (a + b < 100)
{
flag1 = true;
res1 = a + b;
}
break;
case 2:
if (a - b > 0)
{
flag1 = true;
res1 = a - b;
}
else
{
temp = a; a = b; b = temp;
flag1 = true;
res1 = a - b;
}
break;
case 3:
if (a * b < 100)
{
flag1 = true;
res1 = a * b;
}
break;
case 4:
if (a % b == 0)
{
flag1 = true;
res1 = a / b;
}
else if (b % a == 0)
{
temp = a; a = b; b = temp;
flag1 = true;
res1 = a / b;
}
break;
}
}
// 第二部分
opt2 = rd.Next(1, 5);
c = rd.Next(1, 100);
flag2 = false;
changed = false;
while (true)
{
if (flag2)
break;
else
{
opt2 = rd.Next(1, 4);
c = rd.Next(1, 100);
changed = false;
}
switch (opt2)
{
case 1:
if (res1 + c < 100)
{
flag2 = true;
res2 = res1 + c;
}
break;
case 2:
if (res1 - c > 0)
{
flag2 = true;
res2 = res1 - c;
}
else
{
temp = res1; res1 = c; c = temp;
flag2 = true;
res2 = res1 - c;
changed = true;
}
break;
case 3:
if (res1 * c < 100)
{
flag2 = true;
res2 = res1 * c;
}
break;
case 4:
if (res1 % c == 0)
{
flag2 = true;
res2 = res1 / c;
}
else if (c % res1 == 0)
{
temp = res1; res1 = c; c = temp;
flag2 = true;
res2 = res1 / c;
changed = true;
}
break;
}
}
if (changed) //如果符号改变
if (opt1 > 2 && opt2 <= 2)
shizi.Add(res1.ToString() + opt[opt2 - 1] + a.ToString() + opt[opt1 - 1] + b.ToString() + "=" + res2.ToString());
else
shizi.Add(res1.ToString() + opt[opt2 - 1] + "(" + a.ToString() + opt[opt1 - 1] + b.ToString() + ")=" + res2.ToString());
else
if (opt1 <= 2 && opt2 > 2)
shizi.Add("(" + a.ToString() + opt[opt1 - 1] + b.ToString() + ")" + opt[opt2 - 1] + c.ToString() + "=" + res2.ToString());
else
shizi.Add(a.ToString() + opt[opt1 - 1] + b.ToString() + opt[opt2 - 1] + c.ToString() + "=" + res2.ToString());
}
return  (string[])shizi.ToArray(typeof(string));
}

public string[] fun3()
{
throw new NotImplementedException();
}
}
}

4.3效果展示

Step1：输入生成算术题数目20

Step2：点击生成题目

Step3：点击显示计算结果

Step4：点击导出题目txt

Step5：进入目录检查题目txt

五、单元测试

5.1添加单元测试项目：CalculatorUnitTest

5.2、配置将原来项目设置成类库，并编译后在CalculatorUnitTest，进行单元测试。

Step1：测试生成具体角色Makefour（生成三个符号的四则运算）

Step2：测试生成具体角色Makequality（生成两个符号的四则运算）

Step3：测试工厂角色生成四则运算是否正常

八、效能工具介绍

Step1：点击主菜单栏下分析选项中的性能探查器

Step2：选择性能向导

Step3：在性能向导中选择测量函数调用计数和用时，这里可以对每一个函数单独分析其性能情况

Step4：得到性能分析结果，可选择想要分析的内容进行查看

Step5：针对具体项对功能进行优化

九、提交代码

Step1首先将自己的代码整理完毕之后就鼠标点击git bush here

Step2：输入用户名密码上传中：

Step3：转到总的仓库：

posted on 2019-09-20 15:03  Slow-walker  阅读(451)  评论(1编辑  收藏  举报