主题:[讨论]急求高手帮忙改一下这个电梯的程序...
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winbase.h>
#include <windows.h>
#include <time.h>
#include "struct.h"
#include "configure.h"
#include "control.h"
#include "getinput.h"
#include "getoutput.h"
#include "lift_status.h"
#include "writemessage.h"
void configure(void);
void getinput(void); //线程1要运行的函数
void getoutput(void); //线程2要运行的函数
void creatthread()
{
DWORD ThreadID = 1;
//创建线程input与output
HANDLE hRead = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getinput,NULL,0,&ThreadID);
DWORD ThreadID2 = 2;
HANDLE hRead2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getoutput,NULL,0,&ThreadID2);
}
FLOORPTR *supPtr=NULL;
FLOORPTR *sdownPtr=NULL;
FLOORPTR *scallPtr=NULL;
int waittime,go,currentfloor,aimfloor,runtime,isrepuire,direction;
char status;
main()
{
configure();//初始化变量
creatthread();//创建线程
while (1){}
system("PAUSE");
return 0;
}
[color=0000FF]struct.h[/color]
struct Floor{
int floor;
struct floor* nextPtr;
};
typedef struct Floor FLOOR;
typedef FLOOR * FLOORPTR;
[color=0000FF]configure.h[/color]
#include <stdio.h>
#include <stdlib.h>
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
void configure(void)
{
waittime=0;//停靠时间:
go=0;//是否收到go请求:
currentfloor=1;//当前层:
aimfloor=0;//目标层;
runtime=0;//运行时间;
status='v'; //运行状态::v空闲;r运行;s停靠
isrequire=0;//存在请求:
direction=0; //运行方向:1向上2向下0为无动作
}
[color=0000FF]getinput.h[/color]
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winbase.h>
#include <windows.h>
#include <time.h>
#include "struct.h"
extern FLOORPTR *supPtr;
extern FLOORPTR *sdownPtr;
extern FLOORPTR *scallPtr;
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
void insert(FLOORPTR,int);
void getinput()
{
char x;
int floor; //x存储代表请求类型的字符,floor存储代表楼层的数字
while(1)
{
printf("请输入命令:\nu-向上请求\nd-向下请求\nc-内部呼叫请求\ngo-运行\n1-9 楼层\n");
scanf("%c%d",&floor);
switch(x){
case'u':
insert(*supPtr,floor);
break;
case'd':
insert(*sdownPtr,floor);
break;
case'c':
insert(*scallPtr,floor);
break;
case'g':
go=1;
break;
}
}
}
void insert(FLOORPTR *sPtr,int newfloor)
{
FLOORPTR newPtr,previousPtr,currentPtr;
newPtr=malloc (sizeof (FLOORPTR));
if(newPtr!=NULL)
{
newPtr->floor = newfloor;
newPtr->nextPtr = NULL;
previousPtr=NULL;
currentPtr=*sPtr; //初始化
while((currentPtr!=NULL)&&(newfloor>currentPtr->floor))//对链表进行排序
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(previousPtr==NULL)//新楼层排在第一位,重置链表表头
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
if(newfloor==currentPtr->floor)//此节点已经存在,即此楼层已存在请求
{free(newPtr);}//忽略新请求,释放链表
else//将新请求插入链表
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
}
}
else
{
printf("%d floor not inserted.No memory available.\n",newfloor);
}
}
[color=0000FF]control.h[/color]
#include <stdio.h>
#include <stdlib.h>
#include "struct.h"
extern FLOORPTR *supPtr;
extern FLOORPTR *sdownPtr;
extern FLOORPTR *scallPtr;
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
int exist_require(FLOORPTR,FLOORPTR,FLOORPTR);
int Partua(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
int Partub(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
void Partuc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
int Partda(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
int Partdb(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
void Partdc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
void Control(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
isrequire=exist_require(*supPtr,*sdownPtr,*scallPtr);
switch(status)
{
case'r'://运行
if(direction==1)//运行状态向上
{
while(upPtr->nextPtr!=NULL)
{
if(currentfloor<((upPtr->floor)+1))
{
aimfloor=upPtr->floor;
break;
}
else
{
upPtr=upPtr->nextPtr;
}
}
}
else//运行状态向下
{
while(upPtr->nextPtr!=NULL)
{
if((upPtr->floor)<(currentfloor-1))
{
aimfloor=upPtr->floor;
break;
}
else
{
upPtr=upPtr->nextPtr;
}
}
}
break;
case's'://停靠
if(direction==1)//运行方向向上
{
if(Partua(supPtr,sdownPtr,scallPtr)==0)//第一种情况不存在,即没有比当前层搞的楼层有向上呼叫或者目标
{
if(Partub(*supPtr,*sdownPtr,*scallPtr)==0)//第二种情况不存在,即没有向下呼叫或者目标
{
Partuc(*supPtr,*sdownPtr,*scallPtr);//以最低的向上呼叫所在楼层为电梯当前的目标楼层
}
}
}
else//运行方向向下
{
if(Partda(*supPtr,*sdownPtr,*scallPtr)==0)//第一种情况不存在,即没有比当前层低的楼层有向下呼叫或者目标
{
if(Partdb(*supPtr,*sdownPtr,*scallPtr)==0)//第二种情况不存在,即没有向上呼叫或者目标
{
Partdc(*supPtr,*sdownPtr,*scallPtr);//以最低的向上呼叫所在楼层为电梯当前的目标楼层
}
}
}
break;
case'v'://空闲
if(isrequire==1)//存在请求
{status='r';} //进入运行状态
break;
}
}
int exist_require(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
if(upPtr!=NULL || downPtr!=NULL || callPtr!=NULL)
{return 1;}
else
{return 0;}
}
int Partua(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
int temp1=0,temp2=0;//暂存楼层
if(upPtr->floor > currentfloor)//最低向上请求比当前层高
{temp1 = upPtr->floor;}
else //最低向上请求比当前层低
{
while(upPtr->nextPtr != NULL) //寻找比当前层高的最低向上请求
{
upPtr=upPtr->nextPtr;
if(upPtr->floor > currentfloor)
{
temp1 = upPtr->floor;
break;
}
}
}
if(callPtr->floor > currentfloor)//确定最低的比当前层高的目标楼层
{temp2 = callPtr->floor;}
else
{
while(callPtr->nextPtr != NULL)
{
callPtr = callPtr->nextPtr;
if(callPtr->floor > currentfloor)
{
temp2=callPtr->floor;
break;
}
}
}
if(temp1!=0 || temp2!=0)
{
if(temp1>temp2) //目标设为向上呼叫和目标中的最小值
{aimfloor=temp2;}
else
{aimfloor=temp1;}
return 1;
}
else //没有比当前层搞的楼层有向上呼叫或者目标
{return 0;}
}
int Partub(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
if(downPtr->nextPtr==NULL && callPtr->nextPtr==NULL)//没有向下呼叫或者目标
{return 0;}
else
{
while(downPtr->nextPtr!=NULL)//寻找最高向下呼叫或者目标
{downPtr = downPtr->nextPtr;}
while(callPtr->nextPtr != NULL)
{callPtr = callPtr->nextPtr;}
if(downPtr->floor > callPtr->floor) //设置最高楼层为目标
{aimfloor = downPtr->floor;}
else
{aimfloor = callPtr->floor;}
return 1;
}
}
void Partuc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
aimfloor=upPtr->floor;
}
int Partda(FLOOR* upPtr,FLOOR* downPtr,FLOOR* callPtr)
{
int temp1=0,temp2=0;
if(downPtr->floor<currentfloor || callPtr->floor <currentfloor)
{
while(downPtr->floor < currentfloor)//找最高低于当前层的向下呼叫或者目标
{
temp1=downPtr->floor;
downPtr=downPtr->nextPtr;
}
while(callPtr->floor <currentfloor)
{
temp2=callPtr->floor;
callPtr=callPtr->nextPtr;
}
if(temp1>=temp2)
{aimfloor=temp1;}
else
{aimfloor=temp2;}
return 1;
}
else//没有比当前层低的楼层有向下呼叫或者目标
{return 0;}
}
int Partdb(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
int temp1=0,temp2=0;
if(upPtr->nextPtr!=NULL || callPtr->nextPtr!=NULL)
{
if(upPtr->nextPtr != NULL)
{temp1=upPtr->floor;}
if(callPtr->nextPtr != NULL)
{temp2=callPtr->floor;}
if(temp1>=temp2)
{aimfloor=temp2;}
else
{aimfloor=temp1;}
return 1;
}
else//没有向上呼叫或者目标
{return 0;}
}
void Partdc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
while(downPtr->nextPtr != NULL)
{downPtr=downPtr->nextPtr;}
aimfloor=downPtr->floor;
}
lift_status.h
#include <stdio.h>
#include <stdlib.h>
extern FLOORPTR *supPtr;
extern FLOORPTR *sdownPtr;
extern FLOORPTR *scallPtr;
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
int extream (FLOORPTR,FLOORPTR,FLOORPTR);
void clear(FLOORPTR sptr);
void Lift_status(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
switch(status)
{
case's':
if(isrequire == 1) //存在请求
{
if(waittime == 0) //停靠时间为0,即刚停靠
{
if(extream(*supPtr,*sdownPtr,*scallPtr) == 1) //电梯停在所有请求中的最高层或最底层
{
clear(*scallPtr);
upPtr=upPtr->nextPtr;
downPtr=downPtr->nextPtr;//呼叫请求清空,当前层清空
}
else
{
callPtr=callPtr->nextPtr;
upPtr=upPtr->nextPtr;
downPtr=downPtr->nextPtr;//当前层清空
}
waittime++;//停靠时间推进一秒
}
else
{
if(waittime == 5) //停靠时间到达
{
status='r';
waittime==0; //改变电梯状态,停靠时间重置
}
else
{
waittime++; //电梯无动作,停靠时间推进
}
}
if(go == 1)
{
waittime=0;
status='r';
go=0;
}
}
else //is_require != 1
{
status='v';
direction=0;
aimfloor=0;
}
break;
case'r':
if(currentfloor == aimfloor) // 到达目标楼层
{
status='s';
}
else //电梯还在运行
{
if(runtime==5) //运行了一层
{
switch(direction){
case'1':
currentfloor++; //当前层推进一层
break;
case'2':
currentfloor--; //当前层减少一层
break;
}
runtime=0; //任务完成,运行时间重置
}
else //继续推进时间
{
runtime++;
}
}
break;
case'v':
if(isrequire == 1) //存在请求
{
status='r';
}
break;
}
}
int extream (FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
int max1,max2,min1,min2;
if(direction==1)
{
max1=upPtr->floor;
while(upPtr->nextPtr!=NULL)
{
upPtr=upPtr->nextPtr;
max1=upPtr->floor;
}
max2=callPtr->floor;
while(callPtr->nextPtr!=NULL)
{
callPtr=callPtr->nextPtr;
max2=callPtr->floor;
}
if(max1>currentfloor||max2>currentfloor)
{
return 0;
}
else
{
return 1;
}
}
else
{
min1=downPtr->floor;
while(downPtr->nextPtr!=NULL)
{
downPtr=downPtr->nextPtr;
min1=callPtr->floor;
}
min2=callPtr->floor;
while(callPtr->nextPtr!=NULL)
{
callPtr=callPtr->nextPtr;
min2=callPtr->floor;
}
if(min1<currentfloor||min2<currentfloor)
{
return 0;
}
else
{
return 1;
}
}
}
void clear(FLOORPTR sptr)
{
FLOORPTR previousPtr,tempPtr;
previousPtr=sptr;
while(previousPtr != NULL)
{
tempPtr=previousPtr;
previousPtr=previousPtr->nextPtr;
free(tempPtr);
}
free(previousPtr);
}
#include <stdlib.h>
#include <winsock.h>
#include <winbase.h>
#include <windows.h>
#include <time.h>
#include "struct.h"
#include "configure.h"
#include "control.h"
#include "getinput.h"
#include "getoutput.h"
#include "lift_status.h"
#include "writemessage.h"
void configure(void);
void getinput(void); //线程1要运行的函数
void getoutput(void); //线程2要运行的函数
void creatthread()
{
DWORD ThreadID = 1;
//创建线程input与output
HANDLE hRead = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getinput,NULL,0,&ThreadID);
DWORD ThreadID2 = 2;
HANDLE hRead2 = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)getoutput,NULL,0,&ThreadID2);
}
FLOORPTR *supPtr=NULL;
FLOORPTR *sdownPtr=NULL;
FLOORPTR *scallPtr=NULL;
int waittime,go,currentfloor,aimfloor,runtime,isrepuire,direction;
char status;
main()
{
configure();//初始化变量
creatthread();//创建线程
while (1){}
system("PAUSE");
return 0;
}
[color=0000FF]struct.h[/color]
struct Floor{
int floor;
struct floor* nextPtr;
};
typedef struct Floor FLOOR;
typedef FLOOR * FLOORPTR;
[color=0000FF]configure.h[/color]
#include <stdio.h>
#include <stdlib.h>
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
void configure(void)
{
waittime=0;//停靠时间:
go=0;//是否收到go请求:
currentfloor=1;//当前层:
aimfloor=0;//目标层;
runtime=0;//运行时间;
status='v'; //运行状态::v空闲;r运行;s停靠
isrequire=0;//存在请求:
direction=0; //运行方向:1向上2向下0为无动作
}
[color=0000FF]getinput.h[/color]
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <winbase.h>
#include <windows.h>
#include <time.h>
#include "struct.h"
extern FLOORPTR *supPtr;
extern FLOORPTR *sdownPtr;
extern FLOORPTR *scallPtr;
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
void insert(FLOORPTR,int);
void getinput()
{
char x;
int floor; //x存储代表请求类型的字符,floor存储代表楼层的数字
while(1)
{
printf("请输入命令:\nu-向上请求\nd-向下请求\nc-内部呼叫请求\ngo-运行\n1-9 楼层\n");
scanf("%c%d",&floor);
switch(x){
case'u':
insert(*supPtr,floor);
break;
case'd':
insert(*sdownPtr,floor);
break;
case'c':
insert(*scallPtr,floor);
break;
case'g':
go=1;
break;
}
}
}
void insert(FLOORPTR *sPtr,int newfloor)
{
FLOORPTR newPtr,previousPtr,currentPtr;
newPtr=malloc (sizeof (FLOORPTR));
if(newPtr!=NULL)
{
newPtr->floor = newfloor;
newPtr->nextPtr = NULL;
previousPtr=NULL;
currentPtr=*sPtr; //初始化
while((currentPtr!=NULL)&&(newfloor>currentPtr->floor))//对链表进行排序
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
if(previousPtr==NULL)//新楼层排在第一位,重置链表表头
{
newPtr->nextPtr=*sPtr;
*sPtr=newPtr;
}
else
{
if(newfloor==currentPtr->floor)//此节点已经存在,即此楼层已存在请求
{free(newPtr);}//忽略新请求,释放链表
else//将新请求插入链表
{
previousPtr->nextPtr=newPtr;
newPtr->nextPtr=currentPtr;
}
}
}
else
{
printf("%d floor not inserted.No memory available.\n",newfloor);
}
}
[color=0000FF]control.h[/color]
#include <stdio.h>
#include <stdlib.h>
#include "struct.h"
extern FLOORPTR *supPtr;
extern FLOORPTR *sdownPtr;
extern FLOORPTR *scallPtr;
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
int exist_require(FLOORPTR,FLOORPTR,FLOORPTR);
int Partua(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
int Partub(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
void Partuc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
int Partda(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
int Partdb(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
void Partdc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr);
void Control(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
isrequire=exist_require(*supPtr,*sdownPtr,*scallPtr);
switch(status)
{
case'r'://运行
if(direction==1)//运行状态向上
{
while(upPtr->nextPtr!=NULL)
{
if(currentfloor<((upPtr->floor)+1))
{
aimfloor=upPtr->floor;
break;
}
else
{
upPtr=upPtr->nextPtr;
}
}
}
else//运行状态向下
{
while(upPtr->nextPtr!=NULL)
{
if((upPtr->floor)<(currentfloor-1))
{
aimfloor=upPtr->floor;
break;
}
else
{
upPtr=upPtr->nextPtr;
}
}
}
break;
case's'://停靠
if(direction==1)//运行方向向上
{
if(Partua(supPtr,sdownPtr,scallPtr)==0)//第一种情况不存在,即没有比当前层搞的楼层有向上呼叫或者目标
{
if(Partub(*supPtr,*sdownPtr,*scallPtr)==0)//第二种情况不存在,即没有向下呼叫或者目标
{
Partuc(*supPtr,*sdownPtr,*scallPtr);//以最低的向上呼叫所在楼层为电梯当前的目标楼层
}
}
}
else//运行方向向下
{
if(Partda(*supPtr,*sdownPtr,*scallPtr)==0)//第一种情况不存在,即没有比当前层低的楼层有向下呼叫或者目标
{
if(Partdb(*supPtr,*sdownPtr,*scallPtr)==0)//第二种情况不存在,即没有向上呼叫或者目标
{
Partdc(*supPtr,*sdownPtr,*scallPtr);//以最低的向上呼叫所在楼层为电梯当前的目标楼层
}
}
}
break;
case'v'://空闲
if(isrequire==1)//存在请求
{status='r';} //进入运行状态
break;
}
}
int exist_require(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
if(upPtr!=NULL || downPtr!=NULL || callPtr!=NULL)
{return 1;}
else
{return 0;}
}
int Partua(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
int temp1=0,temp2=0;//暂存楼层
if(upPtr->floor > currentfloor)//最低向上请求比当前层高
{temp1 = upPtr->floor;}
else //最低向上请求比当前层低
{
while(upPtr->nextPtr != NULL) //寻找比当前层高的最低向上请求
{
upPtr=upPtr->nextPtr;
if(upPtr->floor > currentfloor)
{
temp1 = upPtr->floor;
break;
}
}
}
if(callPtr->floor > currentfloor)//确定最低的比当前层高的目标楼层
{temp2 = callPtr->floor;}
else
{
while(callPtr->nextPtr != NULL)
{
callPtr = callPtr->nextPtr;
if(callPtr->floor > currentfloor)
{
temp2=callPtr->floor;
break;
}
}
}
if(temp1!=0 || temp2!=0)
{
if(temp1>temp2) //目标设为向上呼叫和目标中的最小值
{aimfloor=temp2;}
else
{aimfloor=temp1;}
return 1;
}
else //没有比当前层搞的楼层有向上呼叫或者目标
{return 0;}
}
int Partub(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
if(downPtr->nextPtr==NULL && callPtr->nextPtr==NULL)//没有向下呼叫或者目标
{return 0;}
else
{
while(downPtr->nextPtr!=NULL)//寻找最高向下呼叫或者目标
{downPtr = downPtr->nextPtr;}
while(callPtr->nextPtr != NULL)
{callPtr = callPtr->nextPtr;}
if(downPtr->floor > callPtr->floor) //设置最高楼层为目标
{aimfloor = downPtr->floor;}
else
{aimfloor = callPtr->floor;}
return 1;
}
}
void Partuc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
aimfloor=upPtr->floor;
}
int Partda(FLOOR* upPtr,FLOOR* downPtr,FLOOR* callPtr)
{
int temp1=0,temp2=0;
if(downPtr->floor<currentfloor || callPtr->floor <currentfloor)
{
while(downPtr->floor < currentfloor)//找最高低于当前层的向下呼叫或者目标
{
temp1=downPtr->floor;
downPtr=downPtr->nextPtr;
}
while(callPtr->floor <currentfloor)
{
temp2=callPtr->floor;
callPtr=callPtr->nextPtr;
}
if(temp1>=temp2)
{aimfloor=temp1;}
else
{aimfloor=temp2;}
return 1;
}
else//没有比当前层低的楼层有向下呼叫或者目标
{return 0;}
}
int Partdb(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
int temp1=0,temp2=0;
if(upPtr->nextPtr!=NULL || callPtr->nextPtr!=NULL)
{
if(upPtr->nextPtr != NULL)
{temp1=upPtr->floor;}
if(callPtr->nextPtr != NULL)
{temp2=callPtr->floor;}
if(temp1>=temp2)
{aimfloor=temp2;}
else
{aimfloor=temp1;}
return 1;
}
else//没有向上呼叫或者目标
{return 0;}
}
void Partdc(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
while(downPtr->nextPtr != NULL)
{downPtr=downPtr->nextPtr;}
aimfloor=downPtr->floor;
}
lift_status.h
#include <stdio.h>
#include <stdlib.h>
extern FLOORPTR *supPtr;
extern FLOORPTR *sdownPtr;
extern FLOORPTR *scallPtr;
extern int waittime,go,currentfloor,aimfloor,runtime,isrequire,direction;
extern char status;
int extream (FLOORPTR,FLOORPTR,FLOORPTR);
void clear(FLOORPTR sptr);
void Lift_status(FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
switch(status)
{
case's':
if(isrequire == 1) //存在请求
{
if(waittime == 0) //停靠时间为0,即刚停靠
{
if(extream(*supPtr,*sdownPtr,*scallPtr) == 1) //电梯停在所有请求中的最高层或最底层
{
clear(*scallPtr);
upPtr=upPtr->nextPtr;
downPtr=downPtr->nextPtr;//呼叫请求清空,当前层清空
}
else
{
callPtr=callPtr->nextPtr;
upPtr=upPtr->nextPtr;
downPtr=downPtr->nextPtr;//当前层清空
}
waittime++;//停靠时间推进一秒
}
else
{
if(waittime == 5) //停靠时间到达
{
status='r';
waittime==0; //改变电梯状态,停靠时间重置
}
else
{
waittime++; //电梯无动作,停靠时间推进
}
}
if(go == 1)
{
waittime=0;
status='r';
go=0;
}
}
else //is_require != 1
{
status='v';
direction=0;
aimfloor=0;
}
break;
case'r':
if(currentfloor == aimfloor) // 到达目标楼层
{
status='s';
}
else //电梯还在运行
{
if(runtime==5) //运行了一层
{
switch(direction){
case'1':
currentfloor++; //当前层推进一层
break;
case'2':
currentfloor--; //当前层减少一层
break;
}
runtime=0; //任务完成,运行时间重置
}
else //继续推进时间
{
runtime++;
}
}
break;
case'v':
if(isrequire == 1) //存在请求
{
status='r';
}
break;
}
}
int extream (FLOORPTR upPtr,FLOORPTR downPtr,FLOORPTR callPtr)
{
int max1,max2,min1,min2;
if(direction==1)
{
max1=upPtr->floor;
while(upPtr->nextPtr!=NULL)
{
upPtr=upPtr->nextPtr;
max1=upPtr->floor;
}
max2=callPtr->floor;
while(callPtr->nextPtr!=NULL)
{
callPtr=callPtr->nextPtr;
max2=callPtr->floor;
}
if(max1>currentfloor||max2>currentfloor)
{
return 0;
}
else
{
return 1;
}
}
else
{
min1=downPtr->floor;
while(downPtr->nextPtr!=NULL)
{
downPtr=downPtr->nextPtr;
min1=callPtr->floor;
}
min2=callPtr->floor;
while(callPtr->nextPtr!=NULL)
{
callPtr=callPtr->nextPtr;
min2=callPtr->floor;
}
if(min1<currentfloor||min2<currentfloor)
{
return 0;
}
else
{
return 1;
}
}
}
void clear(FLOORPTR sptr)
{
FLOORPTR previousPtr,tempPtr;
previousPtr=sptr;
while(previousPtr != NULL)
{
tempPtr=previousPtr;
previousPtr=previousPtr->nextPtr;
free(tempPtr);
}
free(previousPtr);
}