电梯模拟源代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
}
for(i=0;i<n;i=i+4)
{
if(Temp[i+3]==-3)
{LiftUp(fp,Temp[i],Temp[i+1],Temp[i+2]);
Temp[i]=-1;Temp[i+1]=-1;Temp[i+2]=-1;Temp[i+3]=-1;}
else if(Temp[i+3]==-2)
RequestShow(fp,Time,time);
fprintf(fp,"在(%dt-->%dt)时间之中正在打开电梯门(在第%d层)\n",Time,time,LiftNow);
Time=time;
}
void LiftShut(FILE *fp)
{
int time;
time=Time+OpenDoor;
PassengerInLift[i]=-1;
for(i=0;i<100;i++)
Temp[i]=-1;
printf("输入读入文件的名字:");
scanf("%s",infile);
if((fp=fopen(infile,"r"))==NULL)
{
printf("无法打开文件\n");
exit(0);
{Up=1,Down=0;break;}
else {Down=1,Up=0;break;}
}
for(j=1;j<24;j=j+2)//找到从第一层上电梯的人要去的最近的楼层
{
if(PassengerInLift[j]!=-1&&PassengerInLift[j]<small)
small=PassengerInLift[j];
{
int i,j;
int flag=0;
LQNode I;
for(i=0;i<9;i++)
{
if(QueueNotEmpty(Lift[i]))
{
QueueGet(Lift[i],&I);
while(1)
{
if(I.AskTime>=NowTime&&I.AskTime<=FinalTime)
{
flag=1;
n=i;
for(i=0;i<n;i=i+4)
{
small=i;
for(j=i+4;j<n;j=j+4)
if(Temp[j]<Temp[small]) small=j;
if(small!=i)
{
temp[0]=Temp[i];temp[1]=Temp[i+1];temp[2]=Temp[i+2];temp[3]=Temp[i+3];
}
while(!feof(fp))
{
fscanf(fp,"%d",&data[j]);
j++;
}
fclose(fp);
p=j;
for(j=0;j<p-1;j++)
{
if(j%4==1)
{
I.Num=data[j-1];
I.WaitFloor=data[j];
I.TargetFloor=data[j+1];
else I.sign=-1;
QueueAppend(&Passenger[i],&I);
for(j=0;j<Max;j=j+4)
{
if(Temp[j]==-1)//说明temp为空
{
Temp[j]=I.AskTime;
Temp[j+1]=I.Num;
Temp[j+2]=I.WaitFloor;
Temp[j+3]=I.TargetFloor;
RequestShow(fp,Time,time);
Time=time;}
}
else break;
}
else break;
}
}
void TestLift(FILE *fp)
{
int time;
time=Time+LiftTest;
if(RequestShow(fp,Time,time))
{
while(1)
}
}
for(j=1;j<24;j=j+2)
for(i=1;i<24;i=i+2)
if(PassengerInLift[i]==-1) break;
PassengerInLift[i]=I.TargetFloor;
PassengerInLift[i-1]=I.Num;
PassengerIn(fp,I.Num);
time=Time+InLift;
PassengerInLift[i-1]=-1;
flag=1;}
}
if(flag==1)
{
time=Time+OutLift;
RequestShow(fp,Time,time);
Time=time;
}
}
int LiftNotFull()
{
int i;
for(i=1;i<24;i=i+2)
if(PassengerInLift[i]==-1) return 1;
if(QueueNotEmpty(Lift[i]))
{
QueueGet(Lift[i],&I);
while(1)
{
if(I.AskTime>=NowTime&&I.AskTime<=FinalTime)
{
flag=1;
QueueDelete(&Lift[i],&I);
if(I.WaitFloor<I.TargetFloor) I.sign=1;
{
Temp[j]=I.AskTime;
Temp[j+1]=I.Num;
Temp[j+2]=I.WaitFloor;
Temp[j+3]=I.TargetFloor;
break;
}
}
}
else break;
if(QueueNotEmpty(Lift[i])) QueueGet(Lift[i],&I);
else break;
}
else if(small-1>0) {Up=1,Down=0;break;}
else if(small-1<0) {Up=0,Down=1;break;}
}
}
break;
}
else
{
fprintf(fp,"在%dt时刻电梯停靠在第一层\n",Time);
}
}
}
void LiftUpRun(FILE *fp)
{
Time++;
if(RequestShow(fp,Time,Time))
{
if(QueueNotEmpty(Passenger[1]))//第一层可能进来人
{
LiftOpen(fp);
GoinLift(fp);
TestLift(fp);
LiftShut(fp);
}
if(QueueNotEmpty(Passenger[2]))
Temp[i]=-1;Temp[i+1]=-1;Temp[i+2]=-1;Temp[i+3]=-1;}
}
}
int RequestShow(FILE *fp,int NowTime,int FinalTime)
{
int i,j;
int flag=0;
LQNode I;
for(i=0;i<9;i++)
{
{
if(QueueNotEmpty(Passenger[LiftNow]))
{Time=time;GoinLift(fp);}
else {Time=time;break;}
}
}
}
void LiftStop1(FILE *fp)
{
int i,j,smalwk.baidu.com=8;
LQNode I;
LiftNow=1;
while(1)
RequestShow(fp,Time,time);
fprintf(fp,"在(%dt-->%dt)时间之中电梯门正在关闭(在第%d层)\n",Time,time,LiftNow);
Time=time;
}
//三个队列的初始化将输入文本中的数据按照等待层数依次写入Lift[]中且按照时间排序时间相同者按照输入文件先后顺序排序
QueueDelete(&Lift[i],&I);
if(I.WaitFloor<I.TargetFloor) I.sign=1;
else I.sign=-1;
QueueAppend(&Passenger[i],&I);
for(j=0;j<Max;j=j+4)
{
if(Temp[j]==-1)//说明temp为空
I.AskTime=data[j+2];
I.sign=0;
QueueAppend(&Lift[data[j]],&I);
}
}
}
//在某一个时间段之间是否有请求如有输出此请求并且Lift出队列Passenger入队列返回1有请求
int Request(FILE *fp,int NowTime,int FinalTime)
{
int i,j,Small=8,time;
LQNode I;
for(i=LiftNow;i<9;i++)
{
if(QueueNotEmpty(Passenger[i]))
{
QueueGet(Passenger[i],&I);
if(I.WaitFloor<Small)
Small=I.WaitFloor;
return 0;
}
void GoinLift(FILE *fp)//规定不管方向如何人们都会选择进入电梯等待
{
int i,time;
LQNode I;
while(1)
{
if(QueueNotEmpty(Passenger[LiftNow]))
{
QueueGet(Passenger[LiftNow],&I);
Temp[i]=Temp[small];Temp[i+1]=Temp[small+1];Temp[i+2]=Temp[small+2];Temp[i+3]=Temp[small+3];
Temp[small]=temp[0];Temp[small+1]=temp[1];Temp[small+2]=temp[2];Temp[small+3]=temp[3];
void LiftInitiate()
{
FILE *fp;
char infile[15];
LQNode I;
int data[1000],p,i,j=0;
for(i=0;i<9;i++)
{
QueueInitiate(&Lift[i]);
QueueInitiate(&Passenger[i]);
}
for(i=0;i<24;i++)
电梯模拟源代码
#include<stdio.h>
#include<stdlib.h>
#include"que.h"
#include"elevator.h"
void Show(FILE *fp)
{
int i,j,small,n;
int temp[4];
for(i=0;i<Max;i=i+4)
if(Temp[i]==-1) break;
{
QueueGet(Passenger[2],&I);
if(I.sign==1)
{Up=1,Down=0;break;}
else {Down=1,Up=0;break;}
}
if(QueueNotEmpty(Passenger[0]))
{
QueueGet(Passenger[0],&I);
if(I.sign==1)
break;
}
}
}
else break;
if(QueueNotEmpty(Lift[i])) QueueGet(Lift[i],&I);
else break;
}
}
}
Show(fp);
return flag;
}
void LiftOpen(FILE *fp)
{
int time;
time=Time+OpenDoor;
}
for(i=3;i<9;i++)
{
if(QueueNotEmpty(Passenger[i]))
{
QueueGet(Passenger[i],&I);
if(I.WaitFloor<small)
{
if(I.sign==1)
{Up=1,Down=0;break;}
else {Down=1,Up=0;break;}
if(LiftNotFull())
{
if(Time-I.AskTime>PassengerMaxTime)
{fprintf(fp,"在%dt时刻由于超过最大等待时间%d号乘客已离开!\n",Time,I.Num);QueueDelete(&Passenger[LiftNow],&I);}
else
{QueueDelete(&Passenger[LiftNow],&I);
{LiftDown(fp,Temp[i],Temp[i+1],Temp[i+2]);
Temp[i]=-1;Temp[i+1]=-1;Temp[i+2]=-1;Temp[i+3]=-1;}
else
{PassengerAsk(fp,Temp[i+2],Temp[i+3],Temp[i+1],Temp[i]);
}
}
}
return flag;
}
void LeaveLift(FILE *fp)
{
int i,time,flag=0;
for(i=1;i<24;i=i+2)
{
if(PassengerInLift[i]==LiftNow)
{PassengerInLift[i]=-1;
PassengerOut(fp,PassengerInLift[i-1]);
}
for(i=0;i<n;i=i+4)
{
if(Temp[i+3]==-3)
{LiftUp(fp,Temp[i],Temp[i+1],Temp[i+2]);
Temp[i]=-1;Temp[i+1]=-1;Temp[i+2]=-1;Temp[i+3]=-1;}
else if(Temp[i+3]==-2)
RequestShow(fp,Time,time);
fprintf(fp,"在(%dt-->%dt)时间之中正在打开电梯门(在第%d层)\n",Time,time,LiftNow);
Time=time;
}
void LiftShut(FILE *fp)
{
int time;
time=Time+OpenDoor;
PassengerInLift[i]=-1;
for(i=0;i<100;i++)
Temp[i]=-1;
printf("输入读入文件的名字:");
scanf("%s",infile);
if((fp=fopen(infile,"r"))==NULL)
{
printf("无法打开文件\n");
exit(0);
{Up=1,Down=0;break;}
else {Down=1,Up=0;break;}
}
for(j=1;j<24;j=j+2)//找到从第一层上电梯的人要去的最近的楼层
{
if(PassengerInLift[j]!=-1&&PassengerInLift[j]<small)
small=PassengerInLift[j];
{
int i,j;
int flag=0;
LQNode I;
for(i=0;i<9;i++)
{
if(QueueNotEmpty(Lift[i]))
{
QueueGet(Lift[i],&I);
while(1)
{
if(I.AskTime>=NowTime&&I.AskTime<=FinalTime)
{
flag=1;
n=i;
for(i=0;i<n;i=i+4)
{
small=i;
for(j=i+4;j<n;j=j+4)
if(Temp[j]<Temp[small]) small=j;
if(small!=i)
{
temp[0]=Temp[i];temp[1]=Temp[i+1];temp[2]=Temp[i+2];temp[3]=Temp[i+3];
}
while(!feof(fp))
{
fscanf(fp,"%d",&data[j]);
j++;
}
fclose(fp);
p=j;
for(j=0;j<p-1;j++)
{
if(j%4==1)
{
I.Num=data[j-1];
I.WaitFloor=data[j];
I.TargetFloor=data[j+1];
else I.sign=-1;
QueueAppend(&Passenger[i],&I);
for(j=0;j<Max;j=j+4)
{
if(Temp[j]==-1)//说明temp为空
{
Temp[j]=I.AskTime;
Temp[j+1]=I.Num;
Temp[j+2]=I.WaitFloor;
Temp[j+3]=I.TargetFloor;
RequestShow(fp,Time,time);
Time=time;}
}
else break;
}
else break;
}
}
void TestLift(FILE *fp)
{
int time;
time=Time+LiftTest;
if(RequestShow(fp,Time,time))
{
while(1)
}
}
for(j=1;j<24;j=j+2)
for(i=1;i<24;i=i+2)
if(PassengerInLift[i]==-1) break;
PassengerInLift[i]=I.TargetFloor;
PassengerInLift[i-1]=I.Num;
PassengerIn(fp,I.Num);
time=Time+InLift;
PassengerInLift[i-1]=-1;
flag=1;}
}
if(flag==1)
{
time=Time+OutLift;
RequestShow(fp,Time,time);
Time=time;
}
}
int LiftNotFull()
{
int i;
for(i=1;i<24;i=i+2)
if(PassengerInLift[i]==-1) return 1;
if(QueueNotEmpty(Lift[i]))
{
QueueGet(Lift[i],&I);
while(1)
{
if(I.AskTime>=NowTime&&I.AskTime<=FinalTime)
{
flag=1;
QueueDelete(&Lift[i],&I);
if(I.WaitFloor<I.TargetFloor) I.sign=1;
{
Temp[j]=I.AskTime;
Temp[j+1]=I.Num;
Temp[j+2]=I.WaitFloor;
Temp[j+3]=I.TargetFloor;
break;
}
}
}
else break;
if(QueueNotEmpty(Lift[i])) QueueGet(Lift[i],&I);
else break;
}
else if(small-1>0) {Up=1,Down=0;break;}
else if(small-1<0) {Up=0,Down=1;break;}
}
}
break;
}
else
{
fprintf(fp,"在%dt时刻电梯停靠在第一层\n",Time);
}
}
}
void LiftUpRun(FILE *fp)
{
Time++;
if(RequestShow(fp,Time,Time))
{
if(QueueNotEmpty(Passenger[1]))//第一层可能进来人
{
LiftOpen(fp);
GoinLift(fp);
TestLift(fp);
LiftShut(fp);
}
if(QueueNotEmpty(Passenger[2]))
Temp[i]=-1;Temp[i+1]=-1;Temp[i+2]=-1;Temp[i+3]=-1;}
}
}
int RequestShow(FILE *fp,int NowTime,int FinalTime)
{
int i,j;
int flag=0;
LQNode I;
for(i=0;i<9;i++)
{
{
if(QueueNotEmpty(Passenger[LiftNow]))
{Time=time;GoinLift(fp);}
else {Time=time;break;}
}
}
}
void LiftStop1(FILE *fp)
{
int i,j,smalwk.baidu.com=8;
LQNode I;
LiftNow=1;
while(1)
RequestShow(fp,Time,time);
fprintf(fp,"在(%dt-->%dt)时间之中电梯门正在关闭(在第%d层)\n",Time,time,LiftNow);
Time=time;
}
//三个队列的初始化将输入文本中的数据按照等待层数依次写入Lift[]中且按照时间排序时间相同者按照输入文件先后顺序排序
QueueDelete(&Lift[i],&I);
if(I.WaitFloor<I.TargetFloor) I.sign=1;
else I.sign=-1;
QueueAppend(&Passenger[i],&I);
for(j=0;j<Max;j=j+4)
{
if(Temp[j]==-1)//说明temp为空
I.AskTime=data[j+2];
I.sign=0;
QueueAppend(&Lift[data[j]],&I);
}
}
}
//在某一个时间段之间是否有请求如有输出此请求并且Lift出队列Passenger入队列返回1有请求
int Request(FILE *fp,int NowTime,int FinalTime)
{
int i,j,Small=8,time;
LQNode I;
for(i=LiftNow;i<9;i++)
{
if(QueueNotEmpty(Passenger[i]))
{
QueueGet(Passenger[i],&I);
if(I.WaitFloor<Small)
Small=I.WaitFloor;
return 0;
}
void GoinLift(FILE *fp)//规定不管方向如何人们都会选择进入电梯等待
{
int i,time;
LQNode I;
while(1)
{
if(QueueNotEmpty(Passenger[LiftNow]))
{
QueueGet(Passenger[LiftNow],&I);
Temp[i]=Temp[small];Temp[i+1]=Temp[small+1];Temp[i+2]=Temp[small+2];Temp[i+3]=Temp[small+3];
Temp[small]=temp[0];Temp[small+1]=temp[1];Temp[small+2]=temp[2];Temp[small+3]=temp[3];
void LiftInitiate()
{
FILE *fp;
char infile[15];
LQNode I;
int data[1000],p,i,j=0;
for(i=0;i<9;i++)
{
QueueInitiate(&Lift[i]);
QueueInitiate(&Passenger[i]);
}
for(i=0;i<24;i++)
电梯模拟源代码
#include<stdio.h>
#include<stdlib.h>
#include"que.h"
#include"elevator.h"
void Show(FILE *fp)
{
int i,j,small,n;
int temp[4];
for(i=0;i<Max;i=i+4)
if(Temp[i]==-1) break;
{
QueueGet(Passenger[2],&I);
if(I.sign==1)
{Up=1,Down=0;break;}
else {Down=1,Up=0;break;}
}
if(QueueNotEmpty(Passenger[0]))
{
QueueGet(Passenger[0],&I);
if(I.sign==1)
break;
}
}
}
else break;
if(QueueNotEmpty(Lift[i])) QueueGet(Lift[i],&I);
else break;
}
}
}
Show(fp);
return flag;
}
void LiftOpen(FILE *fp)
{
int time;
time=Time+OpenDoor;
}
for(i=3;i<9;i++)
{
if(QueueNotEmpty(Passenger[i]))
{
QueueGet(Passenger[i],&I);
if(I.WaitFloor<small)
{
if(I.sign==1)
{Up=1,Down=0;break;}
else {Down=1,Up=0;break;}
if(LiftNotFull())
{
if(Time-I.AskTime>PassengerMaxTime)
{fprintf(fp,"在%dt时刻由于超过最大等待时间%d号乘客已离开!\n",Time,I.Num);QueueDelete(&Passenger[LiftNow],&I);}
else
{QueueDelete(&Passenger[LiftNow],&I);
{LiftDown(fp,Temp[i],Temp[i+1],Temp[i+2]);
Temp[i]=-1;Temp[i+1]=-1;Temp[i+2]=-1;Temp[i+3]=-1;}
else
{PassengerAsk(fp,Temp[i+2],Temp[i+3],Temp[i+1],Temp[i]);
}
}
}
return flag;
}
void LeaveLift(FILE *fp)
{
int i,time,flag=0;
for(i=1;i<24;i=i+2)
{
if(PassengerInLift[i]==LiftNow)
{PassengerInLift[i]=-1;
PassengerOut(fp,PassengerInLift[i-1]);