⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 control.c

📁 模拟电梯程序
💻 C
字号:
#include<stdlib.h>
#include<stdio.h>
#include<windows.h>
#include"egg.h"
#include"data.h"
#include"control.h"
#include"lift.h"
#include"OutputFlash.h"

State=0;/*设置电梯初始状态,空闲*/
Time=0;/*设置初始时间*/
CurrentDirection=0;
StatusFloor=1;
GoalFloor=0;/*1-9表示目标楼层为1-9层,0表示当前无目标楼层。*/
int RunTime=1;/*电梯运行一层的时间*/
int CountTime;/*计时器*/ 
  
void control(void)
{
     StartFlash();
     while(1){
        Time++;  /*时间片加1*/ 
		GetInput2(Time);
        ChangeCurrentFloor(&ControlPausePTR);
        Deletenode(&ControlCallPTR);
        ChangeGoalFloor(&ControlCallPTR);
        ControlFlash();
        Output(&ControlPausePTR);
	 }
}

/*函数1:控制电梯目标楼层*/
void ChangeGoalFloor(CALLPTR *startPtr)
{
    CALLPTR curruntPtr, tempPtr;
    if(State==0){/*当电梯空闲时*/ 
       if(GoalFloor==0){
          if((*startPtr)!=NULL)/*当有呼叫请求时,将目标楼层定为呼叫请求*/
             GoalFloor=(*startPtr)->CallFloor;
       }
    }
    else{
       if(State==1){/*当电梯停靠时*/ 
          if((*startPtr)!=NULL){/*当有呼叫请求时*/ 
             curruntPtr=*startPtr;
             if(CurrentDirection==1){/*当电梯向上驶来*/
                while(curruntPtr!=NULL){/*以最低的高于当前楼层的有向上呼叫或者目标的楼层为目标*/ 
                   if((curruntPtr->CallDirection==0||curruntPtr->CallDirection==1)
                      &&curruntPtr->CallFloor>StatusFloor)
                      GoalFloor=curruntPtr->CallFloor;
                   curruntPtr=curruntPtr->Nextptr;
                }
                if(GoalFloor!=0){
                   curruntPtr=*startPtr;
                   while(curruntPtr!=NULL){
                      if((curruntPtr->CallDirection==1||curruntPtr->CallDirection==0)&&
                         (curruntPtr->CallFloor>StatusFloor)&&(curruntPtr->CallFloor<GoalFloor))
                         GoalFloor=curruntPtr->CallFloor;
                      curruntPtr=curruntPtr->Nextptr;
                   }
                }
                else{
                    GoalFloor=(*startPtr)->CallFloor;
                    curruntPtr=*startPtr;
                    while(curruntPtr!=NULL){
                       if((curruntPtr->CallDirection==2||curruntPtr->CallDirection==0)&&
                          (curruntPtr->CallFloor>GoalFloor))
                          GoalFloor=curruntPtr->CallFloor;
                       curruntPtr=curruntPtr->Nextptr;
                    }
				}
             }
             else{/*当电梯向下驶来*/
                curruntPtr=*startPtr;
                while(curruntPtr!=NULL){/*以最高的低于当前楼层的有向下呼叫或者目标的楼层为目标*/ 
                   if((curruntPtr->CallDirection==0||curruntPtr->CallDirection==2)
                      &&curruntPtr->CallFloor<StatusFloor)
                      GoalFloor=curruntPtr->CallFloor;
                   curruntPtr=curruntPtr->Nextptr;
                }
                curruntPtr=*startPtr;
                if(GoalFloor!=0){
                   while(curruntPtr!=NULL){
                      if((curruntPtr->CallDirection==2||curruntPtr->CallDirection==0)&&
                         (curruntPtr->CallFloor<StatusFloor)&&(curruntPtr->CallFloor>GoalFloor))
                         GoalFloor=curruntPtr->CallFloor;
                      curruntPtr=curruntPtr->Nextptr;
                   }
                }
                else{
                   GoalFloor=(*startPtr)->CallFloor;
                   curruntPtr=*startPtr;
                   while(curruntPtr!=NULL){
                      if((curruntPtr->CallDirection==1||curruntPtr->CallDirection==0)&&
                         (curruntPtr->CallFloor<GoalFloor))
                         GoalFloor=curruntPtr->CallFloor;
                      curruntPtr=curruntPtr->Nextptr;
                   }
				}
             }
          }
       }
       else{/*当电梯运行时*/ 
          if(CurrentDirection==1){
             if((*startPtr)!=NULL){
                curruntPtr=*startPtr;
                while(curruntPtr!=NULL){
                   if((curruntPtr->CallDirection==1||curruntPtr->CallDirection==0)&&
					   (curruntPtr->CallFloor>StatusFloor+1)&&(curruntPtr->CallFloor<GoalFloor))
                      GoalFloor=curruntPtr->CallFloor;
                   curruntPtr=curruntPtr->Nextptr;
                }
             }
          }
          else{
             if((*startPtr)!=NULL){
                curruntPtr=*startPtr;
                while(curruntPtr!=NULL){
                   if((curruntPtr->CallDirection==2||curruntPtr->CallDirection==0)&&
                      (curruntPtr->CallFloor<StatusFloor-1)&&(curruntPtr->CallFloor>GoalFloor))
                      GoalFloor=curruntPtr->CallFloor;
                   curruntPtr=curruntPtr->Nextptr;
                }
             }
          }
       }
	}
}         
                   
void ChangeCurrentFloor(PAUSEPTR *startPtr)
{
     
    if(State==0){
       if(GoalFloor!=0){
          if(StatusFloor==GoalFloor){
             State=1;
             CountTime=1;
             *startPtr=malloc(sizeof(PAUSE));
             (*startPtr)->PauseTime=Time;
             (*startPtr)->PauseFloor=StatusFloor;
             GoalFloor=0;
          }
          else{
             if(StatusFloor<GoalFloor){
                State=2;
                CountTime=1;
                CurrentDirection=1;
             }
             else{
                State=2;
                CountTime=1;
                CurrentDirection=2;
             }
          }
       }
    }
    else{
       if(CountTime==RunTime){               
          if(State==1){
             if(GoalFloor!=0){
				 if(GoalFloor==StatusFloor){
					 State=1;
				     GoalFloor=0;
				 }
				 else{
                   State=2;
                   if(StatusFloor<GoalFloor)
                      CurrentDirection=1;
                   else
                      CurrentDirection=2;
				 }
             }
             else
                State=0;
          }
          else{
                if(CurrentDirection==1)
                   StatusFloor++;
                else
                   StatusFloor--;
		        if(StatusFloor==GoalFloor){
                State=1;
                *startPtr=malloc(sizeof(PAUSE));
                (*startPtr)->PauseTime=Time;
                (*startPtr)->PauseFloor=StatusFloor;
                GoalFloor=0;
				}
          }
          
          CountTime=1;
       }
       else{
          CountTime++;
       }
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -