📄 elevator.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "DIANTI.h"
extern int IDLE;
extern int RUN;
extern int STOP;
extern int UP;
extern int DOWN;
extern int updown;
extern int destination;
extern int current_floor;
extern int current_state;
extern int TIME;
extern int GO;
extern int require[10];
extern char up[9];
extern char down[10];
extern struct order* headPtr;
void input(void)//接收键盘输入的数据,存入链表
{
char Order; //呼叫请求所在楼层
char o;
while(1)
{
printf("Your order:"); /*输入呼叫请求*/
scanf("%c",&Order);
if(Order=='G')
{
scanf("%c",&o);
if(o=='O')
{
GO=1;
Order=-9;
}
}
fflush(stdin);
if(Order<='9'&&Order>='1')
Inorder(Order);
else
{
if(trans(Order)<=9&&trans(Order)>=1)
{
Outorder(Order);
call(direction(Order),trans(Order));
}
else
if(Order!=-9)
printf("error input.\n");
}
Button(require,GO);
}
}
void input2(void)//接收文件输入的数据
{
char Order,o; //呼叫请求所在楼层
int Time;
FILE *cfPtr;
printf("呼叫请求将由文件order输入。\n");
if((cfPtr=fopen("order.txt","r"))==NULL)
printf("File could not be opened.\n");
else
{
printf("电梯运行过程将写入在client文件中\n数据输入与处理中...\n");
fscanf(cfPtr,"%c",&Order);
if(Order=='G')
{
o=fgetc(cfPtr);
if(o=='O')
{
GO=1;
Order=-9;
}
}
fscanf(cfPtr,"%d\n",&Time);
while(!feof(cfPtr))
{
if(Time<=TIME)
{
Time=0;
if(Order<='9'&&Order>='1')
Inorder(Order);
else
{
Outorder(Order);
call(direction(Order),trans(Order));
}
fscanf(cfPtr,"%c",&Order);
if(Order=='G')
{
o=fgetc(cfPtr);
if(o=='O')
{
GO=1;
Order=-9;
}
}
fscanf(cfPtr,"%d\n",&Time);
Button(require,GO);
}
}
while(Time>TIME)
;
if(Order<='9'&&Order>='1')
Inorder(Order);
else
{
Outorder(Order);
call(direction(Order),trans(Order));
}
printf("数据输入完毕。");
Button(require,GO);
}
fclose(cfPtr);
}
void Outorder(char Order) /*将请求存入结构,并组成链表*/
{
struct order* newPtr;
struct order* currentPtr;
struct order* previousPtr;
newPtr=(struct order*)malloc(sizeof(struct order));
if(newPtr!=NULL) //申请成功
{
newPtr->from=Order;
newPtr->nextPtr=NULL;
if(headPtr==NULL) //如果headPtr为空,确定headPtr的值
headPtr=newPtr;
else // 如果headPtr不为空,将节点连接在链表最后
{
previousPtr=headPtr;
currentPtr=headPtr->nextPtr;
while(currentPtr!=NULL)
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
previousPtr->nextPtr=newPtr;
}
}
}
void Inorder(char Order)
{
int a;
a=Order-48;
require[a]=1;
}
/*控制模块*/
/*函数1*/
void state_trans(void) //改变电梯的状态
{
int r;
while(1)
{
switch(current_state)
{
case 1: /*1代表空闲状态*/
output();
idle();
while(destination==0)
{
control();
time_count();
TIME=TIME+1;
}
if(headPtr!=NULL)
{
if(trans(headPtr->from)>current_floor)
{
updown=UP;
current_state=RUN;
}
if(trans(headPtr->from)<current_floor)
{
updown=DOWN;
current_state=RUN;
}
if(trans(headPtr->from)==current_floor)
{
current_state=STOP;
Delete();
}
}
else
{
if(destination>current_floor)
{
updown=UP;
current_state=RUN;
}
if(destination<current_floor)
{
updown=DOWN;
current_state=RUN;
}
}
break;
case 2: /*2代表运行状态*/
GO=0;
fadego();
run(updown);
TIME=TIME+5;
current_floor=current_floor+updown; /*电梯移动一层,current_floor沿当前的方向改变1*/
cf(current_floor);
if(check()==1)
current_state=STOP;
if(current_floor==destination) //到达destination,停靠并确定下一个destination
{
current_state=STOP;
Delete();
control();
}
if(current_state==RUN)
output();
break;
case 3: /*3代表停靠状态*/
output();
idle();
if(destination!=0)
{
if(destination>current_floor)
updown=UP;
if(destination<current_floor)
updown=DOWN;
current_state=RUN;
}
else
{
current_state=IDLE;
updown=0;
}
cf(current_floor);
fade(current_floor);
for(r=1;r<4&&GO==0;r++) /*停靠时间为3s*/
time_count();
TIME=TIME+r-1;
break;
default:
break;
}
}
}
/*函数2*/
int check(void) //电梯到达在每层时,检验该层是否有请求
{
struct order* currentPtr;
struct order* previousPtr;
struct order* tempPtr;
int existence=0;
char request;
currentPtr=headPtr;
previousPtr=NULL;
while(currentPtr!=NULL)
{
if(updown==UP) //如果电梯上行
request=up[current_floor];
if(updown==DOWN) //如果电梯下行
request=down[current_floor];
if(currentPtr->from==request) //检查在该楼层是否有同方向请求
{
existence=1; //如果有同方向请求,将该节点从链表中删除
if(currentPtr==headPtr) /*删除节点*/
{
headPtr=currentPtr->nextPtr;
tempPtr=currentPtr;
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
free(tempPtr); //释放内存
}
else
{
previousPtr->nextPtr=currentPtr->nextPtr;
tempPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
free(tempPtr);
}
}
else //如果无同方向请求,则继续遍历链表
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
}
if(require[current_floor]==1) //判断该楼层是否有停靠请求
{
require[current_floor]=0; //同时将停靠请求删除
existence=1;
}
return(existence);
}
/*函数3*/
void control(void) //在电梯完成一个destination后,确定其下一个destination
{
struct order* currentPtr=headPtr;
int i,j,m,n,y,go;
destination=0; //清除原目标
go=1;
/* 当电梯为上行时 */
if(updown==UP) //检查是否有同方向上的呼叫请求。若有,则将其确定为新的destination
{
while(currentPtr!=NULL&&go==1) //检查链表中是否有同方向上并且请求的方向也与运行方向相同的请求
{
if(trans(currentPtr->from)>current_floor&&direction(currentPtr->from)==UP)
go=0;
else
currentPtr=currentPtr->nextPtr;
}
if(go==0)
destination=trans(currentPtr->from);
else //若链表中没有,则检查数组中是否有同方向上的请求
{
for(i=current_floor;require[i]==0&&i<9;i++)
;
if(i<9||require[9]==1)
destination=i;
}
currentPtr=headPtr;
if(destination==0) //检查链表中是否有同方向上但请求的方向与运行方向不同的请求
{
while(currentPtr!=NULL&&go==1)
{
if(trans(currentPtr->from)>current_floor)
go=0;
else
currentPtr=currentPtr->nextPtr;
}
if(go==0)
destination=trans(currentPtr->from);
}
if(destination==0) /*若无同方向上的请求,则将链表第一个节点或数组中最远目标定义为目标*/
{
if(headPtr!=0) //先检查链表
destination=trans(headPtr->from);
else //若链表中无请求,则检查数组
{
for(j=1;require[j]==0&&j<current_floor;j++)
;
if(j<current_floor)
destination=j;
}
}
}
/* 当电梯为下行时 */
if(updown==DOWN)
{
while(currentPtr!=NULL&&go==1) //检查链表中是否有同方向上并且请求的方向也与运行方向相同的请求
{
if(trans(currentPtr->from)<current_floor&&direction(currentPtr->from)==DOWN)
go=0;
else
currentPtr=currentPtr->nextPtr;
}
if(go==0)
destination=trans(currentPtr->from);
else
{
for(m=current_floor;require[m]==0&&m>1;m--)
;
if(m>1||require[1]==1)
destination=m;
}
currentPtr=headPtr;
if(destination==0) //检查链表中是否有同方向上但请求的方向与运行方向不同的请求
{
while(currentPtr!=NULL&&go==1)
{
if(trans(currentPtr->from)<current_floor)
go=0;
else
currentPtr=currentPtr->nextPtr;
}
if(go==0)
destination=trans(currentPtr->from);
}
if(destination==0)
{
if(headPtr!=0)
{
destination=trans(headPtr->from);
updown=UP;
}
else
{
for(n=9;require[n]==0&&n>current_floor;n--)
;
if(n>current_floor)
{
destination=n;
updown=UP;
}
}
}
}
/* 当电梯为空闲时 */
if(updown==0)
{
for(y=1;y<=9&&require[y]==0;y++)
;
if(y<10)
destination=y;
if(destination==0)
{
if(headPtr!=NULL)
destination=trans(headPtr->from);
}
}
}
/*函数4*/
void Delete(void) //将响应完的请求从链表或数组中删除
{
struct order* currentPtr;
struct order* previousPtr;
struct order* tempPtr;
currentPtr=headPtr;
previousPtr=NULL;
while(currentPtr!=NULL)
{
if(trans(currentPtr->from)==current_floor) /*查找并删除所有与该楼层相同的呼叫请求*/
{
if(currentPtr==headPtr) /*删除节点*/
{
headPtr=currentPtr->nextPtr;
tempPtr=currentPtr;
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
free(tempPtr); //释放内存
}
else
{
previousPtr->nextPtr=currentPtr->nextPtr;
tempPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
free(tempPtr);
}
}
else //如请求不是该楼层,则继续遍历链表
{
previousPtr=currentPtr;
currentPtr=currentPtr->nextPtr;
}
}
require[current_floor]=0; //将该层停靠请求删除
}
/* 函数5 */
int trans(char requirement)//将输入的字母转化为楼层数
{
switch(requirement)
{
case 'I':
return 1;
case 'U': case 'K':
return 2;
case 'Y': case 'J':
return 3;
case 'T': case 'H':
return 4;
case 'R': case 'G':
return 5;
case 'E': case 'F':
return 6;
case 'W': case 'D':
return 7;
case 'Q': case 'S':
return 8;
case 'A':
return 9;
default:
return 0;
}
}
/* 函数6 */
int direction(char requirement) //判断请求的方向
{
switch(requirement)
{
case 'I': case 'U': case 'Y':
case 'T': case 'R': case 'E':
case 'W': case 'Q':
return 1;
case 'K': case 'J': case 'H':
case 'G': case 'F': case 'D':
case 'S': case 'A':
return -1;
default:
return 0;
}
}
/* 函数7 */
int time_count(void) //时间片过一秒
{
clock_t start,finish;
start=clock();
finish=clock();
while((int)((finish-start)/CLOCKS_PER_SEC)<1)
finish=clock();
return 0;
}
/*输出模块*/
void output(void) //文件输出
{
FILE *cfPtr;
if((cfPtr=fopen("client.txt","a+"))==NULL)
printf("File could not be opened.\n");
else
{
fprintf(cfPtr,"Current floor:%d \tTime:%d ",current_floor,TIME);
switch(current_state)
{
case 1:
fprintf(cfPtr,"\tCurrent state:IDLE\n");
break;
case 2:
fprintf(cfPtr,"\tCurrent state:PASS BY ");
if(updown==1)
fprintf(cfPtr,"\tDirection:Up\n");
else
fprintf(cfPtr,"\tDirection:Down\n");
break;
case 3:
fprintf(cfPtr,"\tCurrent state:STOP\n");
break;
}
fclose (cfPtr);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -