c language project -------- parking lot management system

        添加摘要           

1 Design Purpose The
"Data Structure" course mainly introduces the most commonly used data structures, clarifies the internal logical relationships of various data structures, discusses their storage representation in computers, and the implementation algorithms when performing various operations on them. The efficiency of the algorithm is simply analyzed and discussed. The data structure course design should achieve the following goals:

1. Understand and master the design methods of data structure and algorithms, and have preliminary independent analysis and design capabilities;

2. Preliminary mastery of basic methods and skills such as problem analysis, system design, program coding, and testing in the software development process;

3. Improve the ability to analyze and solve problems independently using the theoretical knowledge and methods learned;

4. Train software development with systematic viewpoints and general software development specifications, and cultivate scientific working methods and styles that software workers should possess.

2 Task overview
There is a long and narrow parking lot that can park n (n>=5) cars. It has only one gate for vehicles to enter and exit. The vehicles are parked from the innermost of the parking lot to the gate according to the time of arrival at the parking lot (the first car that arrives first is placed at the innermost of the parking lot). If the parking lot is full of n cars, subsequent vehicles can only wait on the sidewalk outside the gate of the parking lot. Once a car drives away in the parking lot, the first car in the sidewalk will enter the parking lot. If there is a car in the parking lot that wants to drive away, all cars entering the parking lot after him must exit the parking lot to make way for it. After they drive out of the parking lot, these vehicles will enter the field in the original order. When each car leaves the parking lot, it should pay the fee according to the length of time it stays in the parking lot. If the car staying on the sidewalk has not entered the parking lot, it must leave, allowing it to leave, no parking fee is charged, and the order of waiting vehicles on the sidewalk is still maintained. Prepare a program that simulates the management of the parking lot.
3 Data structure used in this design

        //时间结点



               typedef
                struct{

                    int day;

                    int hour;

                    int min;

                }TIME;




        //车辆信息

            typedef
            struct{

                char num[10];  //车牌号 

                TIME time;  
            //停车

        时间 



               int n;     

        //停车场中所在位置 

            }information;




        //栈的结构体定义

        typedef
        struct node

    {

        information data;

        struct node *next;

    }stacknode;

    stacknode
    *top1,*top2;//top1 为停车场  top2为暂存域



    //队列结构体定义

    typedef
    struct{

        information data;

        s

tacknode *front,*rear;

}LQueue;

LQueue
*Q;

1. This program mainly uses two stacks to simulate a last-in first-out parking lot, and one queue is used to simulate a first-in first-out parking lane.

2. As the vehicle needs to record the license plate number and parking time in the parking lot,

And the location of the vehicle, so the stack structure is composed of nested vehicles

The information is composed of member names and pointer fields, and the vehicle information is composed of license plate number, parking time and the location of the vehicle in the parking lot.

3. The queue structure is composed of vehicle information data fields and head and tail pointers

4 System function module structure diagram and main function description

System function module description:

The vehicle arrives,

Vehicle leaving

Parking lot information and access road information printing

The main functions of the system and their description:

  1. Cars enter the parking lot function

    stacknode
    *into(stacknode *top1,LQueue *Q)

    First enter the information of the vehicle to be parked and then determine whether the parking lot is full, if it is not full, park the vehicle, if it is full, park the vehicle into the sidewalk and wait. Use the pointer p to store the information of the vehicle to be stopped, and then serve as the node of the stack or queue according to the conditions.

The function flow chart is shown in Figure 1.1.
Insert picture description here

2. The function of the vehicle leaving the parking lot
Insert picture description here

 stacknode *leave(stacknode *top1,charstr[],LQueue *Q)

First judge whether the parking lot is empty, if it is not empty, then the vehicle will leave, then judge whether there is a car on the sidewalk, if there is a sidewalk, the vehicle will enter

Parking lot, otherwise it ends. Point q to the parking lot stack top1 to search for the location of the vehicle, and then stop the vehicle in front of the vehicle

The top1 stack of the parking lot enters the temporary storage stack top2, and after the vehicle leaves the top1 stack of the parking lot, the nodes in top2 are sequentially removed from the stack and enter top1.

Finally, judge whether to park the sidewalk vehicles according to whether there are vehicles on the sidewalk.

3. Print parking lot information/print sidewalk vehicle information function

void showtop1(stacknode *top1)


int showLQ(LQueue *Q)

Mainly use while to traverse the stack or queue.

The function flow chart is shown in Figure 1.3. The function flow chart of printing parking lot information/printing road vehicle information is shown in figure 1.3.
Insert picture description here

Figure 1.3 Function flowchart of printing parking lot information/printing sidewalk vehicle information

