• 大小: 21KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C/C++
  • 标签:

资源简介

比较简易的版本,利用有限自动机原理做的 需要的朋友可以拿来参看参看!

资源截图

代码片段和文件信息


#include                 // 程序所包含头文件 
#include
#include

int status (int *timeCulculate);         // 电梯运行状态转换函数  
void Control (int *time_countint *position);        // 电梯运行控制函数  
void GetInput(FILE *);           // 电梯指令输入函数  
void Writemessage ();             // 电梯指令输出函数

//外部公共参数 
int state = 0;
int time = 0;
int array_call[50] = {0};
int CurrentFloor = 1;       
int CurrentOrder = 0; 
int CurrentDirection = 1;     
int time_count = 0;   // 片断计数器 
int order_time=0;   //   GetInput函数的指令计时器 
main()
{  
   /*Initialize   初始化过程*/ 
   int position = 0;   // 外置control参数 
   FILE *fptr;
   char list[5];
   
   printf(“\t\t  电梯控制程序\n“);
   if( (fptr=fopen(“Order.txt““r“))==NULL )
      printf(“Warning:Can‘t open the order file!\n“); 
   else{
      printf(“文件已经读取\n“);
      fgets(list5fptr); 
      fgetc(fptr);
       /*Initialize   初始化过程*/  
       while (1){
          
          Writemessage ();  
          printf(“\n“);
          GetInput(fptr);
          Control (&time_count&position);    //控制函数Control调用有限状态自动机函数 Status 
          time=time+1;           // 时间的推进模拟 
       
          printf(“\n程序运行时间  第%d秒\n“time);
          Sleep(1000);     
       } 
       fclose(fptr); 
   }  
     
   return 0;
}

int status(int *timeCulculate)       // 电梯运行状态转换函数  
{
   int TimeLimit=20; //    TimeLimit暂设定  5 +4 +5 +6 = 20为 开关门事件 及 等待命令总时间 
   //int sign;/*表示是否到达目标楼层*并不需要/  
   
   switch(state) {                  // 电梯运行有限状态自动机 
       case 0:    //   开始 START 状态 
            state = 1;                 // 开始 转到 空闲 状态 
            break;
       case 1:    //   空闲 FREE 状态
            if ( CurrentOrder==0 )    //无指令时   
            {
               state = 1;                       // 状态不变 
               CurrentDirection = 0;           // 方向初始化为零 
            }
            else if (CurrentOrder==CurrentFloor)  // 所在即为目标楼层 
               state = 3;             //  空闲 转到 停靠 状态 
                 else
               state = 2;             //  空闲 转到 运行 状态 
            break;
       case 2:    //   运行 RUN 状态
            if(CurrentOrder==CurrentFloor)   // 到达目标楼层即停靠 
               state = 3;
            else                             //  未到达则继续运行 
               state = 2;
            break;
       case 3:    //   停靠 STOP 状态
            if(*timeCulculate            {
               state = 3;                        // 状态不变 
               if(*timeCulculate                  printf(“开门%d秒\n“*timeCulculate);
               else if(*timeCulculate                  printf(“等待%d秒\n“*timeCulculate-5);  
               else
                  printf(“关门%d秒\n“*timeCulculate-9);
            
            }
            else                                 // 停靠时间 结束  
            {
               if(CurrentOrder==0)                    //无指令时   
               {
                  if(*ti

评论

共有 条评论

相关资源