Parking problems

Application of experimental bilinear tables (2)

Designed Experiments

[Experimental Purpose]

1. Familiarize yourself with the features and operations of sequential and chain storage of linear tables;
2. Skilled in the basic operation of linear tables in different storage structures to implement algorithms;
3. This experiment helps students to deepen the use of C language (especially parameter invocation of functions, application of pointer types, establishment of chain lists and other basic operations)
4. Reasonable requirement analysis can be carried out for a practical problem, the appropriate storage structure can be selected, and the functions that meet the actual needs can be designed.

[Experimental Hours] 4 Hours

[Number of experimental groups] 1 person.

[Environment of Experimental Equipment] Computer, VC++6.0, C-Free, etc.

[Experimental Content]

1. Parking lot management (4 hours)
[Problem Description] There is a parking lot where n cars can be parked. It has two doors for vehicles to enter and exit, one of which is in and the other out. When the vehicle arrives at the parking lot, it can choose any idle parking space, and each parking space is numbered sequentially. If the parking lot is full of n vehicles, subsequent vehicles can only wait on the sidewalk outside the entrance of the parking lot. Once a car is driving in the parking lot, the first car on the sidewalk enters the parking lot. When each vehicle leaves the parking lot, it is charged based on how long it spends in the parking lot. If a vehicle parked on a freeway leaves before entering the parking lot, it is allowed to leave without parking fees and remains in the order of vehicles waiting on the freeway. Develop a program to simulate parking lot management.

[Basic Requirements] 1. Require the program to output the parking location (parking lot or sidewalk) after each vehicle arrives;
2. Expenses and length of stay for a vehicle leaving the parking lot;
3. You can check the status of parking lots and aisles at any time.
4. You can view the free parking spaces at any time. 4. You can view the free parking spaces at any time.
[Tips for implementation]
1. This topic can use static chain table as storage structure
2. The input format of the car simulation is: (arrival \departure, car license number, arrival \departure time). For example, ('A', 1,5) means vehicle 1 arrives at 5 hours, ('D', 5,20) means vehicle 5 leaves at 20 hours, and the end sign is ('E', 0,0).
Description: In addition to the basic functions required, the above topics can be freely used according to the requirements of actual research, add feasible functions, and make the functional application of the system more perfect.

#include "stdio.h"
#include "malloc.h"

#define MAX 3 // Maximum number of cars that a parking station can accommodate
#define SIZE_INIT_LANE 100
#Define INCREASE 10 //No increment
#define PRICE 10; //unit price
typedef int Elemtype;//Vehicle Number Variable Type

typedef struct
{
    Elemtype Car_license;        //Car number
    int Car_Inbound;             //Inbound moment
    int Car_Outbound;           //Outbound Moment
    int  flag;                  //Is there a car sign
    struct Stop_car* next;

}Stop_car;//Static Chain List for Parking

typedef struct
{
    Elemtype* Car_license;
    int num;
    int size;

}Lane;//Freeway Dynamic Array


//global variable
int Stop_car_Num;//Number of parking stations


//Function declaration
int Init_Stop_car(Stop_car* s);
int Init_Lane(Lane* L);
int push_car(Elemtype carNum, int time, Stop_car* s, Lane* L);
int pull_car(Elemtype carNum, int time, Stop_car* s, Lane* L);
Stop_car* find_INSTOP(Stop_car* s, Elemtype carNum);
int find_Lane(Lane* L, Elemtype carNum);
void meau(Stop_car* s, Lane* L);



int main()
{
    Stop_car* s;
    s = (Stop_car*)malloc(sizeof(Stop_car));
    Lane* L;
    L = (Lane*)malloc(sizeof(Lane));
    Init_Stop_car(s);
    Init_Lane(L);
    meau(s, L);


    return 0;
}
/*---Parking lot initialization--*/
int Init_Stop_car(Stop_car* s)
{
    if (s == NULL)
    {
        return 0;
    }
    // S->Car_license =';//Car number is empty
   //   s->Car_Inbound = 0;
   //  s->Car_Outbound = 0;
    s->next = NULL;            //Head Plug Initialization
 //   s->flag = 0;

    Stop_car_Num = 0;       //The initial number of cars in the parking lot is zero
    return 1;

}
/*---Initialization of sidewalk--*/
int Init_Lane(Lane* L)
{
    L->Car_license = (Elemtype*)malloc(SIZE_INIT_LANE * sizeof(Elemtype));
    if (L->Car_license == NULL)
        return 0;
    L->num = 0;
    L->size = SIZE_INIT_LANE;
    return 1;

}

