-严蔚敏数据结构
-电梯模拟系统的实现
#include<stdio.h>
#include<iostream>
#include<iomanip>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include"HEAD.h"
using namespace std;
void PrintClientInfo(Client p,ClientStatus s){
//输出乘客信息
switch(s) {
case New:cout<<Time<<'\t'<<p.ClientID<<"号乘客进入第"<<p.Infloor<<"层"<<endl;break;
case GiveUp:cout<<Time<<"\t"<<p.ClientID<<"号乘客放弃等待, 等待了"<<Time-p.InTime<<"/0.1s"<<endl;break;
case Out:cout<<Time<<'\t'<<p.ClientID<<"号乘客走出电梯"<<endl;break;
case In:cout<<Time<<'\t'<<p.ClientID<<"号乘客走进电梯,要去第"<<p.Outfloor<<"层"<<endl;break;
default:break;
};
}
Status CreatClient(Client *&p){
//生成新的乘客
p=new Client; //新建乘客类型
if(!p) //新建乘客失败
return OVERFLOW;
p->ClientID=ID++; //产生乘客编号
p->GiveUpTime=rand()%120+30; //产生乘客所能容忍的等待时间
p->InTime=Time; //产生乘客进入电梯系统的时间
InterTime=rand()%300; //产生下一乘客要到达的时间
p->Outfloor=rand()%(Maxfloor+1); //产生乘客所要到达的楼层
//该乘客出现的楼层
while((p->Infloor=rand()%(Maxfloor+1))==p->Outfloor);
//cin>>p->Infloor>>p->Outfloor>>InterTime;
PrintClientInfo(*p,New);
return OK;
}
Status DestoryClient(Client *&p) {
//该乘客离开系统
delete p;
p=NULL;
return OK;
}
Status GoAbove(Client const &e) {
//判断该乘客是否去往高层
if(e.Outfloor>e.Infloor) return TRUE;
else return FALSE;
}
int CInfloor(Client const &e) {
//返回乘客进入的楼层
return e.Infloor;
}
int CInTime(Client const &e) {
//返回乘客进入时间
return e.InTime;
}
int COutfloor(Client const &e) {
//返回乘客进入时间
return e.Outfloor;
}
Status InitQueue(Queue &Q) {
//构造一个空队列Q
Q.front=Q.rear=new QNode;
if(!Q.front) return OVERFLOW;//分配存储失败
Q.front->next=NULL;
Q.front->data=NULL;
return OK;
}
Status DestroyQueue(Queue &Q) {
//销毁队列Q
while(Q.front) {
Q.rear=Q.front->next;
if(Q.front->data) DestoryClient(Q.front->data);
delete Q.front;
Q.front=Q.rear;
}
return OK;
}
Status EnQueue(Queue &Q,QElemType e) {
//插入元素e为Q的新的队尾元素
QueuePtr p;
p=new QNode;
if(!p) return OVERFLOW;
p->data=e;p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(Queue &Q,QElemType &e) {
//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;
//否则返回ERROR
QueuePtr p;
if(Q.front==Q.rear) return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
delete p;