5 Program running data and results

  1. The running result of vehicles entering the parking lot is shown in Figure 1.4.
    Figure 1.4 The result of parking the vehicle
    Insert picture description here

  2. The running result of the vehicle leaving the parking lot is shown in Figure 1.5.
    Insert picture description here

Figure 1.5 Operation result of vehicle leaving Figure
Insert picture description here
3. Operation result of parking lot information printing operation result is shown in Figure 1.6.
Insert picture description here

  1. The running result of waiting lane vehicle information printing is shown in Figure 1.7.
    Bold style


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <time.h>

#define max 3

#define price 1

int b=1;

 

typedef struct{

       int
day;

       int
hour;

       int
min;

}TIME;//时间结点 

typedef struct{

       char
num[10];  //车牌号 

       TIME
time;   //停车时间 

       int
n;      //停车场中所在位置 

}information; 

 

//栈的结构体定义

typedef struct node{

       information
data;

       struct
node *next;

}stacknode;

stacknode *top1,*top2;//top1 为停车场  top2为暂存域

 

//队列结构体定义

typedef struct{

       information
data;

       stacknode
*front,*rear;

}LQueue;

LQueue *Q;

 

//队列初始化 

LQueue *InitQ()

{

       LQueue
*Q;

       stacknode
*p;

       Q
= (LQueue *)malloc(sizeof(LQueue));

       p
= (stacknode*)malloc(sizeof(stacknode));

       p->next
= NULL;

       Q->front
= Q->rear = p;

       return
Q;

}

 

//栈的初始化

stacknode *Init()

{

       stacknode
*top;

       top
= (stacknode*)malloc(sizeof(stacknode));

       top
= NULL;

       return
top;

}

 

/*

 

调用时间函数用法 

time_t t;

   
struct tm * lt;

   
time (&t);//获取Unix时间戳。

   
lt = localtime (&t);//转为时间结构。

 

时间结构体struct tm 说明:

struct tm { 

int tm_sec; /* 秒 – 取值区间为[0,59] */ 

//int tm_min; /* 分 - 取值区间为[0,59] */ 

//int tm_hour; /* 时 - 取值区间为[0,23] */ 

//int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ 

//int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ 

//int tm_year; /* 年份,其值等于实际年份减去1900 */ 

//int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ 

//int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ 

//int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/ 

//}; */ 

 

//车辆进入便道函数

LQueue *wait(LQueue *q,stacknode *s)

{

       s->next
= NULL;

       q->rear->next
= s;

       q->rear
= s;

       return
q;

       

 } 

 

 

//车辆进入停车场函数 

stacknode *into(stacknode *top1,LQueue *Q)

{

       stacknode
*p,*q;

       time_t
t;//调用系统时间函数

       struct
tm *timeinfo; //时间结点

       time(&t);

       timeinfo
= localtime(&t);

       p
= (stacknode *)malloc(sizeof(stacknode));

       if(p
== NULL)

       {

               printf("内存分配失败!\n");

               return
top1;

        } 

       printf("请输入车牌号:\n");

       scanf("%s",p->data.num);

       q
= top1;

       p->data.time.day
= timeinfo->tm_mday;

       p->data.time.hour
= timeinfo->tm_hour;

       p->data.time.min
= timeinfo->tm_min;

       p->data.n
= b;

       if(b>max)

       {

               printf("停车场以满,请进入便道等候\n");

               wait(Q,p);

              return top1;

       }

       if(top1==NULL)

       {

               p->next
= NULL;

               top1
= p;

       }else{

              p->next = top1;

               top1
= p;

       }

       b++;

       printf("车辆已进入停车场!\n");

       printf("车牌为%s的汽车驶入时间为:%d日%d点%d分\n",top1->data.num,top1->data.time.day,top1->data.time.hour,top1->data.time.min);

       return
top1;

 } 

 

//判断候车区是否为空

int EmptyLQ(LQueue *q) 

{

       if(q->front
== q->rear)

       return
0;

       else

       return
1;

}

 

//候车区出队

stacknode *outQ(LQueue *q)

{

       stacknode
*p;

       p
= q->front->next;

       if(q->front->next
== q->rear)//如果候车区只有一辆车 

       {

              q->rear = q->front;

               return
p;

       }else{

               q->front->next
= p->next;

       }

              p->next = NULL;

              return p;

 } 

 

//候车区车辆进入停车场 

stacknode *LQinto(stacknode *p,stacknode
*top1)

{

       p->next
= top1;

       top1
= p;

       return
top1;

}

 

//车辆计费函数

int expense(stacknode *q,int x1,int x2,int
x3)

