您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 设计及方案 > 模拟病人到医院排队看病上机内容完整实验报告!
【实训内容】编写一个程序,模拟病人到医院排队看病的情况。【思路】由于医院看病是先到先看,所以可以使用队列来实现排队过程。模拟病人的排队看病主要完成以下几个操作:(1)病人把病历本交到护士手中,相当于进队。(2)排在最前面的病人先看,同时取走病历,这一步相当于出队。(3)查看排队,从队头到队尾依次显示队列中所有的病历号。(4)停止排队,退出程序。【程序源代码】#includestdio.h#includestdio.h#includemalloc.htypedefstructqueue{/*链式队列存储结构*/intdata;/*数据域*/structqueue*next;/*指针域*/}QUEUE;/*链式队列类型名*/QUEUE*front=NULL;/*队头指针*/QUEUE*rear=NULL;/*队尾指针*//*========================================================*//*病例入队*//*========================================================*/voidAddQueue(intx){QUEUE*s;s=(QUEUE*)malloc(sizeof(QUEUE));/*创建结点*/s-data=x;s-next=NULL;if(rear==NULL)/*空队列*/front=s;else/*非空*/rear-next=s;rear=s;}/*========================================================*//*病人就诊,病历出队*//*========================================================*/voidDelQueue(int*e){if(front==NULL)/*队列空*/printf(Queueisempty\n);else{QUEUE*temp;temp=front-next;*e=front-data;free(front);front=temp;}return;}/*========================================================*//*显示病历*//*========================================================*/voidDisplayQueue(){if(rear==NULL){printf(Queueisempty\n);return;}QUEUE*temp=front;printf(目前所有的病历号:);while(temp!=NULL){printf(%d,temp-data);temp=temp-next;}printf(\n);}/*========================================================*//*病人看病的程序*//*========================================================*/voidSeeDoctor(){intselection,number;/*number为病历号*/intexinum;/*exinum用于确认号码是否已有,true表示与之前的号码重复,输入无效*/intstop=0;/*判断病历号是否全部进队,stop=1全部进队,stop=0还有剩余的没有进队*/inte;intflag=1;/*flag=0,停止排队*/QUEUE*temp;while(flag==1){printf(1:排队2:就诊3:查看排队4:停止排队\n);printf(请选择:);scanf(%d,&selection);switch(selection){case1:{while(stop==0)/*排队*/{printf(输入病历号:);do{scanf(%d,&number);if(number==0)break;exinum=0;temp=front;while(temp!=NULL&&exinum==0)/*判断是否有重复的号*/{if(number==temp-data)exinum=1;elsetemp=temp-next;if(exinum==1)printf(输入病历号重复,重新输入\n);}}while(exinum==1);if(number==0)break;elseAddQueue(x);/*号码入队*/}}break;case2:/*看病*/{if(rear==NULL)/*空队列*/printf(目前无人排队\n);else{temp=front;DelQueue(&e);printf(第%d号病人就诊\n,e);}}break;case3:DisplayQueue();/*显示队列中的所有号码*/break;case4:{if(front!=NULL)/*队列不为空*/printf(请排队的病人明天就医!\n);flag=0;/*中止循环*/}break;}}}/*========================================================*//*主函数*//*========================================================*/voidmain(){SeeDoctor();}【运行结果】程序运行结果如图3-22所示。图3-22运行结果
本文标题:模拟病人到医院排队看病上机内容完整实验报告!
链接地址:https://www.777doc.com/doc-4371805 .html