使用GitHub
作业背景介绍:
阿超家里的孩子上小学一年级了,这个暑假老师给家长们布置了一个作业:家长每天要给孩子出一些合理的,但要有些难度的四则运算题目,并且家长要对孩子的作业打分记录。
作为程序员的阿超心想,既然每天都需要出题,那何不做一个可以自动生成小学四则运算题目与解决题目的命令行 “软件”呢。他把老师的话翻译一下,就形成了这个软件的需求:
程序接收一个命令行参数 n,然后随机产生 n 道加减乘除(分别使用符号+-*/来表示)练习题,每个数字在 0 和 100 之间,运算符在 2 个 到 3 个之间。由于阿超的孩子才上一年级,并不知道分数。所以软件所出的练习题在运算过程中不得出现非整数,比如不能出现 3÷5+2=2.6 这样的算式。
Part1.配置环境
1.vs的安装
先前用过vs开发软件故不再掩饰如何安装
2.Git安装
根据助教辅助教程到官网注册并下载Git
安装完毕
3.GitHub克隆目标仓库
在GitHub中输入目标地址: https://github.com/ChildishChange/Calculator
并克隆到本地
新建文件夹存放克隆的仓库,我的为xxoo
创建完毕开始写代码
Part2.代码设计
思路:穷举法,随机数等,没有用到c#本身计算字符串内数学表达式的函数库,思路简单。
过程:由于程序较为简单,原本想直接写在main里就行了,但为了实现后来的测试,把部分代码封装到了类里。
具体代码:
namespace ConsoleApp2
{
public class Program
{
static void Main(string[] args)
{
string str= Console.ReadLine();
for (int i = 0; i < Convert.ToInt32(str); i++)
{
int A = 200;
int d = 200;
pp Q = new pp();
int P = Q.rd(0, 2), P1 = Q.rd(0, 2), P2 =Q. rd(1, 3), P3 = Q.rd(1, 4);
int B =Q.rd(1, 100), C = Q.rd(1, 100), D = Q.rd(1, 100);
int a = Q.rd(1, 100), b =Q. rd(1, 100);
if (Q.calculator(d,a,b,A,B,C,D,P,P1,P2,P3) < 0)
i--;
}
System.Console.WriteLine("hahaha");
System.Threading.Thread.Sleep(1000000000);
}
}
public class pp
{
public int rd(int p, int q)
{
long tick = DateTime.Now.Ticks;
System.Threading.Thread.Sleep(1);
Random rd1 = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
return rd1.Next(p, q);
}
public int calculator(int d, int a, int b, int A, int B, int C, int D, int P, int P1, int P2, int P3)
{
int answer = 0;//P为三项式或四项式(0,1),P1为是否有除(0,1)
while (d > 100)
{
a = rd(1, 100); b = rd(1, 100); //生成六个100以内的整数
d = a * b;
}
while (A > 100)
{
B = rd(1, 100); C = rd(1, 100); D = rd(1, 100);
A = B + C + D;
}
if (P == 1)//为四项多项式
{
if (P1 == 1)
{
if (P2 == 1)//两除
{
if (P3 == 1)//加
{
answer = ((d / a) / b) + A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "÷" + b + "+" + A + " 答案:" + answer);
}
else if (P3 == 2)//减
{
answer = ((d / a) / b) - A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "÷" + b + "-" + A + " 答案:" + answer);
}
else if (P3 == 3)//乘
{
answer = ((d / a) / b) * A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "÷" + b + "x" + A + " 答案:" + answer);
}
}
else if (P2 == 2)//一除
{
if (P3 == 1)//+-
{
answer = (d / a) + B - A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "+" + B + "-" + A + " 答案:" + answer);
}
else if (P3 == 2)//x+
{
answer = ((d / a) * B) + A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "x" + B + "+" + A + " 答案:" + answer);
}
else if (P3 == 3)//x-
{
answer = ((d / a) * B) - A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "x" + B + "-" + A + " 答案:" + answer);
}
}
}
else if (P1 == 0)//没除
{
if (P2 == 1)//一乘
{
if (P3 == 1)//+-
{
answer = A * B + C - D;
if (answer > 0)
Console.WriteLine(A + "x" + B + "+" + C + "-" + D + " 答案:" + answer);
}
else if (P3 == 2)//++
{
answer = A * B + C + D;
if (answer > 0)
Console.WriteLine(A + "x" + B + "+" + C + "+" + D + " 答案:" + answer);
}
else if (P3 == 3)//--
{
answer = A * B - C - D;
if (answer > 0)
Console.WriteLine(A + "x" + B + "-" + C + "-" + D + " 答案:" + answer);
}
}
if (P2 == 2)//两乘
{
if (P3 == 1 || P3 == 2)//+
{
answer = A * B * C + D;
if (answer > 0)
Console.WriteLine(A + "x" + B + "x" + C + "+" + D + " 答案:" + answer);
}
else if (P3 == 1 || P3 == 2)//-
{
answer = A * B * C - D;
if (answer > 0)
Console.WriteLine(A + "x" + B + "x" + C + "-" + D + " 答案:" + answer);
}
}
}
}
else if (P == 0)//三项式
{
if (P1 == 1)//有除
{
if (P3 == 1)//加
{
answer = d / a + A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "+" + A + " 答案:" + answer);
}
else if (P3 == 2)//减
{
answer = d / a - A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "-" + A + " 答案:" + answer);
}
else if (P3 == 3)//乘
{
answer = d / a * A;
if (answer > 0)
Console.WriteLine(d + "÷" + a + "x" + A + " 答案:" + answer);
}
}
else if (P1 == 0)//无除
{
if (P3 == 1)//+-
{
answer = A + B - C;
if (answer > 0)
Console.WriteLine(A + "+" + B + "-" + C + " 答案:" + answer);
}
else if (P3 == 2)//x+
{
answer = A * B + C;
if (answer > 0)
Console.WriteLine(A + "x" + B + "+" + C + " 答案:" + answer);
}
else if (P3 == 3)//x-
{
answer = A * B - C;
if (answer > 0)
Console.WriteLine(A + "x" + B + "-" + C + " 答案:" + answer);
}
}
}
return answer;
}
}
运行结果:
以生成20个算式为例:
Part3.单元测试
生成一个测试的解决方案,并实现接口
具体代码(随便测试除乘加、乘加减等四种情况):
namespace UnitTestProject1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
/*public void TestMethod1()
{
//准备
ConsoleApp2.pp test = new ConsoleApp2.pp();
//设定测试用例
int d = 10; int a = 5; int b = 2; int A = 30; int B = 15; int C = 10; int D = 5; int P = 1; int P1 = 1; int P2 = 2; int P3 = 2;
//测试标准
int actual=test.calculator(d, a, b, A, B, C, D, P, P1, P2, P3);
int expect = 60;
//判断
Assert.AreEqual(expected: expect,actual: actual, message: "除乘加不正确");
}*/
public void TestMethod2()
{
//准备
ConsoleApp2.pp test = new ConsoleApp2.pp();
//设定测试用例
int d = 10; int a = 5; int b = 2; int A = 30; int B = 15; int C = 10; int D = 5; int P = 1; int P1 = 0; int P2 = 1; int P3 = 1;
//测试标准
int actual = test.calculator(d, a, b, A, B, C, D, P, P1, P2, P3);
int expect = 455;
//判断
Assert.AreEqual(expected: expect, actual: actual, message: "乘加减不正确");
}
public void TestMethod3()
{
//准备
ConsoleApp2.pp test = new ConsoleApp2.pp();
//设定测试用例
int d = 10; int a = 5; int b = 2; int A = 30; int B = 15; int C = 10; int D = 5; int P = 0; int P1 = 1; int P2 = 2; int P3 = 1;
//测试标准
int actual = test.calculator(d, a, b, A, B, C, D, P, P1, P2, P3);
int expect = 32;
//判断
Assert.AreEqual(expected: expect, actual: actual, message: "除乘加不正确");
}
public void TestMethod4()
{
//准备
ConsoleApp2.pp test = new ConsoleApp2.pp();
//设定测试用例
int d = 10; int a = 5; int b = 2; int A = 30; int B = 15; int C = 10; int D = 5; int P = 0; int P1 = 0; int P2 = 2; int P3 = 1;
//测试标准
int actual = test.calculator(d, a, b, A, B, C, D, P, P1, P2, P3);
int expect = 35;
//判断
Assert.AreEqual(expected: expect, actual: actual, message: "除乘加不正确");
}
}
}
运行结果:
当条件为乘加减时通过测试
Part4.断点测试和回归测试
都是最基本的测试手段,放一张图
Part.5 效能分析
我们只考虑CPU使用率
结果如下
Part.6 上传代码
在 add 和 commit 后 state 检查状态
最后push
同步成功,下一步上传给阿超
上传成功
Part.7 总结:
GitHub真是个好东西,为开源学习提供了一个很好的平台同时也方便我线上管理我的代码仓库。
我个人编程能力很差,因为懒,几乎没编过程。前两天在一个比赛里赶鸭子上架用C++编了一套模拟系统,一千多行代码,以前从来没用过的诸如断点调试等一些小技巧一下子都会了。感觉学习最好的方式就是边做边学,就像这次的GitHub使用一样,必须通过项目来促进其学习及使用。纸上得来终觉浅,绝知此事要躬行。