编程菜鸟的日记-初学尝试编程-关于测试程序
2013-10-23 10:59
267 查看
//club函数模拟一个俱乐部的顾客,初始化情况下是0个顾客,俱乐部内只能容纳有限个数的顾客,假设为50.当用户超过50,则必须在外面等待。当一些顾客离开时,等待队列将减少。用club函数打印在俱乐部内部的人数和在外面等待的人数,编写代码如下:
#include <iostream>
using namespace std;
#define MAX_IN_CUSTOM (50)
void int club(int x)
{//若果外面来的客人分两种情况x>=0和x<0
//此处静态变量使用是必要的
static int in_custom=0;
static int out_custom=0;
//若大于或等于0
if(x>0)
{//大于0的情况下再分为两种子情况in_custom+x>=MAX_IN_CUSTOM和小于的情况
if(x+in_custom>=MAX_IN_CUSTOM)
{
out_custom+=x+in_custom-MAX_IN_CUSTOM;
in_custom=MAX_IN_CUSTOM;
}
else
in_custom+=x;
}
else if(x<0)
{
x=-x;
//小于0的情况下分两种情况out_custom>=x和out_custom<x
if(x>=out_custom)
{
in_custom-=x-out_custom;
out_custom=0;
}
else
out_custom-=x;
}
if(in_custom<0)
{
cout<<"N/A"<<endl;
}
else
{
cout<<"in_custom="<<in_custom<<endl;
cout<<"out_custom="<<out_custom<<endl;
}
}
int main()
{
club(40);
club(20);
club(-5);
club(-30);
club(-30);
club(0);
club(-5);
system("pause");
return 0;
}
总结:测试数据尽可能覆盖各种情况,语句覆盖和条件覆盖,
#include <iostream>
using namespace std;
#define MAX_IN_CUSTOM (50)
void int club(int x)
{//若果外面来的客人分两种情况x>=0和x<0
//此处静态变量使用是必要的
static int in_custom=0;
static int out_custom=0;
//若大于或等于0
if(x>0)
{//大于0的情况下再分为两种子情况in_custom+x>=MAX_IN_CUSTOM和小于的情况
if(x+in_custom>=MAX_IN_CUSTOM)
{
out_custom+=x+in_custom-MAX_IN_CUSTOM;
in_custom=MAX_IN_CUSTOM;
}
else
in_custom+=x;
}
else if(x<0)
{
x=-x;
//小于0的情况下分两种情况out_custom>=x和out_custom<x
if(x>=out_custom)
{
in_custom-=x-out_custom;
out_custom=0;
}
else
out_custom-=x;
}
if(in_custom<0)
{
cout<<"N/A"<<endl;
}
else
{
cout<<"in_custom="<<in_custom<<endl;
cout<<"out_custom="<<out_custom<<endl;
}
}
int main()
{
club(40);
club(20);
club(-5);
club(-30);
club(-30);
club(0);
club(-5);
system("pause");
return 0;
}
总结:测试数据尽可能覆盖各种情况,语句覆盖和条件覆盖,
相关文章推荐
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习2
- 编程菜鸟的日记-初学尝试编程-回文数的判断
- 编程菜鸟的日记-初学尝试编程-单链表
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习3
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习6
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习8
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习5
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习7
- 编程菜鸟的日记-初学尝试编程-寻找等长数组A与B(所含元素相同,顺序不同)相匹配的元素即a[i]=b[j]
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习9
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习2
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习3
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习7
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习1
- 编程菜鸟的日记-初学尝试编程-链表元素快速排序
- 编程菜鸟的日记-初学尝试编程-顺序表的类定义及其基本操作算法(创建表、元素插入、元素删除、顺序查找、测表空、求表长、输出等)
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习8
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习5
- 编程菜鸟的日记-初学尝试编程-寻找2到n之间的素数并输出
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习3