结对编程2
码市地址:https://git.coding.net/cjz113/jiedui2.git
结对成员:201421123111 201421123113
题目描述:
上一周大家为四则运算程序设计了2-3个新功能,本次在隔了一周之后,我们循序渐进地进阶。本次目标:
1.把计算模块提取出来,单独创建一个类。
2.针对提取出来的计算类的接口函数做单元测试。
3.通过单元测试代码,测试加法是否能正确工作;
4.通过单元测试代码,测试加减乘除功能。
5.通过单元测试代码,测试计算类对于各种参数的支持:
题目要求:
1.结对编程实现上述功能,同样的,在程序正式开发之前,请先预估下PSP每个环节的消耗时间(分钟),并在过程中统计实际耗时(分钟),最后提交PSP表格。依然注意,这个主要是给你们自己看的,不必造假数据。
2.继续两人结对协作,把编码规范、领航员和驾驶员角色互换做到位。
3.单元测试: 有单元测试保证,有代码覆盖率。
提取计算类:
类定义:
class cla
{
public:
string integer(int op,int x,int y);
string fraction(int op,int a,int b,int c,int d);
static int maxNumber(int k, int l);
};
整数:
string integer(int op, int x, int y)
{
char Operator[] = { '+', '-', '*', '/' };
string result;
int element, denominator; //定义答案分子、分母
int maxNum; //定义公约数
char answer[10]; //答案
if (Operator[op] == Operator[0])
{
int n;
n = x + y;
result = sprintf_s(answer, "%d", n);
}
else
if (Operator[op] == Operator[1])
{
int n;
n = x - y;
result = sprintf_s(answer, "%d", n);
}
else
if (Operator[op] == Operator[2])
{
int n;
n = x*y;
result = sprintf_s(answer, "%d", n);
}
else
if (Operator[op] == Operator[3])
{
element = x;
denominator = y;
maxNum = cla::maxNumber(element, denominator);
element = element / maxNum;
denominator = denominator / maxNum;
if (denominator != 1)
{
result = sprintf_s(answer, "%d/%d", element, denominator);
}
else
{
result = sprintf_s(answer, "%d", element);
}
}
else
if (Operator[op] != Operator[0] && Operator[op] != Operator[1] && Operator[op] != Operator[2] && Operator[op] != Operator[3])
{
result = "error";
}
return result;
}
分数:
string fraction(int op, int a, int b, int c, int d)
{
char Operator[] = { '+', '-', '*', '/' };
string result;
int element, denominator; //定义答案分子、分母
int maxNum; //定义公约数
char answer[10]; //答案
if (Operator[op] == Operator[0])
{
element = (b*c) + (a*d);
denominator = a*c;
maxNum = cla::maxNumber(element, denominator);
element = element / maxNum;
denominator = denominator / maxNum;
if (denominator != 1)
{
result = sprintf_s(answer, "%d/%d", element, denominator);
}
else
{
result = sprintf_s(answer, "%d", element);
}
}
else
if (Operator[op] == Operator[1])
{
element = (b*c) - (a*d);
denominator = a*c;
maxNum = cla::maxNumber(element, denominator);
element = element / maxNum;
denominator = denominator / maxNum;
if (denominator != 1)
{
result = sprintf_s(answer, "%d/%d", element, denominator);
}
else
{
result = sprintf_s(answer, "%d", element);
}
}
else
if (Operator[op] == Operator[2])
{
element = b*d;
denominator = a*c;
maxNum = cla::maxNumber(element, denominator);
element = element / maxNum;
denominator = denominator / maxNum;
if (denominator != 1)
{
result = sprintf_s(answer, "%d/%d", element, denominator);
}
else
{
result = sprintf_s(answer, "%d", element);
}
}
else
if (Operator[op] == Operator[3])
{
element = b*c;
denominator = a*d;
maxNum = cla::maxNumber(element, denominator);
element = element / maxNum;
denominator = denominator / maxNum;
if (denominator != 1)
{
result = sprintf_s(answer, "%d/%d", element, denominator);
}
else
{
result = sprintf_s(answer, "%d", element);
}
}
else
if (Operator[op] != Operator[0] && Operator[op] != Operator[1] && Operator[op] != Operator[2] && Operator[op] != Operator[3])
{
result = "error";
}
return result;
}
取最大公约数:
static int maxNumber(int k, int l)
{
while (k%l != 0)
{
int t = k%l;
k = l;
l = t;
}
return l;
}
测试用例:
整数计算测试用例:
TEST_METHOD(integerTest)
{
//TrivialCalculator test;
string actual = test->integer(0, 1, 2);
string expected = "3";
Assert::AreEqual(expected, actual);
actual = test->integer(0, 2, 1);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->integer(1, 1, 2);
expected = "-1";
Assert::AreEqual(expected, actual);
actual = test->integer(1, 2, 1);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->integer(2, 1, 2);
expected = "2";
Assert::AreEqual(expected, actual);
actual = test->integer(2, 2, 1);
expected = "1";
Assert::AreNotEqual(expected, actual);
actual = test->integer(3, 1, 2);
expected = "1 / 2";
Assert::AreEqual(expected, actual);
actual = test->integer(3, 2, 1);
expected = "2";
Assert::AreNotEqual(expected, actual);
}
分数计算测试用例:
TEST_METHOD(fractionTest)
{
string actual = test->fraction(0, 1, 2, 1, 2);
string expected = "1";
Assert::AreEqual(expected, actual);
actual = test->fraction(0, 1, 2, 1, 2);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->fraction(1, 1, 2, 1, 2);
expected = "0";
Assert::AreEqual(expected, actual);
actual = test->fraction(1, 1, 2, 1, 2);
expected = "2";
Assert::AreNotEqual(expected, actual);
actual = test->fraction(2, 1, 2, 1, 2);
expected = "1 / 4";
Assert::AreEqual(expected, actual);
actual = test->fraction(2, 1, 2, 1, 2);
expected = "1";
Assert::AreNotEqual(expected, actual);
actual = test->fraction(3, 1, 2, 1, 2);
expected = "1";
Assert::AreEqual(expected, actual);
actual = test->fraction(3, 1, 2, 1, 2);
expected = "2";
Assert::AreNotEqual(expected, actual);
}
最大公约数测试用例:
TEST_METHOD(maxNumberTest)
{
int actual = test->maxNumber(6, 3);
int expected = 3;
Assert::AreEqual(expected, actual);
actual = test->maxNumber(4, 2);
expected = 3;
Assert::AreNotEqual(expected, actual);
}
结果:
不知道是否是VS的问题,用C++做的程序一直改都还是会有错误。换过了很多版本的VS还是不行。
小结与感受:
可能是对单元测试这种东西不了解,一开始几乎不知道要干什么。坚持使用C++可不知是软件还是什么原因一直不成功,感觉用JAVA的话应该可以简单一点
有同学使用C++完成了单元测试,但把源码都交给同学却依旧无法成功。
PSP2.1 | Personal Software Process Stages | Time (%) Senior Student(/hour) | Time (%)(/hour) |
· Planning | 计划 | 2 | 1.5 |
· Estimate | 估计这个任务需要多少时间 | 50 | 50 |
· Analysis | 需求分析 (包括学习新技术) | 1 | 1 |
· Coding Standard | 代码规范 | 0.5 | 0.5 |
· Design | 具体设计 | 1.5 | 1 |
· Coding | 具体编码 | 43 | 44 |
· Test | 测试(自我测试,修改代码,提交修改) | 1 | 1 |
Reporting | 报告 | 1 | 1 |