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

📄 parking.c

📁 This program can do Parking management
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#define BUFF 50#define STACK_SIZE 5   /*stack at most 5 cars*/#define STACK_ELEMENT_TYPE Datatype        /* Needs to change type for different program !*/#define TRUE 1#define FALSE 0#define QUEUE_ELEMENT_TYPE Datatypetypedef struct {    char id[BUFF];        int ltime;    int atime;}Datatype;typedef struct {                             /*seqstack*/        STACK_ELEMENT_TYPE elem[STACK_SIZE];        int top;}seqstack;void Initstack(seqstack *S){     S->top=-1;}int Isempty(seqstack *S){    return(S -> top == -1 ? TRUE:FALSE); }int Isfull(seqstack *S){    return (S -> top == STACK_SIZE-1 ? TRUE:FALSE);}int Push(seqstack *S, STACK_ELEMENT_TYPE x){    if (S->top==STACK_SIZE)        return(FALSE);      /*stack is full*/    S->top++;    S->elem[S->top] = x;    return (TRUE);}int Pop(seqstack *S,STACK_ELEMENT_TYPE *x){    if(S->top==-1)       return(FALSE);   /*stack is empty*/    *x=S->elem[S->top];    S->top--;    return (TRUE);}STACK_ELEMENT_TYPE Gettop(seqstack *S){    STACK_ELEMENT_TYPE x;    if(S->top==-1)       printf("There is no element!\n");    x=S->elem[S->top];    return x;}typedef struct Node{              /* Linkqueue */       QUEUE_ELEMENT_TYPE data;       struct Node *next;}qnode;typedef struct {      qnode *front;      qnode *rear;}Linkqueue;void Initqueue (Linkqueue *Q){     Q->front = NULL;     Q->rear = NULL;}int Enterqueue(Linkqueue *Q, QUEUE_ELEMENT_TYPE x){    qnode *new;    new = (qnode *)malloc(sizeof(qnode));    if(new != NULL){      new -> data = x;      new -> next = NULL;      if(Q -> front == NULL){        Q -> front = new;        Q -> rear  = new;      }      else {        Q -> rear -> next = new;        Q -> rear = new;      }      return(TRUE);    }   else return (FALSE);   /* overflow */}int Deletequeue(Linkqueue *Q, QUEUE_ELEMENT_TYPE *x){    qnode *p;    if(Q->front == NULL)      return (FALSE);    p = Q -> front;    Q -> front = Q -> front -> next;  /* Pop out the head item */    if(Q -> rear == p){      Q -> rear = NULL;      Q -> front = NULL;    }    *x = p->data;     free (p);    return (TRUE);}int main(){    char ID[BUFF];    Datatype car,x;    seqstack park;    seqstack temp;    Linkqueue postern; /*便门*/    qnode *p,*r;    char state[BUFF];    int flag,flag1,flag2;    int nqueue,nstack,n,i;    int time;        Initstack(&park);    Initqueue(&postern);    Initstack(&temp);        nqueue=0;    while(1) {       printf("\nWelcome to our park!\nOur park can only park 5 cars!\nSo you can wait in the postern when the park is full \nand you can park in it if one car left.\nPlease input leave, arrive or stop where stop means stop this program.\n");       printf("[leave/arrive/stop]:");       scanf("%s",state);       if ( !strcmp(state,"arrive") ){        printf("One car is arriving ...\n\n");        printf("Please input arriving car's ID:");        scanf("%s",car.id);        printf("What's time now:");        scanf("%d",&car.atime);        if( !Isfull(&park) ){           Push(&park, car);           printf("The car is parking in the park with number %d\n",park.top+1);        }        else {            car.ltime = car.atime;            Enterqueue (&postern,car);            nqueue++;            n=0;            for(p = postern.front ; p != NULL ; p = p->next)                                            n++;            printf("Becase the park is full, this car is waiting in the postern with the number %d\n",n);          }       }       if( !strcmp (state,"leave") ){   /*if1*/        printf("One car is leaving ...\n\n");        printf("Please input leaving car's ID:");        scanf("%s",car.id);        printf("What's time now:");        scanf("%d",&time);        n = park.top;        nstack = n;        flag=0;        flag2=0;        if( Isempty(&park) ){         printf("There is no car in the park!\n");             continue;          }               do{       /*find the number of car in the park*/            strcpy(ID , park.elem[nstack].id);            if( !strcmp(car.id ,ID) ){             flag=1;       /*find!*/             break;           }                      nstack--;         }while(nstack != -1);          if(flag){         /*whether the car is in the park,if2*/          for(i=n; i>nstack ; i--){               Pop(&park,&x);               Push(&temp,x);           }          Pop(&park,&x);          printf("%s left and it spend %d hours in the park.\n",x.id,(time-x.atime) );          for(i=n ; i>nstack ; i--){               Pop(&temp,&x);                 Push(&park,x);           }          if(postern.front != NULL){             Deletequeue(&postern,&x);             nqueue--;             x.atime = time;             Push(&park,x);            }          }    /*if2*/                              else if(postern.front != NULL){ /*else1*/                                    p = postern.front;             strcpy(ID, p -> data.id );             if( !strcmp(ID,car.id) ){                flag2=1;                printf("%s left from the postern.\n",p->data.id);                r = p;                p = p->next;                                free(r);                continue; /* next operation goto while(1)*/             }             flag1 = 0;             for( p = postern.front ; p ->next != NULL ; p = p->next){                 strcpy(ID, p->next -> data.id);                    if( !strcmp(ID,car.id) ){                 flag2=1;                 flag1 = 1;                   break;                 }                         }/*for*/            if(flag1){                     printf("%s left from the postern.\n",p->next->data.id);               r = p->next;               p->next = p->next->next;               free(r);               continue;  /* next operation goto while(1)*/             }                         }/* else1 */           if( !(flag || flag2) )    /*flag means in the park,and flag2 means in the postern */              printf("Sorry! Cannot find this car. \nThis car is neither in the park nor in the postern.\n");                                    }/*if1*/       if ( !strcmp(state,"stop") ){           printf("You have stop this program !\n");         return ;       }       else          printf("\n\n");    }/* while */     }

⌨️ 快捷键说明

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