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

📄 list.c

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

CALLPTR ControlCallPTR;/*控制呼叫请求的头指针*/ 
PAUSEPTR ControlPausePTR;/*控制输出状态的头指针*/
extern FILE *InputFileptr;
extern FILE *OutputFileptr;


/*从键盘直接输入的线程*/
DWORD WINAPI GetInput1(LPVOID lpParameter)
{
    char calling;
	while(1){
          printf("请输入呼叫请求:"); 
		  scanf("%c",&calling);
		  translatecalling(calling);/*翻译命令*/
		  fflush(stdin);
	}
}
      
  
      
/*翻译命令*/
void translatecalling(char command)
{
    int direction,floor;//direction=1外部有向上呼叫,direction=2外部有向下呼叫 ,direction=0内部呼叫.floor代表呼叫楼层*/  
    switch(command)
    {
        /*内部呼叫*/
        case '1':
               direction=0;
               floor=1;
               break;
        case '2':
               direction=0;
               floor=2;
               break;
        case '3':
               direction=0;
               floor=3;
               break;
        case '4':
               direction=0;
               floor=4;
               break;
        case '5':
               direction=0;
               floor=5;
               break;
        case '6':
               direction=0;
               floor=6;
               break;
        case '7':
               direction=0;
               floor=7;
               break;
        case '8':
               direction=0;
               floor=8;
               break;
        case '9':
               direction=0;
               floor=9;
               break;
        /*外部呼叫  上行*/       
                
        case 'q':
               direction=1;                       
               floor=8;
               break;
        case 'w':
               direction=1;                     
               floor=7;
               break;
        case 'e':
               direction=1;                      
               floor=6;
               break; 
        case 'r':
               direction=1;                      
               floor=5;
               break;
        case 't':
               direction=1;                     
               floor=4;
               break;
        case 'y':
               direction=1;                     
               floor=3;
               break;
        case 'u':
               direction=1;                     
               floor=2;
               break;
        case 'i':
               direction=1;                      
               floor=1;
               break;
        /*外部呼叫   下行 */
        case 'a':
               direction=2;
               floor=9;
               break;
        case 's':
               direction=2;
               floor=8;
               break;
        case 'd':
               direction=2;
               floor=7;
               break;
        case 'f':
               direction=2;
               floor=6;
               break;
        case 'g':
               direction=2;
               floor=5;
               break;
        case 'h':
               direction=2;
               floor=4;
               break;
        case 'j':
               direction=2;
               floor=3;
               break;
        case 'k':
             direction=2;                    
             floor=2;
             break;
    }
    Storenode(direction,floor,&ControlCallPTR);/*把结点存储到链表中*/ 
   
}


/*将翻译的命令存储到链表中*/
void Storenode(int direction, int floor, CALLPTR *startptr)
{
    CALLPTR newptr, curruntptr;
    if(floor<=9&&floor>=1){     
       newptr=(CALLPTR)malloc(sizeof(CALL));
       newptr->CallDirection=direction;
       newptr->CallFloor=floor;
       newptr->Nextptr=NULL;
       ControlCallFlash(newptr, TRUE);
       if(*startptr==NULL)
          *startptr=newptr;
       else{
          curruntptr=*startptr;
          while(curruntptr->Nextptr!=NULL)
             curruntptr=curruntptr->Nextptr;
          curruntptr->Nextptr=newptr;
       }
    }
	else
       printf("输入错误,请重新输入。\n");
}