{

       int
w;

       if(x3!=0)

               w =
(x1*24+x2+1-(top1->data.time.day*24+top1->data.time.hour+top1->data.time.min))*price;

       else

              w = (x1*24+x2-(top1->data.time.day*24+top1->data.time.hour+top1->data.time.min))*price;

       return
w;

 }

 

 //车辆驶出停车场函数

 stacknode *leave(stacknode *top1,char str[],LQueue
*Q)

 {

      int a,i,day,hour,min;

      time_t 
t;

       struct
tm *timeinfo;

       time(&t);

       timeinfo
= localtime(&t);

       day
= timeinfo->tm_mday;

       hour
= timeinfo->tm_hour;

       min
= timeinfo->tm_min;

       stacknode
*p,*q;

       if(top1
== NULL)

       {

             printf("停车场为空!\n");

             return
top1;

       }


       q
= (stacknode*)malloc(sizeof(stacknode));

       if(q
== NULL){

             printf("内存分配失败");

             return
top1;

       }

       q
= top1;

       while(q!=NULL)

       {

             if(strcmp(str,q->data.num)==0)

             break;

             q=
q->next;

       }

       if(q==NULL)

       {

       printf("输入错误,无此车辆!\n");

       return
top1;

       }

       for(i
= top1->data.n;i>top1->data.n;i--)//将该车辆之前的车停入临时地点 

       {

               p =
(stacknode *)malloc(sizeof(stacknode));

               if(p
== NULL)

               {

                     printf("内存分配失败!\n");

                      return
top1;

               }

               strcpy(p->data.num,top1->data.num);

               p->data.n
= top1->data.n-1;

               p->data.time
= top1->data.time;

               top1
= top1->next;

               if(top2
== NULL)

               {

                     p->next
= NULL;

                      top2
= p;

               }else{

                      p->next
= top2;

                      top2
= p;

              }

        }

       a
= expense(q,day,hour,min);

       top1=top1->next;//指定车辆停出

       while(top2!=NULL)

       {

               p =
(stacknode *)malloc(sizeof(stacknode));

               if(p
== NULL)

               {

                      printf("内存分配失败!\n");

                      return
top1;

               }

               strcpy(p->data.num,top2->data.num);

               p->data.n
= top2->data.n;

              p->data.time = top2->data.time;

              p->next = top1;

              top1 = p;

              top2=top2->next;

       }


       if(EmptyLQ(Q))//车辆出停车场完成,判断便道是否有车 

       {

              p = outQ(Q);//候车区车辆出队 

              p->data.n--;

              top1 = LQinto(p,top1);//入栈 

        }else{

              b--;

       }


              printf("车牌为%s的汽车驶出时间为:%d/%d:%d\n",q->data.num,day,hour,min);

              printf("车辆驶出停车场需要缴纳的费用为:%d元\n",a);         

              return top1;

  } 

 

//打印停车场信息 

void showtop1(stacknode *top1)

{

       printf("   停车场内全部车辆信息表\n");

       if(top1==NULL)

              printf("  停车场内无车!\n");

       else{

                     printf("车牌号      进入时间       位置\n");

              while(top1!=NULL){

                     printf("
%s        %d/%d:%d   第%d位\n",top1->data.num,top1->data.time.day,top1->data.time.hour,top1->data.time.min,top1->data.n);

                     top1=top1->next;

              }

       }

}

 

//打印便道车辆信息 

int showLQ(LQueue *Q){

       stacknode
*p;

       p
= Q->front->next;

       if(p==NULL)

       {

              printf("内便道无车辆\n"); 

       }

       while(p){

              printf("   候车便道内全部车辆信息表\n"); 

              printf("车牌号:%s\n",p->data.num);

              p=p->next;

       }

       printf("\n");

       return
1;

}

 

int main()

{

       char
str[10];

       Q
= InitQ();

       top1
= Init();

       top2
= Init();

       Q
= InitQ();

       int
i;

       printf("\t\t\t#####################################\n");

       printf("\t\t\t       停车场管理系统\n");

       printf("\t\t\t||    1. 车辆进入停车场            ||\n");

       printf("\t\t\t||    2. 车辆离开停车场            ||\n");

       printf("\t\t\t||    3. 显示停车场内所有车辆信息  ||\n");

       printf("\t\t\t||    4. 显示候车区内所有车辆信息  ||\n");

       printf("\t\t\t||    5. 退出                      ||\n");

       printf("\t\t\t#####################################\n");

       while(i!=5){

       printf("\t请输入选项1-5:");

       scanf("%d",&i);

       switch(i){

       case
1:

              top1=into(top1,Q);

              break;

       case
2:

              printf("请输入离开车辆的车牌号:");

              scanf("%s",str);

              top1=leave(top1,str,Q);

              break;

       case
3:showtop1(top1);break;

       case
4:showLQ(Q);break;

       case
5:exit(1);

       default:printf("输入错误,请重新输入1-5:");

              break;

       }            

       }

 }


Guess you like

Origin blog.csdn.net/weixin_44192389/article/details/100147773