C++ 调用队列的基本操作实现病人看病模拟程序包括排队、就诊、查询、退出等功能

C++ 调用队列的基本操作实现病人看病模拟程序包括排队、就诊、查询、退出等功能

定义常量,根据需要定义队列的长度

const int MaxSize = 20;

队列的结构

typedef struct {//队列的 顺序队结构
	int data[MaxSize];//队列长度
	int front, rear;//队头队尾下标注
}SqQueue;

初始化队列

void InitQueue(SqQueue*& q) {//初始化队列
	q = (SqQueue*)malloc(sizeof(SqQueue));
	q->front = q->rear = -1;
}

销毁队列

void DestroyQueue(SqQueue*& q) {//销毁队列
	free(q);
}

判断队列是否为空

bool QueueEmpty(SqQueue*& q) {//判断队列是否为空
	return(q->front == q->rear);
}

加入队列

bool enQueue(SqQueue*& q, int e) {//进队列
	if (q->rear == MaxSize - 1)
		return false;
	q->rear++;
	q->data[q->rear] = e;
	return true;
}

退出队列

bool deQueue(SqQueue*& q, int& e) {//出队列
	if (q->front == q->rear)
		return false;
	q->front++;
	e = q->data[q->front];
	return true;
}

输出当前队列患者

void SearchQueue(SqQueue*& q) {//输出队列
	if (QueueEmpty(q)) {
		cout << "当前没有患者排队。" << endl;
		return;
	}
	SqQueue* p;
	p = q;
	int show = p->front;
	while (show != p->rear) {
		cout << "当前患者为:";
		cout << p->data[show+1]<< endl;
		show++;
	}
}

主程序

int main() {//主程序
	cout << "病人看病程序" << endl;
	cout << "1.排队" << endl;
	cout << "2.就诊" << endl;
	cout << "3.查询" << endl;
	cout << "4.退出" << endl;
	SqQueue* q= new SqQueue;//新建队列
	InitQueue(q);//初始化队列
	int number = 1;//患者病历号
	int now=1;//出队患者病历号
	int n;//操作选项
	while (1) {
		cout << "请选择:" << endl;
		cin >> n;
		if (n == 1) {//排队,加入队列
			enQueue(q, number);
			cout << "队尾患者的病历号为:";
			cout << number << endl;
			number++;
		}
		if (n == 2) {//就诊,出队
			if (QueueEmpty(q)) {
				cout << "当前队列无患者。"<<endl;
				continue;
			}
			deQueue(q, now);
			cout << "当前就诊患者病历号为:";
			cout << now << endl;;
		}
		if (n == 3) {//输出队列中的患者
			if (QueueEmpty(q)) {
				cout << "当前队列无患者。" << endl;
				continue;
			}
			SearchQueue(q);
		}
		if (n == 4) {//退出程序
			DestroyQueue(q);
			cout << "退出成功。" << endl;
			break;
		}
	}
	return 0;
}

总代码

#include<iostream>
using namespace std;

const int MaxSize = 20;

typedef struct {//队列的 顺序队结构
	int data[MaxSize];
	int front, rear;
}SqQueue;

void InitQueue(SqQueue*& q) {//初始化队列
	q = (SqQueue*)malloc(sizeof(SqQueue));
	q->front = q->rear = -1;
}

void DestroyQueue(SqQueue*& q) {//销毁队列
	free(q);
}

bool QueueEmpty(SqQueue*& q) {//判断队列是否为空
	return(q->front == q->rear);
}

bool enQueue(SqQueue*& q, int e) {//进队列
	if (q->rear == MaxSize - 1)
		return false;
	q->rear++;
	q->data[q->rear] = e;
	return true;
}

bool deQueue(SqQueue*& q, int& e) {//出队列
	if (q->front == q->rear)
		return false;
	q->front++;
	e = q->data[q->front];
	return true;
}

void SearchQueue(SqQueue*& q) {//输出队列
	if (QueueEmpty(q)) {
		cout << "当前没有患者排队。" << endl;
		return;
	}
	SqQueue* p;
	p = q;
	int show = p->front;
	while (show != p->rear) {
		cout << "当前患者为:";
		cout << p->data[show+1]<< endl;
		show++;
	}
}

int main() {//主程序
	cout << "病人看病程序" << endl;
	cout << "1.排队" << endl;
	cout << "2.就诊" << endl;
	cout << "3.查询" << endl;
	cout << "4.退出" << endl;
	SqQueue* q= new SqQueue;
	InitQueue(q);
	int number = 1;
	int now=1;
	int n;
	while (1) {
		cout << "请选择:" << endl;
		cin >> n;
		if (n == 1) {//排队,加入队列
			enQueue(q, number);
			cout << "队尾患者的病历号为:";
			cout << number << endl;
			number++;
		}
		if (n == 2) {//就诊,出队
			if (QueueEmpty(q)) {
				cout << "当前队列无患者。"<<endl;
				continue;
			}
			deQueue(q, now);
			cout << "当前就诊患者病历号为:";
			cout << now << endl;;
		}
		if (n == 3) {//输出队列中的患者
			if (QueueEmpty(q)) {
				cout << "当前队列无患者。" << endl;
				continue;
			}
			SearchQueue(q);
		}
		if (n == 4) {//退出程序
			DestroyQueue(q);
			cout << "退出成功。" << endl;
			break;
		}
	}
	return 0;
}

遗留问题:
1.当患者的数量超出队列的长度的时候没有错误提示或者限制,应当在患者数量超出的时候输出警告并阻止新患者加入队伍。
2.在功能选择的时候,没有对选项进行规范。可以将1234数字之外均判为无效字符,要求用户重新输入。

特别感谢前辈:https://blog.csdn.net/migu77777/article/details/52841370

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值