电话客户服务模拟

来源:互联网 发布:杭州淘宝摄影基地室内 编辑:程序博客网 时间:2024/05/01 08:24
/*****************************************************************************题目:电话客户服务模拟问题描述一个模拟时钟提供接听电话服务的时间(以分钟计),然后这个时钟将循环地自增1(分钟),直到到达指定的时间为止。在时钟的每个“时刻”,就会执行一次检查来看看当前电话的服务是否已经完成,如果是,这个电话从电话队列中删除,模拟服务将从队列中取出下一个电话(如果有)继续开始。同时还需要执行一个检查来判断是否有一个新的电话到达,如果有将其到达的时间记录下来,并为其产生一个随机服务时间,这个服务时间也被记录下来,然后将这个电话放入电话队列中,当客户服务人员空闲时,按照先来先服务的方式处理这个队列。当时钟到达指定时间时,不会再接听新电话,但是服务将继续,直到队列中所有电话得到处理为止。要求程序需要处理的初始数据包括:客户服务人员的人数、时间限制,电话到达的速率,平均服务时间。程序产生的结果包括:处理的电话数,每个电话的平均等待时间*****************************************************************************/#include<stdio.h> #include<stdlib.h>#include<time.h> #define OK 1#define ERROR 0#define D (24*60*60)#define H (60*60)#define M (60)typedef int QueueData;typedef struct node{    QueueData time_call,time_serve;       //结点数据    struct node * next;       //结点链指针} QueueNode;typedef struct LinkQueue{    QueueNode *rear, *front;} LinkQueue;LinkQueue* InitQueue (void) {    LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) );    Q->rear=Q->front=NULL;return Q;}int QueueEmpty ( LinkQueue *Q ) {    return Q->front == NULL;}int GetFront ( LinkQueue *Q, QueueData *time_call, QueueData *time_serve){    if ( QueueEmpty (Q) ) return 0;    (*time_call) = Q->front->time_call;     (*time_serve) = Q->front->time_serve; return 1;}int EnQueue ( LinkQueue **Q, QueueData time_call,QueueData time_serve ){    QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) );    p->time_call = time_call;     p->time_serve = time_serve; p->next = NULL;    if ( (*Q)->front == NULL )  //空,创建第一个结点       (*Q)->front = (*Q)->rear = p;    else (*Q)->rear = (*Q)->rear->next = p;    return 1;}int DeQueue ( LinkQueue **Q, QueueData *time_call,QueueData *time_serve ) {                        //删去队头结点,并返回队头元素的值    if ( QueueEmpty (*Q) ) return 0;//判队空    QueueNode *p = (*Q)->front;    (*time_call) = p->time_call;                           //保存队头的值    (*time_serve) = p->time_serve;     (*Q)->front = (*Q)->front->next;  //新队头    if ((*Q)->front == NULL) (*Q)->rear = NULL;    free (p);    return 1;}int now_time(void){time_t t1; time(&t1); long time=t1%D;return time;}int cre_rand(int t){    int tmp;srand((int)time(0));tmp=1+(int)(t*1.0*rand()/(RAND_MAX+1.0));tmp-=t/2;return tmp;}int main(){    int i,empty=0,          //空闲客服数        num_serve,        //客服人数time_limit,       //时间限制tel_speed,        //电话速率tel_speed_tmp,    //电话速率计时time_avg_serve,   //平均服务时间time_wait_count,  //总等待时间count_tel,        //处理电话数    num_serve_left,   //等待接听客服数    time_now;         //当前时间    time_limit=20;count_tel=0;time_wait_count=0;int count[100];    QueueData time_call,time_serve;    LinkQueue *Q;Q=InitQueue();do{printf("input the num of server(>0)\n");scanf("%d",&num_serve);}while(num_serve<=0);do{printf("input the limit of time(>0s)\n");scanf("%d",&time_limit);}while(time_limit<=0);do{printf("input the speed of tel(>0s)\n"); scanf("%d",&tel_speed);}while(tel_speed<=0);do{printf("input the avg time of sreve(>0s)\n");scanf("%d",&time_avg_serve);}while(time_avg_serve<=0);tel_speed_tmp=tel_speed;num_serve_left=num_serve;for(i=0;i<num_serve;i++){    count[i]=0;}system("clear");while(time_limit){    printf("time_limit:%d\n",time_limit);printf("num_serve_left=%d\t",num_serve_left);        for(i=0;i<num_serve;i++){if(count[i]>0) {      count[i]--;  if(count[i]==0)  {  num_serve_left++;  } }printf("ser_left[%d]=%d\t",i,count[i]);    // }printf("\n");    if(tel_speed_tmp==0){time_now=now_time();EnQueue(&Q,time_now,(time_avg_serve+cre_rand(time_avg_serve)));tel_speed_tmp=tel_speed-1;}else{    tel_speed_tmp--;}    while(num_serve_left)        //客服空闲    {        if(!QueueEmpty(Q))       //等待队列未空        {DeQueue(&Q,&time_call,&time_serve);    //对头出队,接通客服count_tel++;for(i=0;i<num_serve;i++){    if(count[i]==0)    {        count[i]=time_serve;    //设置通话时间num_serve_left--;break;    }}time_now=now_time();time_wait_count+=time_now-time_call;        }else{    break;}    }printf("the num of tel is:%d\n",count_tel);sleep(1);system("clear");    time_limit--;}printf("time is over!\n");    //时间限制到while(1)                      //处理剩余电话    {        printf("num_serve_left=%d\t",num_serve_left);        for(i=0;i<num_serve;i++){    if(count[i]==0)    {empty++;if(!QueueEmpty(Q))    //处理等待电话{DeQueue(&Q,&time_call,&time_serve);count_tel++;time_now=now_time();time_wait_count+=time_now-time_call;count[i]=time_serve;num_serve_left--;}    }if(count[i]>0) {      count[i]--;  if(count[i]==0)  {      num_serve_left++;  empty++;  }    printf("ser_left[%d]=%d\t",i,count[i]);  // } }printf("\n");if(empty==num_serve){break;}empty=0;printf("the num of tel is:%d\n",count_tel);sleep(1);system("clear");    }printf("serve is over\n");printf("the num of tel is:%d\n",count_tel);printf("the avg time to wait:%d(s)\n",time_wait_count/count_tel);    return 0;}

原创粉丝点击