数据结构-停车场系统的实现

#include <stdio.h>   /*包含了与标准IO库有关的变量定义和宏定义*/
#include <stdlib.h>  /*文件包含所用的文件*/
#include <conio.h>    /*clrscr()*/
#include <string.h>
#include <malloc.h>

int cars[12][4]={{1111,1,1,5},{2222,1,2,10},{3333,1,3,10},
{4444,1,4,15},{5555,1,5,20},{6666,1,6,25},{7777,2,1,10},{8888,2,2,10},{9999,2,3,10},
{1212,2,4,10},{1313,2,5,10},{0,2,6,0}};      /*二维数组代表停车信息*/

typedef struct {int number;
}Car;                                     /*车牌 */

typedef struct QNode{                    /*定义一个链式队列(候车道)*/
        Car data;
        struct QNode *next;             /*指向下一结点*/
}QNode,*QueuePtr;

typedef struct{
        QueuePtr front;
        QueuePtr rear;
}LinkQueue;

void save();
void car_park();                 /*停车操作*/
void car_get();                  /*取车操作*/
void printfdata();               /*打印停车场车辆信息*/
void initialization() ;          /*初始函数*/
int  readcommand() ;             /*选择功能*/
int  InitQueue();
int  EnQueue();
int  DeQueue();
int  print();


void save(){
        FILE *fp;
        int i,j;
        if((fp=fopen("cars","w"))==NULL){
        printf("cannot open file/n");
        return;
        }
        for(i=0;i<12;i++)
        for(j=0;j<4;j++)
        if(fwrite(cars,2,1,fp)!=1)
        printf("file write error/n");
        fclose(fp);
        getch();
}

void printfdata()           /* 停车场信息*/
{
       int i,j,b,z;
       FILE *fp;
       QueuePtr p;
       fp=fopen("cars","r");
       printf("/n Number Floor Position Time/n");
       for(i=0;i<12;i++)
       {for(j=0;j<4;j++)
         {
           fread(cars,2,1,fp);
           printf("%6d",cars[i][j]);
         }         printf("/n");
       }
       gotoxy(29,30);
       printf("/nthe cars on waiting queue/n");
       print();
       fclose(fp);
}


/*候车道队列操作*/
int InitQueue(LinkQueue *Q)        /*构造一个空队列 Q*/
{
        Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
        if(!Q->front) exit(0);
        Q->front->next=NULL;
        return 1;

}

int EnQueue(LinkQueue *Q,Car e )       /*候车道插入操作*/
{
        QueuePtr p;
        p=(QueuePtr)malloc(sizeof(QNode));
        if(!p) exit(0);
        p->data=e; p->next=NULL;
        if(Q->rear==NULL){
        Q->front=Q->rear=p;
        }
        else{Q->rear->next=p;
        Q->rear=p;
        printf("/nThanks using this car park!");
        }
        return 1 ;
}

int DeQueue(LinkQueue *Q,Car e)  {           /*等候车辆出队*/


        QueuePtr p=Q->front;
        if(Q->front==NULL){         /*空队*/
        printf("/nQueue is empty,can't dequeue/n");
        getch();
        }
        e=p->data;
        Q->front=p->next;
        if(Q->rear==p)         /*只有一个结点,则对空顺便制队尾空*/
        Q->rear=NULL;
        free(p);
        printf("/nThanks using this car park!/n/n");
        return 1;
        /*就是这里有点问题*/
}

int print(LinkQueue *Q)      /*读取队列*/
{
          QueuePtr p1;
          p1=Q->front;
          while(p1!=NULL){
          printf("the car:%d/n",p1->data);
          p1=p1->next;
          }
          return;
}

 /*停车场汽车管理,执行顺序是输入车牌后场满,车辆进入等待队列*/
