利用 队列 来实现医院挂号模拟看病系统(c++,顺序及链式)

//一,顺序队列(麻烦&&锻炼自己能力)

 #include<iostream>
using namespace std;
typedef struct{
    int *elem;
    char name[15][10]; 
    int front,rear;
}squeue,*s;
int initqueue(squeue &l);
int pushqueue(squeue &l);
int popqueue(squeue &l);
int check(squeue l);
int situation(squeue l);
int i=1;
int main(){
    squeue l;
    int choice;
    while(1)
    { 
        system("cls");
        cout<<"***********************顺序队列***************************"<<endl;
        cout<<"     1-初始化         2-入队               3-出队         "<<endl;
        cout<<"     4-查看病历号     5-查看候诊病人数量   0-退出          "<<endl;
        cout<<"**********************************************************"<<endl;
        cout<<"Enter your choice:";
        cin>>choice;
        switch(choice)
        {
        case 1:
            initqueue(l);
            system("pause");
            break;
        case 2:
            pushqueue(l);
            system("pause");
            break;
        case 3:
            popqueue(l);
            system("pause");
            break;
        case 4:
            check(l);
            system("pause");
            break;
            
        case 5:
            situation(l);
            system("pause");
            break;
        case 0:cout<<"感谢使用,再见";
            return 0;
        default:
            cout<<"输入选项错误,请重新输入!"<<endl;
            system("pause");
            break;
        }
    }
    
    
}
int initqueue(squeue  &l){
    l.elem=new int[15];
    l.front=l.rear=0;
    cout<<"finish"<<endl;
}
int pushqueue(squeue &l){
    if(l.rear-14==l.front){
        cout<<"队列已满,请稍后再试"<<endl; 
        return 0;     }
    cout<<"请输入病人的名字"<<endl; 
    cin>>l.name[l.rear%15];
    
    l.rear++;
    cout<<"ok";
}
int popqueue(squeue &l){
    if(l.front==l.rear){
        cout<<"队空"<<endl;
        return 0; 
    }
    cout<<"下一个病历号是"<<l.front+1<<",名字是"<<l.name[l.front%15]<<endl;
    l.front++;
}
int check(squeue l){
    int j;
    cout<<"请输入需要查找的病历号"<<endl;
    cin >>j;
if(l.front==l.rear){
        cout<<"队空"<<endl;
        return 0; 
        }
        if(l.front==j)
        {
            cout<<"您是下一个"<<endl;
            }    
    if(j<l.front||j>l.rear){
        cout<<"输入错误,请输入正确的病历号"<<endl;
        check(l);
        return 0;
    }
    while(l.front!=j){
    cout<<"下一个病历号是"<<l.front+1<<",名字是"<<l.name[l.front%15]<<endl;
    l.front++;
    }
}
int situation(squeue l){
    if(l.front==l.rear){
        cout<<"队空"<<endl;
        return 0; 
        }
    while(l.front!=l.rear){
    cout<<"下一个病历号是"<<l.front1<<",名字是"<<l.name[l.front%15]<<endl;
    l.front++;
    }
}

二,链式队列(简单&&锻炼自己的链式思维)

 

#include<iostream>
using namespace std;
typedef struct que{
	struct que *next;
	char name[10]; 
	int data;
}S,*linkqueue;

