📄 control.c
字号:
//控制模块
#include<stdio.h>
#include<stdlib.h>
#define q0 0
#define q1 1
#define q2 2
#define q3 3
extern int STATE;
extern int LOCATION;
extern int HEADING;
extern int TIME;
extern int CALL;
extern int floorcall[9];
extern int upcall[8];
extern int downcall[8];
extern int destination[100];
extern int CALLSOURSE;
extern int P;
extern int des[100][2];
void control(void)
{
int a=0,b,c,d,e=0,h=0,i=0,g=0; //初始化变量,a用来标志策略,a=1代表先来先服务策略,a=2代表顺便服务策略
int up[9]={0},down[9]={0};//初始化目标楼层数组 ,上升数组,下降数组
if(P==1)//先来先服务策略
{
while(1)//运行死循环
{
if(CALL==1)//如果有输入呼叫信息
{
switch (CALLSOURSE)//寻找输入的来源
{
case 1:for(b=0;b<=8;b++)//如果是电梯内部输入,遍历数组floorcall
if(floorcall[b]==1)//如果b元素为1,说明有呼叫b+1楼
{
c=b+1;//用c记录呼叫楼层
floorcall[b]=0;//恢复b元素为0
for(d=0;destination[d]!=0;d++)//寻找呼叫需要存储在目标楼层数组的位置
;
destination[d]=c;//将目标楼层信息存入相应位置
}
CALLSOURSE=0;
break;
case 2:for(b=0;b<=7;b++)//如果是电梯外部输入的向上的呼叫,遍历数组upcall
if(upcall[b]==1)//如果b元素为1,说明有b+1楼向上呼叫
{
c=b+1;//用c记录呼叫楼层
upcall[b]=0;//恢复b元素为0
for(d=0;destination[d]!=0;d++)//寻找呼叫需要存储在目标楼层数组的位置
;
destination[d]=c;//将目标楼层信息存入相应位置
}
CALLSOURSE=0;
break;
case 3:for(b=0;b<=7;b++)//如果是电梯外部输入的向下的呼叫,遍历数组downcall
if(downcall[b]==1)//如果b元素为1,说明有b+2楼向下呼叫
{
c=b+2;//用c记录呼叫楼层
downcall[b]=0;//恢复b元素为0
for(d=0;destination[d]!=0;d++)//寻找呼叫需要存储在目标楼层数组的位置
;
destination[d]=c; //将目标楼层信息存入相应位置
}
CALLSOURSE=0;
break;
default:printf("There is something wrong in input\n");//如果不是来自这三种输入打印输入错误
break;
} CALL=0;//恢复无呼叫输入
}
statechange();//调用函数statechange改变电梯的状态
}
}
else if(P==2)//顺便服务策略
{
while(1)//运行死循环
{
if(CALL==1)
{ g=0;
switch (CALLSOURSE)
{
case 1:if(HEADING==1)
{
for(b=0;b<=8;b++)
if(floorcall[b]==1&&LOCATION<b+1)
{
up[b]=1;
floorcall[b]=0;
}
else if(floorcall[b]==1&&LOCATION>=(b+1))
{
down[b]=1;
floorcall[b]=0;
}
}
else if(HEADING==2)
{
for(b=0;b<8;b++)
if(floorcall[b]==1&&LOCATION>b+1)
{
down[b]=1;
floorcall[b]=0;
}
else if(floorcall[b]==1&&LOCATION<=(b+1))
{
up[b]=1;
floorcall[b]=0;
}
}
else
for(b=0;b<=8;b++)
if(floorcall[b]==1)
{
if(b+1>=LOCATION)
up[b]=1;
else if(b+1<LOCATION)
down[b]=1;
}
break;
case 2:for(b=0;b<=7;b++)
if(upcall[b]==1)
{
up[b]=1;
upcall[b]=0;
}
break;
case 3:for(b=0;b<=7;b++)
if(downcall[b]==1)
{
downcall[b]=0;
down[b+1]=1;
}
break;
default:printf("There is something wrong in input\n");//如果不是来自这三种输入打印输入错误
break;
}
if(HEADING==1)
{
for(a=LOCATION;a<=8;a++)
if(up[a]==1)
{
for(b=0;des[b][0]<a+1&&des[b][1]==1;b++)
;
if(des[b][0]==a+1&&des[b][1]==1)
up[a]=0;
else
{
for(c=b;des[c][0]!=0;c++)
;
for(;c>b;c--)
des[c][0]=des[c-1][0];
des[c][0]=a+1;
des[c][1]=1;
up[a]=0;
}
}
for(a=8;a>=0;a--)
if(down[a]==1)
{
for(b=0;des[b][1]==1;b++)
;
for(;des[b][0]>a+1&&des[b][1]==2;b++)
;
if(des[b][0]==a+1&&des[b][1]==2)
down[a]=0;
else
{
for(c=b;des[c][0]!=0;c++)
;
for(;c>b;c--)
des[c][0]=des[c-1][0];
des[c][0]=a+1;
des[c][1]=2;
down[a]=0;
}
}
for(a=0;a<=LOCATION-1;a++)
if(up[a]==1)
{
for(b=0;des[b][1]==1;b++)
;
for(;des[b][2]==2;b++)
;
for(;des[b][1]<a+1;b++)
;
if(des[b][0]==a+1&&des[b][1]==1)
up[a]=0;
else
{
for(c=b;des[c][0]!=0;c++)
;
for(;c>b;c--)
des[c][0]=des[c-1][0];
des[c][0]=a+1;
des[c][1]=1;
up[a]=0;
}
}}
else if(HEADING==2)
{
for(a=LOCATION-2;a>=0;a--)
if(down[a]==1)
{
for(b=0;des[b][0]>a+1;b++)
;
if(des[b][0]==a+1&&des[b][1]==2)
down[a]=0;
else
{
for(c=b;des[c][0]!=0;c++)
;
for(;c>b;c--)
des[c][0]=des[c-1][0];
des[c][0]=a+1;
des[c][1]=2;
down[a]=0;
}
}
for(a=0;a<=8;a++)
if(up[a]==1)
{
for(b=0;des[b][1]==2;b++)
;
for(;des[b][0]<a+1&&des[b][1]==1;b++)
;
if(des[b][0]==a+1&&des[b][1]==1)
up[a]=0;
else
{
for(c=b;des[c][0]!=0;c++)
;
for(;c>b;c--)
des[c][0]=des[c-1][0];
des[c][0]=a+1;
des[c][1]=1;
up[a]=0;
}
}
for(a=8;a>LOCATION-2;a++)
if(down[a]==1)
{
for(b=0;des[b][1]==2;b++)
;
for(;des[b][1]==1;b++)
;
for(;des[b][0]>a+1&&des[b][1]==2;b++)
;
if(des[b][0]==a+1&&des[b][1]==1)
down[a]=0;
else
{
for(c=b;des[c][0]!=0;c++)
;
for(;c>b;c--)
des[c][0]=des[c-1][0];
des[c][0]=a+1;
des[c][1]=2;
down[a]=0;
}
}
}
else
{
for(a=0;a<=8;a++)
if(up[a]==1)
{
des[0][0]=a+1;
des[0][1]=1;
up[a]=0;
}
else if(down[a]==1)
{
des[0][0]=a+1;
des[0][1]=2;
down[a]=0;
}
}
}
for(a=0;a<=8;a++)
if(floorcall[a]==1)
g=1;
for(a=0;a<=7;a++)
if(upcall[a]==1||downcall[a]==1)
g=1;
if(g==0)
CALL=0;
statechange();//调用状态改变函数
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -