我正在尝试模拟一个等待队列。该程序包括队列和队列的控制器。它在单个运行时非常适合1个队列,但是现在我必须向其中添加多个队列。我制作了一个新的结构,其中包括上述两个结构(队列结构和控制器结构),然后使用malloc动态分配内存。 关键是新客户每次都应进入队列中拥有较少客户的队列。我已经创建了一个名为QueueCheck的函数,但是它并没有使我分配给它的指针显示给较少客户的队列。
队列功能是处理队列的基本功能。 请忽略不是英语的评论,谢谢。 这是代码:
int main(void)
{ Cont_Oura *diaxeirish; /* struct array that includes a queue and it's Controller */
float arrival_possibility; /*pi8anothta afikshs*/
unsigned int simulation_time; /*synolikos xronos prosomoiwshs*/
unsigned int service_time;/*xronos e3yphrethshs enos pelath*/
unsigned int minnum; /* used to randomly generate service time */
unsigned int maxnum; /* used to randomly generate service time */
unsigned int time1; /*roloi prosomoiwshs*/
unsigned int time_left; /*time left for a customer to be serviced*/
unsigned int customer_sum; /*customers that were serviced*/
unsigned int waiting_time; /*synolikos xronos anamonhs*/
unsigned int left_cust; /* customers that were not serviced*/
unsigned int max_cust; /*max number of customers entered the queue */
TSOuras customer; /*The customer in the queue*/
float average; /*average time of customers waiting in the queue*/
float randomArrival;
int controllerInactive=0;
time_t t;
TController Controller;
TOuras queue;
int n, i;
srand(time(&t));
printf("Give number of queues that the simulation will have\n");
scanf("%d", &n);
diaxeirish = (Cont_Oura *)malloc(n * sizeof(Cont_Oura));
if (diaxeirish == NULL){
printf("Not enough space to allocate memory for diaxeirish");
return -1;
}
printf("Give units of time for the simulation (0 <=), arrival possibility in unit of time (0,1), min and max number to calculate (randomly) the service time(same for every queue)\n");
scanf("%u %f %u %u",&simulation_time,&arrival_possibility,&minnum, &maxnum);
getchar();
printf("The simulation will last %4u units of time.\n",simulation_time);
printf("The arrival possibility for a customer in a unit of time is: %4.2f.\n",arrival_possibility);
service_time = (rand() % (maxnum + 1 - minnum) + minnum);
CustomerSetServiceTime(&customer, service_time);
printf("The service time for every customer is %d units of time .\n",CustomerGetServiceTime(customer));
for (i=0; i<=n-1; i++){
QueueCreation(&(diaxeirish->queue)); /* initializes queue */
ControllerCreation(&(diaxeirish->Controller)); /*initializes Controller */
}
QueueCreation(&queue);
ControllerCreation(&Controller);
time1 = 0;
time_left = 0;
waiting_time =0;
i=0;
while( time1 < simulation_time )
{ /* Pelatis- Aytokinhto */
randomArrival = (float)rand()/(float)RAND_MAX;
if ( randomArrival < arrival_possibility ){
CustomerSetEntranceTime(&customer, time1);
QueueCheck(diaxeirish, n, &queue);
if (!QueueIns(&queue, customer)){
printf("Queue is small! The simulation will stop \n");
getchar();
return 0;
};
};
Cont_Oura结构是这个
typedef struct{ /*struct for controller and queue */
TOuras queue;
TController Controller;
}
而QueueCheck函数就是这个:
void QueueCheck(Cont_Oura *diaxeirish, int n, TOuras *queue){ /*the customers start entering from the queue number n, then n-1... */
if (QueueGetSize((diaxeirish+n-1)->queue) == 0 || (QueueGetSize((diaxeirish+n-1)->queue) == QueueGetSize((diaxeirish)->queue))) /* if the last queue has 0 customer or the first and last queue have the same number of customers */
queue = &((diaxeirish+n-1)->queue); /* then add customer to the last queue */
else if (QueueGetSize((diaxeirish+n-2)->queue) < QueueGetSize((diaxeirish+n-1)->queue))
QueueCheck(diaxeirish, n-1, queue);
}