您的位置:首页 > 编程语言

编程菜鸟的日记-初学尝试编程-关于测试程序

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;

}

总结:测试数据尽可能覆盖各种情况,语句覆盖和条件覆盖,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