# 《构建之法》第二次博客 熟悉工具

GIT地址 GIT地址
GIt用户名 Loserhqs

13+17-1=29

11*15-5=160

3+10+4-16=1

15÷5+3-2=4

## 三、环境配置

Visual Studio 2013是我大一的时候就安装好的，版本不是很高，但是觉得再安装一个Visual Studio 2017或者Visual Studio 2019很浪费时间，同时也增加了电脑的负担。所以这次四则运算的作业我还是用的Visual Studio 2013来实现的。如果没有的可以去官网下载或者在网上找百度云盘的资源。

## 四、项目克隆

https://github.com/Cherish599/AchaoCalculator，进入阿超四则运算生成器仓库，点击右上角的Fork键，将该仓库拷贝到自己的同名仓库中。

## 五、创建项目

public class Rand
{
//产生随机数
public static Random rand = new Random();
/*
public int n1 = rand.Next(0, 100);
public int n2 = rand.Next(0, 100);
public int n3 = rand.Next(0, 100);
public int n4 = rand.Next(0, 100);
public int s1 = rand.Next(0, 4);
public int s2 = rand.Next(0, 4);
public int s3 = rand.Next(0, 4);
public int type = rand.Next(0, 2);
*/
}


public class Input
{
public static int input()
{
Console.WriteLine("请输入需要产生的题目数：");
//题目数量
return n;
}
//文本输出
public static StreamWriter sw1 = new StreamWriter(@"E:\gitKU\AchaoCalculator\Loserhqs\Primarymath\Primarymath\result.txt");
}


public class Linkup
{
//判断算式结果是否为整数
public static bool nonnegint(double sum)
{
if (sum == (int)sum)
return true;
else
return false;
}
//两个数的运算
public static double arith(int a, int b, int s)
{
if (s == 0)
return a + b;
else if (s == 1)
return a - b;
else if (s == 2)
return a * b;
else
{
if (b != 0)
{
int temp = a / b;
if (temp * b == a)
return a / b;
else
return -1;
}
else
return -1;
}
}
}

public class Opterator
{
//两个运算符
public static int optwo(int n1, int n2, int n3, int s1, int s2, char[] oper)
{
int flag = 0;
//if ((oper[s1] == '+' || oper[s1] == '-') && (oper[s2] == '*' || oper[s2] == '/'))
if ((s1 / 2) == 0 && (s2 / 2) == 1)
{
double sum = Linkup.arith(n2, n3, s2);
if (sum >= 0)
{
{
double temp = Linkup.arith(n1, (int)sum, s1);
if (temp >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "=" + (int)temp);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "=" + (int)temp + "\r\n");
}
}
}
}
}
else
{
double sum = Linkup.arith(n1, n2, s1);
if (sum >= 0)
{
{
double temp = Linkup.arith((int)sum, n3, s2);
if (temp >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "=" + (int)temp);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "=" + (int)temp + "\r\n");
}
}
}
}
}
return flag;
}
//三个运算符
public static int opthree(int n1, int n2, int n3, int n4, int s1, int s2, int s3, char[] oper)
{
int flag = 0;
//if ((oper[s1] == '+' || oper[s1] == '-') && (oper[s2] == '*' || oper[s2] == '/') && (oper[s3] == '*' || oper[s3] == '/'))
if ((s1 / 2) == 0 && (s2 / 2) == 1 && (s3 / 2) == 1)
{
double sum = Linkup.arith(n2, n3, s2);
if (sum >= 0)
{
{
double temp = Linkup.arith((int)sum, n4, s3);
if (temp >= 0)
{
double temp1 = Linkup.arith(n1, (int)temp, s1);
if (temp1 >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1 + "\r\n");
}
}
}
}
}
}
else if ((s1 / 2) == 0 && (s2 / 2) == 1 && (s3 / 2) == 0)
{
double sum = Linkup.arith(n2, n3, s2);
if (sum >= 0)
{
{
double temp = Linkup.arith(n1, (int)sum, s1);
if (temp >= 0)
{
double temp1 = Linkup.arith((int)temp, n4, s3);
if (temp >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1 + "\r\n");
}
}
}
}
}
}
else if ((s1 / 2) == 0 && (s2 / 2) == 0 && (s3 / 2) == 1)
{
double sum = Linkup.arith(n3, n4, s3);
if (sum >= 0)
{
{
double temp = Linkup.arith(n1, n2, s1);
if (temp >= 0)
{
double temp1 = Linkup.arith((int)temp, (int)sum, s2);
if (temp1 >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1 + "\r\n");
}
}
}
}
}
}
else if ((s1 / 2) == 1 && (s2 / 2) == 0 && (s3 / 2) == 1)
{
double sum = Linkup.arith(n1, n2, s1);
if (sum >= 0)
{
{
double temp = Linkup.arith(n3, n4, s3);
if (temp >= 0)
{
double temp1 = Linkup.arith((int)sum, (int)temp, s2);
if (temp1 >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1 + "\r\n");
}
}
}
}
}
}
else
{
double sum = Linkup.arith(n1, n2, s1);
if (sum >= 0)
{
{
double temp = Linkup.arith((int)sum, n3, s2);
if (temp >= 0)
{
double temp1 = Linkup.arith((int)temp, n4, s3);
if (temp1 >= 0)
{
{
flag = 1;
Console.WriteLine(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1);
Input.sw1.Write(n1 + "" + oper[s1] + "" + n2 + "" + oper[s2] + "" + n3 + "" + oper[s3] + n4 + "=" + (int)temp1 + "\r\n");
}
}
}
}
}
}
return flag;
}
}