/*删除链表结点*/
void Deletenode(CALLPTR *startPtr)
{
     CALLPTR previousptr, currentptr, tempptr;
     n3=0;

     if(*startPtr!=NULL){
        if(CurrentDirection==1){//如果电梯正在上行
           while(State==1&&(*startPtr!=NULL)&&(StatusFloor==(*startPtr)->CallFloor)&&
              ((*startPtr)->CallDirection==1||(*startPtr)->CallDirection==0)){//若为头结点 

              if((*startPtr)->CallDirection==1)
                 n3=1;
              tempptr=(*startPtr);
		      ControlCallFlash(tempptr, FALSE);
              *startPtr=(*startPtr)->Nextptr;
              free(tempptr);
		   }
           if(*startPtr!=NULL){
              currentptr=(*startPtr)->Nextptr;
              previousptr=*startPtr;
              while(currentptr!=NULL){
                 if((currentptr->CallFloor==StatusFloor)&&State==1&&
                   (currentptr->CallDirection==1||currentptr->CallDirection==0)){
                   // if(currentptr->CallDirection==1)                                                       
                       n3=1;
                    tempptr=currentptr;
				    ControlCallFlash(tempptr, FALSE);
                    currentptr=currentptr->Nextptr;
                    previousptr->Nextptr=currentptr;
                    free(tempptr);
				 }
                 else{
                    previousptr=currentptr;
                    currentptr=currentptr->Nextptr;
				 }
			  }
			}
            if(n3==0){
                if(*startPtr!=NULL){  
                   while(State==1&&(*startPtr!=NULL)&&(StatusFloor==(*startPtr)->CallFloor)){//若为头结点 
                       tempptr=(*startPtr);
		               ControlCallFlash(tempptr, FALSE);
                       *startPtr=(*startPtr)->Nextptr;
                       free(tempptr);
				   }
				}
                if(*startPtr!=NULL){
                   currentptr=(*startPtr)->Nextptr;
                   previousptr=*startPtr;
                   while(currentptr!=NULL){
                      if((currentptr->CallFloor==StatusFloor)&&State==1){
                          tempptr=currentptr;
				          ControlCallFlash(tempptr, FALSE);
                          currentptr=currentptr->Nextptr;
                          previousptr->Nextptr=currentptr;
                          free(tempptr);
					  }
                      else{
                          previousptr=currentptr;
                          currentptr=currentptr->Nextptr;
					  }
				   }
				}
			}
        }
        else{
           while(State==1&&(*startPtr!=NULL)&&(StatusFloor==(*startPtr)->CallFloor)&&
              ((*startPtr)->CallDirection==2||(*startPtr)->CallDirection==0)){//若为头结点 
              if((*startPtr)->CallDirection==2)
                 n3=1;
              tempptr=(*startPtr);
		      ControlCallFlash(tempptr, FALSE);
              *startPtr=(*startPtr)->Nextptr;
              free(tempptr);
		   }
           if(*startPtr!=NULL){
              currentptr=(*startPtr)->Nextptr;
              previousptr=*startPtr;
              while(currentptr!=NULL){
                 if((currentptr->CallFloor==StatusFloor)&&State==1&&
                 (currentptr->CallDirection==2||currentptr->CallDirection==0)){
                    //if(currentptr->CallDirection==2)                                                       
                       n3=1;
                    tempptr=currentptr;
				    ControlCallFlash(tempptr, FALSE);
                    currentptr=currentptr->Nextptr;
                    previousptr->Nextptr=currentptr;
                    free(tempptr);
                 }
                 else{
                    previousptr=currentptr;
                    currentptr=currentptr->Nextptr;
                 }
              }
              if(n3==0){
                 if(*startPtr!=NULL){     
                    while(State==1&&(*startPtr!=NULL)&&(StatusFloor==(*startPtr)->CallFloor)){//若为头结点 
                       tempptr=(*startPtr);
		               ControlCallFlash(tempptr, FALSE);
                       *startPtr=(*startPtr)->Nextptr;
                       free(tempptr);
                    }
                 }
                 if(*startPtr!=NULL){
                     currentptr=(*startPtr)->Nextptr;
                     previousptr=*startPtr;
                     while(currentptr!=NULL){
                        if((currentptr->CallFloor==StatusFloor)&&State==1){
                           tempptr=currentptr;
				           ControlCallFlash(tempptr, FALSE);
                           currentptr=currentptr->Nextptr;
                           previousptr->Nextptr=currentptr;
                           free(tempptr);
                        }
                        else{
                           previousptr=currentptr;
                           currentptr=currentptr->Nextptr;
                        }
                     }
				 }
              }
		   }
        }
	}
}


/*释放链表*/
void FreeNote(void)
{
	CALLPTR tempPtr;

	tempPtr=ControlCallPTR;
	while(tempPtr!=NULL){
		tempPtr=ControlCallPTR->Nextptr;
		free(ControlCallPTR);
	}
}

⌨️ 快捷键说明

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