int initqueue(linkqueue &l,linkqueue &rear);//初始化
int pushqueue(linkqueue &l,linkqueue &rear);//入队 
int Popqueue(linkqueue &l,linkqueue &rear);//出队
int check(linkqueue l,linkqueue rear);
int situation(linkqueue l); 
int i=1; 
int main(){
	linkqueue l,rear;
	int choice;
	while(1)
	{
		system("cls");
		cout<<"***********************顺序队列***************************"<<endl;
		cout<<"     1-初始化         2-入队         	  3-出队         "<<endl;
		cout<<"     4-查看病历号     5-查看候诊病人数量   0-退出 	     "<<endl;
		cout<<"**********************************************************"<<endl;
		cout<<"Enter your choice:";
		cin>>choice;
		switch(choice)
		{
		case 1:
			initqueue(l,rear);
			system("pause");
			break;
		case 2:
			pushqueue(l,rear);
			system("pause");
			break;
		case 3:
			Popqueue(l,rear);
			system("pause");
			break;
		case 4:
			check(l,rear);
			system("pause");
			break;
			
		case 5:
			situation(l);
			system("pause");
			break;
	
		case 0:cout<<"感谢使用,再见";
			return 0;
		default:
			cout<<"输入选项错误,请重新输入!"<<endl;
			system("pause");
			break;
		}
	}
}
int initqueue(linkqueue &l,linkqueue &rear){
	l=new S;
	rear=l;
	cout<<"ok"<<endl;
} 
int pushqueue(linkqueue &l,linkqueue &rear){
	linkqueue p;
	int e;
	p=new S;
	cout<<"请输入患者的名字"<<endl;
	cin>>p->name;
	p->data=i;
	p->next=NULL;
	i++;
	rear->next=p;
	rear=rear->next;
}
int Popqueue(linkqueue &l,linkqueue &rear){
	if(!l){
		cout<<"无患者"<<endl;
		return 0; 
	}
	cout<<"下一位患者病历号是"<<l->next->data<<",名字是"<<l->next->name<<endl;
	l=l->next;
}
int check(linkqueue l,linkqueue rear){
	cout<<"请输入病历号"<<endl;
	l=l->next;
	int j=l->data,x=0,flag=0;
	cin>>x;
	if(j==x){
	cout<<"您是下一位,前面无患者"<<endl; 
	}
	
	if(x<l->data||x>rear->data){
		
		cout<<"病历号输入错误,请继续输入"<<endl;
		check(l,rear); 
		return 0; 
	}
	while(x!=j){
		flag=1;
		cout<<"患者病历号是"<<l->data<<",名字是"<<l->name<<endl;
		l=l->next;
		j++;
	}
}
int situation(linkqueue l){
	l=l->next;
	if(l)
	{
		while(l){
		cout<<"患者病历号是"<<l->data<<",名字是"<<l->name<<endl;
		l=l->next;
				}
	}
	else
	cout<<"无患者"<<endl; 
}

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是一个利用队列和计时器实现电梯模拟系统简单运行的 C++ 代码: ``` #include <iostream> #include <queue> #include <chrono> #include <thread> using namespace std; enum Direction { UP, DOWN, STAY }; class Elevator { public: Elevator(int floors) : floors(floors), currentFloor(1), direction(STAY), doorOpen(true) {} void update() { if (direction == UP) { currentFloor++; } else if (direction == DOWN) { currentFloor--; } if (currentFloor == targetFloor) { openDoor(); direction = STAY; requests.pop(); } if (requests.empty()) { direction = STAY; } else if (requests.front() > currentFloor) { direction = UP; } else if (requests.front() < currentFloor) { direction = DOWN; } } void addRequest(int floor) { requests.push(floor); if (direction == STAY) { if (floor > currentFloor) { direction = UP; } else if (floor < currentFloor) { direction = DOWN; } } } bool isDoorOpen() { return doorOpen; } private: int floors; int currentFloor; int targetFloor; Direction direction; bool doorOpen; queue<int> requests; void openDoor() { doorOpen = true; cout << "Opening door on floor " << currentFloor << endl; this_thread::sleep_for(chrono::seconds(2)); // 模拟电梯门开启2秒 } void closeDoor() { doorOpen = false; cout << "Closing door on floor " << currentFloor << endl; this_thread::sleep_for(chrono::seconds(1)); // 模拟电梯门关闭1秒 } }; int main() { Elevator elevator(10); // 创建一个10层的电梯 // 添加一些请求 elevator.addRequest(5); elevator.addRequest(2); elevator.addRequest(8); // 模拟电梯运行 while (!elevator.isDoorOpen()) { elevator.update(); } elevator.closeDoor(); // 等待2秒,模拟乘客进出电梯的过程 this_thread::sleep_for(chrono::seconds(2)); // 模拟电梯运行 while (!elevator.isDoorOpen()) { elevator.update(); } elevator.closeDoor(); // 等待2秒,模拟乘客进出电梯的过程 this_thread::sleep_for(chrono::seconds(2)); // 模拟电梯运行 while (!elevator.isDoorOpen()) { elevator.update(); } elevator.closeDoor(); return 0; } ``` 这段代码与之前的代码类似,但是在打开和关闭电梯门时使用了计时器来模拟电梯门的开启和关闭过程。在每次打开电梯门时,我们使用 `this_thread::sleep_for` 函数来暂停程序的执行,模拟电梯门开启一段时间后再关闭。在这个例子中,我们假设电梯门需要开启2秒,关闭1秒。在每个等待时间结束后,我们继续模拟电梯的运行过程。 注意,使用计时器模拟电梯门的开启和关闭过程可能会导致程序的执行时间不稳定,因此在实际应用中需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

だふ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值