public class Produce
{
//存放运算符号的数组
public static char[] oper = new char[4] { '+', '-', '*', '/' };
public static void produce(int n)
{
int i=0;
//有n道题
while(i<n)
{
//生成两个或三个运算符的题目
int type = Rand.rand.Next(0, 2);
if (type == 0)
{
//随机生成三个数
int num1 = Rand.rand.Next(0, 100);
int num2 = Rand.rand.Next(0, 100);
int num3 = Rand.rand.Next(0, 100);
//随机生成两种运算符号
int index1 = Rand.rand.Next(0, 4);
int index2 = Rand.rand.Next(0, 4);
int flag=Opterator.optwo(num1, num2, num3, index1, index2, Produce.oper);
if(flag==1)
{
i++;
}
}
else
{
//随机生成四个数
int num1 = Rand.rand.Next(0, 10);
int num2 = Rand.rand.Next(0, 100);
int num3 = Rand.rand.Next(0, 100);
int num4 = Rand.rand.Next(0, 100);
//随机生成三种运算符号
int index1 = Rand.rand.Next(0, 4);
int index2 = Rand.rand.Next(0, 4);
int index3 = Rand.rand.Next(0, 4);
int flag=Opterator.opthree(num1, num2, num3, num4, index1, index2, index3, Produce.oper);
if(flag==1)
{
i++;
}
}
}
}
}


## 六、单元测试与回归测试

public class UnitTest1
{
[TestMethod]
public void TestMethod()
{
//TestMethod1();
//TestMethod2();
//TestMethod3();
//TestMethod4();
TestMethod5();
}
public void TestMethod1()
{
//测试随机数
int testnum = Rand.rand.Next(0, 100);
int testopr = Rand.rand.Next(0, 4);
int testtype = Rand.rand.Next(0, 2);
Console.WriteLine(testnum);
Console.WriteLine(Produce.oper[testopr]);
if (testtype == 0)
{
Console.WriteLine("三个数的运算");
}
else
{
Console.WriteLine("四个数的运算");
}
}
//测试两个数的运算
public void TestMethod2()
{
//testA=8*9
double ResultA = 72;
double TestA = Linkup.arith(8, 9, 2);
if (ResultA == TestA)
{
Console.WriteLine("testA的测试结果正确");
}
else
{
Console.WriteLine("testA的测试结果错误");
}
//testB=20/40
double ResultB = 0.5;
double TestB = Linkup.arith(20, 40, 3);
if (ResultB == TestB)
{
Console.WriteLine("testB的测试结果正确");
}
else
{
Console.WriteLine("testB的测试结果错误");
}
//testC=56/8
double ResultC = 7;
double TestC = Linkup.arith(56, 8, 3);
if (ResultB == TestB)
{
Console.WriteLine("testC的测试结果正确");
}
else
{
Console.WriteLine("testC的测试结果错误");
}
}
//测试结果是否为整数
public void TestMethod3()
{
double testnum1 = 5.63;
double testnum2 = 45;
{
Console.WriteLine("testnum1测试正确");
}
else
{
Console.WriteLine("testnum1测试错误");
}
{
Console.WriteLine("testnum2测试正确");
}
else
{
Console.WriteLine("testnum2测试错误");
}
}
//测试运算函数
public void TestMethod4()
{
//三个数的运算
//testop1=78-5*9
//int resultop1 = 33;
Console.WriteLine("预测结果为33");
Console.WriteLine("真实结果如下：");
int flag1 = Opterator.optwo(78, 5, 9, 1, 2, Produce.oper);
if(flag1==1)
{
Console.WriteLine("结果正确");
}
else
{
Console.WriteLine("结果错误");
}
//testop2=23+12/4
//int resultop2 = 26;
Console.WriteLine("预测结果为26");
Console.WriteLine("真实结果如下：");
int flag2 = Opterator.optwo(23, 12, 4, 0, 3, Produce.oper);
if (flag2 == 1)
{
Console.WriteLine("结果正确");
}
else
{
Console.WriteLine("结果错误");
}
//四个数的运算
//testop3=5*9-42/7
//int resultop3 = 39;
Console.WriteLine("预测结果为26");
Console.WriteLine("真实结果如下：");
int flag3 = Opterator.opthree(5, 9, 42, 7, 2,1,3 ,Produce.oper);
if (flag3 == 1)
{
Console.WriteLine("结果正确");
}
else
{
Console.WriteLine("结果错误");
}
//testop4=5-9+77/11
//int resultop4 = 11;
Console.WriteLine("预测结果为11");
Console.WriteLine("真实结果如下：");
int flag4 = Opterator.opthree(5, 9, 77, 11, 1, 0, 3, Produce.oper);
if (flag4 == 1)
{
Console.WriteLine("结果正确");
}
else
{
Console.WriteLine("结果错误");
}
}
//测试能否生成相应数量的题目
public void TestMethod5()
{
int n = 5;
Produce.produce(n);
}
}


## 十、总结

1.在安装git过程中，没有什么意外。整体来说，安装软件也是没有什么难的地方。只是第一次用git，不是很清楚它的一些指令，需要看相关的资料才能交文件下载与上传。

2.在用C#编程的过程中，也没有什么大的问题。可能是因为这个题的难度我还能接受的原因吧。但是有一段时间没有用C#编程了，有些用法与Python都能搞混，我也真是垃圾。

3.在用C#编程的过程中一定要注意静态变量的使用，因为C#是没有全局变量一说的。

4.第一次使用单元测试和回归测试，觉得很棒。以后的学习工作中，要经常使用这一功能。

posted @ 2019-09-20 18:48  STJinBo  阅读(182)  评论(2编辑  收藏  举报