void car_park()
{
        Car e;
        int x,i,a;
        QueuePtr p;
        LinkQueue Q;
        InitQueue(&Q);
        printf("/nwelcome to our car park^-^!/n");
        printf("/nplease input your car number,then two enter/n");
        scanf("%d",&x);                     /*输入车牌号*/
        for (i=0;i<12;i++)
        if(cars[i][0]==x||x<1000||x>9999) break;

        if(i!=12)
          {printf("/nWrong number or it's parked !!!/n");
                getchar(); }                        /*如果此车号以在,打印此车已停*/
        else if(i==12&&x>=1000&&x<=9999)
                {for (i=0;i<=12;i++)
                        if(cars[i][0]==0) {          /*如果此车号不在,则进行停车操作*/
                                 cars[i][0]=x;save();
                                 printf("/n/nSUCCESS/n/n");
                                 printf("Floor=%d,position=%d/n",cars[i][1],cars[i][2]);
                                 printf("/n/n/nTwo times 'Enter' to end...");break;

                          }
                 for(i=0;i<12;i++)                    /*停车所有时间*/
                 if(cars[i][0]!=0) cars[i][3]+=5;
                 save();
          }

        if(cars[i][0]>=0) {                            /*无空车位,进入等待队列*/
                                printf("the park is full./nDo you want to wait in waiting queue to a empty position/n/n yes-1  no-0/n/n");
                                scanf("%d",&a); {
                                 switch(a){
                                  case 1:
                                         printf("/nplease input your number again/n");
                                         scanf("%d",p->data);
                                         EnQueue(&Q,e);               /*无空位的时候入队*/
                                         printf("/nwe will inform you immediately if there is a empty car position/n"); break;
                                  case 0: printf("Good Bye!/n"); getchar(); break;
                                  default:  getchar();   break;
                                           }      }
                            }
        }


/*取车操作*/
void car_get(){
      Car e;
      int i,y,a,z,q;
      float paid;
      LinkQueue Q;
      InitQueue(&Q);
      printf("/n Get car/n/n Input your car number:/n/n");
      printf("NOTICE:car number is a digit between 1000 and 9999/n    Wrong load would have no cue/n/n");
      scanf("%d",&y);
      for(i=0;i<12;i++)
        if(cars[i][0]==y){
                paid=cars[i][3];
                printf("/nyour paid is:%f yuan",paid);                 /*计算停车费*/
                cars[i][0]=0;                                           /*取车后车牌清0*/
                cars[i][3]=0;                                          /*时间清0*/
                printf("/n/nGood Bye!");
                printf("/n/n there is a empty position in the park./nif you want to park your car. you can press 0 to continue.... /n");
                 }save();
                scanf("%d",&q);                   /*候车队车辆出队操作*/
                switch(q) {
                case 0:  DeQueue(&Q,e); break;    /*有空位后车主选择入场前出队操作*/
                default: getchar(); break;
                           }

         if(i==12)
          printf("The number is not in the park!!!/n");
          printf("input two 'Enter' to exit!!!/n");
          getch();

}


void main()
{
   int c ;
   while(1)
     {
   initialization();                  /*初始化界面*/
   c=readcommand();                   /*读取停车场状况*/
   clrscr();                           /*清除文本模式窗口 */
   switch(c)
{
    case 1: car_park();               /*停车操作*/
      printf("/n/nplease press 'Enter' to continue..../n");
      scanf("%c",&c);
      break;
    case 2: car_get();                 /*取车操作*/
    printf("/n/nplease press 'Enter' to continue..../n");
      scanf("%c",&c);
      break;
    case 3: printfdata();                /*停车/候车信息*/
       gotoxy(30,30);
       printf("/n/nplease press 'Enter' to continue..../n");
       scanf("%c",&c);
       break;
    case 0: printf("/n/n/n/n    Press 'Enter' to continue...");exit(0); break;

    default : printf("ERROR! Press 'Enter' to continue...");      getchar(); break;
}
}
}


int readcommand()      /*选择函数*/
{
   int c;
while((c!=0)&&(c!=1)&&(c!=2)&&(c!=3))
{
      printf("Input 0,1,2,3 choose!!/n");
      scanf("%d",&c);
      printf("/n");break;
    }
    return c;
}


void initialization()   /*初始函数*/
{
     int i;
     getchar();
     clrscr();
     gotoxy(0,0);
     for(i=1;i<=80;i++)      printf("/1/4");
     for(i=1;i<=80;i++)             printf("/2");
     gotoxy(15,6);
       printf("THIS IS MY CAR PART MANAGE SYSTEM!");
     gotoxy(15,9);
       printf("NAME:   bichun /1");        /*我的名字***/
     gotoxy(15,10);
       printf("NUM:     0507508162  /2");           /*学号*/

     gotoxy(1,14);
       printf("/n*****************************************************************************/n");
       printf(" 1. Park car--1     2. Get car--2     3. Date of parking--3      0.Exit--0");
       printf("/n/n*****************************************************************************/n");
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值