www.pudn.com > gc.rar > 8_4.C
# include# include typedef struct { // 顾客信息结构 int arrive; int treat; }NODE; typedef struct node { NODE data; struct node *link; }LNODE; LNODE *head,*tail; NODE curr,temp; char Fname[120]; FILE *fp; //指向FILE类型结构体的指针变量 /*函数,链接队列进队函数*/ void len_queue(LNODE **hpt,LNODE **tpt,NODE x) { LNODE *p = (LNODE *)malloc(sizeof(LNODE)); p->data = x; p->link = NULL; if (*hpt == NULL) *tpt = *hpt = p; else *tpt = (*tpt)->link = p; } /*函数,链接队列出队函数*/ int lde_queue(LNODE **hpt,LNODE **tpt,NODE *cp) { LNODE *p = *hpt; if (*hpt == NULL) return 1 ; //对空 *cp = (*hpt)->data; *hpt = (*hpt)->link; if (*hpt == NULL) *tpt = NULL; free(p); return 0; } void main() { int dwait = 0, //接待员总等待时间 clock = 0, wait = 0, //顾客总等待时间 count = 0, //顾客总人数 have = 0,finish; printf("Enter file name."); printf("输入同目录下wait.txt的完整物理路径\n"); scanf("%s",Fname); if ((fp = fopen(Fname,"r")) == NULL) { //打开文件 printf("Can't open file %s",Fname); return; } head = NULL; tail=NULL; have = fscanf(fp,"%d%d",&temp.arrive, &temp.treat); /*正确读入,have为2*/ do { /*约定每轮循环,接待一位顾客*/ if (head == NULL && have == 2) { /*等待队列为空,但还有顾客*/ dwait += temp.arrive - clock; /*累计接待员总等待时间 */ clock = temp.arrive; /*时钟推进到暂存顾客的到达时间*/ len_queue(&head,&tail,temp); /*暂存变量中的顾客信息进队*/ have = fscanf(fp,"%d%d",&temp.arrive,&temp.treat); /*fp,文件指针,从磁盘文件上读入*/ } count ++; /*累计顾客人数*/ lde_queue(&head,&tail,&curr); /*出队一个顾客信息*/ wait += clock - curr.arrive; /*累计顾客的总等待时间*/ finish = clock + curr.treat; /*设定接待结束时间*/ while (have == 2 && temp.arrive <= finish) { /*下一位顾客的到达时间在当前顾客接待结束之前*/ len_queue(&head,&tail,temp); /*暂存变量中的顾客信息进队*/ have = fscanf(fp,"%d%d",&temp.arrive,&temp.treat); } clock = finish; /*时钟推进到当前顾客接待结束时间 */ } while(have == 2 || head!=NULL); printf("结果:接待员等待时间%d\n顾客平均等待时间%f\n",dwait,(double)wait/count); printf("模拟总时间:%d,顾客人数:%d,总等待时间:%d\n",clock,count,wait); }