打印

C++循环问题

[复制链接]
867|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
txcy|  楼主 | 2012-4-14 20:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
for循环块里面没有continue语句却为什么会出现循环跳越?
下面是主要的代码,另外两个源文件我已经查过了,没有问题


#include<iostream>
#include "QueueAndCustomer.h"
#include<ctime>

bool customer(double factor);

int main(){

using std::cout;
using std::cin;
using std::endl;
using std::ios_base;
const int MIN_PER_HR=60;

std::srand(std::time(0));
double wait_time=0;
int hour;
int cyclelimit;
int clients;
int clientserved;
int clientturnaway;
int customer_per_hour;
int line_wait;
double min_per_cust;
double sum_line;
int size;
double result[100][3];
int column=0, row=0;
int maxCustomer;

cout<<"Please enter the maximun size of the first queue:";
cin>>size;

cout<<"Please enter the maximum customer per hour:";
cin>>maxCustomer;

cout<<"\nQueueSize"<<'\t'<<"ClientPerHour\t"<<"LineWait\tServed\t\tAveWait"<<endl;
Queue lineFirst(size), lineSecond;
Item client;
for(int q=1;q<=size;q++){
for(int ac=1;ac<=maxCustomer;ac++){ //就在这个代码块里面出现了循环跳越
sum_line=0;
line_wait=0;
clientserved=0;
clientturnaway=0;
clients=0;
wait_time=0;
for(int cycle=0;cycle<6000;cycle++){
if(customer(MIN_PER_HR/ac)){
if(lineFirst.isFull()){
clientturnaway++;
}else{
clients++;
client.set(cycle);
lineFirst.EnQueue(client);
}
}
if(wait_time<=0&&!lineFirst.isEmpty()){
lineFirst.DeQueue(client);
wait_time=client.getProcesstime();
line_wait+=cycle-client.when();
clientserved++;
}
if(wait_time>0)
wait_time--;
sum_line+=lineFirst.QueueCount();

}
if(q==10&&ac==15)
cout<<"("<<q<<","<<ac<<")\t\t"<<sum_line<<"\t\t"<<clientserved<<"\t\t"<<line_wait<<"\t\t"<<(double)line_wait/clientserved<<endl;

if((double)line_wait/clientserved<=1){
cout<<q<<"\t\t"<<ac<<"\t\t"<<line_wait<<"\t\t"<<clientserved<<"\t\t"<<(double)line_wait/clientserved<<endl; //A
result[row][0]=q;
result[row][1]=ac;
result[row][2]=(double)line_wait/clientserved;
row++;
}
}
}
cout<<"result match requirement:"<<endl;
cout<<"queue size\tcustomer per hour\taverage wait time"<<endl;
cout.precision(2);
for(int j=0;j<row;j++){
cout<<result[j][0]<<"\t\t"<<result[j][1]<<"\t\t\t"<<result[j][2]<<endl;
}
}

bool customer(double factor){

return (rand()*factor/RAND_MAX<1);

}

上面的代码编译执行之后,如果输入10, 30,就会出现一个循环的跳越,比如在A行的输出,ac就会可能从15跳越到20,而16到19没有输出,这到底是为什么?

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

274

主题

2106

帖子

0

粉丝