模拟医院挂号系统

// Hospital.cpp : 定义控制台应用程序的入口点。
/**
病人到医院看病排队看医生的情况,在病人排队过程中,主要重复两件事:
(1)病人到达诊室,将病历交给护士,排到等待队列中侯诊
(2)护士从等待队列中取出下一位病人的病历,该病人进入诊室就诊
要求:模拟病人等待就诊这一过程,程序采用菜单式,其选项和功能说明如下:
(6)	上班——初始化排队队列。
(1)	排队——输入排队病人的病历号,加入到病人排队队列中
(2)	就诊——病人排队队列中最前面的病人就诊,将其从队列中删除
(3)	查看排队——从队首到队尾理出所有的排队病人的病历号
(4)	离下班(30mins),不再排队,余下依次就诊——从队首到队尾列出所有的排队病人的病历号,并退出运行
(5)	下班——退出运行
*/

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct QElemType{
	int num;
}QElemType;
typedef struct QNode{
	QElemType date;
	struct QNode *next;
}QNode,*QueuePtr;
typedef struct LinkQueue{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

int total=0;//一次值班,接待病人总数
int work_time=4;//工作时间,默认为3分钟
int last_num=0;//记录最后一个病历号
void start_work(LinkQueue &Q);
void queue(LinkQueue &Q);
void see_doctor(LinkQueue &Q);
void end_work();
void initQueue(LinkQueue &Q);
void clearQueue(LinkQueue &Q);
void enQueue(LinkQueue &Q,QElemType e);
int deQueue(LinkQueue &Q,QElemType &e);//有病人,返回--0,无,返回--1
void visit(LinkQueue Q);
int _tmain(int argc, _TCHAR* argv[])
{
	int flag=0;
	int flag_work=0;
	LinkQueue Q;
	initQueue(Q);
	printf("1--start work\n");
	printf("2--new comer\n");
	printf("3--see doctor\n");
	printf("4--visit the queue\n");
	printf("5--take off work\n");
	printf("6--just see doctor\n");
	
	while(1)
	{
		printf("What happens now?\n");
		scanf("%d",&flag);
		switch(flag)
		{
		case 1:
			printf("Input work time:");
			scanf("%d",&work_time);
			start_work(Q);
			flag_work=1;
			break;
		case 2:
			if(flag_work)
			{
				queue(Q);
				last_num+=1;
				break;
			}
			else
				printf("Not work time!\n");
		case 3:
			if(flag_work)
				see_doctor(Q);break;
		case 4:
			if(flag_work)
				visit(Q);break;
		case 5:
			if(flag_work)
				end_work();
			system("pause");
			exit(0);
		case 6:
			if(flag_work)
			{
				while(Q.front!=Q.rear)
					see_doctor(Q);
				flag_work=0;
				break;
			}
		default:printf("This is not our responsibility!\n");
		}
		see_doctor(Q);
	}
	return 0;
}
void start_work(LinkQueue &Q)
{
	printf("New day!!\n");
	total=0;
	clearQueue(Q);
}
void queue(LinkQueue &Q)
{
	QElemType e;
	e.num=last_num+1;
	printf("Welcome!!\nYour number is %d,please waiting in patience\n",e.num);
	total++;
	enQueue(Q,e);
}
void see_doctor(LinkQueue &Q)
{
	QElemType e;
	if(deQueue(Q,e)==0)//有病人,返回0
	{
		if(e.num<total)
			printf("NO.%d,it's your turn,good luck!\nNext is NO.%d\n",e.num,e.num+1);
		else if(e.num==total)
			printf("NO.%d,it's your turn,good luck!\n",e.num);
	}
}
void end_work()
{
	printf("I am sorry,but we are get off work today.\n");
	printf("\nToday we got %d people",total);
	if(total>0)
		printf(",big income!!!");
	return;
}
void initQueue(LinkQueue &Q)
{
	Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
	if(!Q.front)
	{
		printf("something is wrong!!");
		exit(0);
	}
	Q.front->next=NULL;
}
void clearQueue(LinkQueue &Q)//清空队列,Q.front==Q.rear==NULL
{
	QueuePtr flag;
	flag=Q.front;
	Q.front=Q.front->next;
	while(Q.front)
	{
		Q.rear=Q.front->next;
		free(Q.front);
		Q.front=Q.rear;
	}
	Q.front=flag;
	Q.front->date.num=0;
}
void enQueue(LinkQueue &Q,QElemType e)
{
	QueuePtr p;
	p=(QueuePtr)malloc(sizeof(QNode));
	if(!p)
	{
		printf("Something is wrong!!");
		exit(0);
	}
	p->date=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
}
int deQueue(LinkQueue &Q,QElemType &e)//有病人,返回--0,无,返回--1
{
	QueuePtr p;
	if(Q.front==Q.rear)//front是head节点
	{
		printf("No patients\n");
		return 1;
	}
	p=Q.front->next;
	e=p->date;
	Q.front->next=p->next;
	if(Q.rear==p)
		Q.rear=Q.front;
	free(p);
	return 0;
}
void visit(LinkQueue Q)
{
	int sum=0;
	QueuePtr p;
	p=Q.front->next;
	
	while(p)
	{
		sum++;
		printf("%d ",p->date.num);
		p=p->next;
	}
	printf("\nThere are %d people waiting now!\n",sum);
}


版权声明:本文为hanchaoqi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。