/*---Car Inbound (Parking/Casual Stop)---*/
int push_car(Elemtype carNum, int time, Stop_car* s, Lane* L)
{
    //When the parking lot can accommodate cars

    if (Stop_car_Num < MAX)
    {
        Stop_car* node = (Stop_car*)malloc(sizeof(Stop_car));
        if (node == NULL)
        {
            return 0;
        }
        node->Car_license = carNum;
        node->Car_Inbound = time;        //Arrival time
        node->flag = 1;

        node->next = s->next;
        s->next = node;
        Stop_car_Num++;
        return 1;

    }
    else
    {
        if (L->num < SIZE_INIT_LANE)
            L->Car_license[L->num++] = carNum;
        else
        {
            L->Car_license[L->num++] = carNum;
            L->Car_license = (char*)realloc(L->Car_license, (L->size + INCREASE) * sizeof(char));
            if (L->Car_license == NULL)
                exit(0);
            L->size += INCREASE;
        }
        return 1;


    }

}
/*---Car outbound (parking/freeway)---*/
int pull_car(Elemtype carNum, int time, Stop_car* s, Lane* L)
{
    float Price;                                            //Here you can write another function, which makes the outbound function a bit more functional.
    Stop_car* ss = find_INSTOP(s, carNum);
    if (ss != NULL)
    {
        Stop_car* p = ss->next;
        p->Car_Outbound = time;
        Price = (p->Car_Outbound - p->Car_Inbound) * PRICE;
        ss = p->next;
        free(p);
        printf("\n Successful outbound at this cost%.3f", Price);
        if(L->num>=1)
        {
            push_car(L->Car_license[0],time,s,L);
            L->Car_license++;
            L->num--;

        }
        else
        {
            return 1;
        }
    }
    else if (ss == NULL)
    {
        int f = find_Lane(L, carNum);
        if (f >= 0)
        {
            for (int i = f; i < L->num; i++)
            {
                L->Car_license[i] = L->Car_license[i + 1];
            }
            L->size--;
            return 1;
        }
        else
        {
            printf("This car is temporarily unavailable");
            return 0;
        }

    }
    else
    {
        printf("This car is temporarily unavailable");
        return 0;
    }


}
/*---Determine if a car is in a parking lot--*/
Stop_car* find_INSTOP(Stop_car* s, Elemtype carNum)
{
    Stop_car* ss = s;
    Stop_car* p = ss->next;

    while (p != NULL)
    {
        if (p->Car_license == carNum)
            return ss;
        ss = p;
        p = p->next;

    }
    return NULL;
}
/*---Determine if the car is on the freeway--*/
int find_Lane(Lane* L, Elemtype carNum)
{
    Lane* LL = L;
    for (int i = 0; i < LL->num; i++)
    {
        if (LL->Car_license[i] == carNum)
            return i;
    }
    return -1;

}
/*---Station Management Menu--*/
void meau(Stop_car* s, Lane* L)
{
    int flag;
    char ch,ch1;
    Elemtype carNum;
    int time;

    printf("---------------------Parking Station Simulation---------------------\n");
    printf("Input format (arrival)/Leave,License plate number,Achieve/The moment to leave)\n");
    printf("Please enter ( A:Represent Inbound D: On behalf of outbound P: End of Representative(Show status after completion))\n");
    flag = 1;
    while (flag)
    {
        printf("Input format (arrival)/Leave,License plate number,Achieve/The moment to leave)\n");
        scanf("%c", &ch);
        ch1 = getchar();
        switch (ch)
        {

        case 'A': {
            scanf("%c,%d",&carNum,&time);
            ch1 = getchar();
                     if(find_INSTOP(s,carNum)||find_Lane(L,carNum)>=0)
                    {
                        printf("The car is nearing the station\n");

                    }
                    else
                    {
                        push_car(carNum,time,s,L);
                        printf("Inbound Success\n");
                    }


        }; break;
        case 'D': {
            scanf("%c,%d",&carNum,&time);
            ch1 = getchar();
            pull_car(carNum, time, s, L);
            printf("Outbound success\n");


        }; break;
        case 'P': {
            printf("Parking Station Left%d Locations, changing lanes, and%d Personal queue\n",MAX-Stop_car_Num,L->num);
            ch1 = getchar();

        }; break;
        }

    }


}


Ideas:

For the first time, the freeway wants to be queued, but it is difficult to delete one of the freeway cars, so a dynamic sequence table is used.
Look at the other codes, yeah, I didn't assign a location to the random location of that station (assignment is easy, but random location isn't so easy, let's not assign a stop).
This question is a comprehensive exercise on simple list of orders and chains.

Here are some errors and explanations





The above is provided by a kind person from qq group. It is only personal opinion. If there are any errors, please understand. If you can't, just comment. I study

Tags: data structure

Posted on Wed, 20 Oct 2021 12:44:53 -0400 by Yossarian