天天看點

基于LinuxC下的停車場項目

停車場項目是我們學完棧與隊列後的一個綜合項目,下面貼一下具體要求。

停車場項目需求

問題描述:停車場是一個能放 n 輛車的狹長通道,隻有一個大門,汽車按到達的先後次序停放。若車場滿了,車要停在門外的便道上等候,一旦有車走,則便道上第一輛車進入。當停車場中的車離開時,由于通道窄,在它後面的車要先退出,待它走後在依次進入。汽車離開時按停放時間收費。

基本功能要求:

(1)建立三個資料結構分别是:停放棧、讓路棧、等候隊列。

(2)輸入資料模拟管理過程,資料(入或出,車号)

功能描述:進車登記、出車登記、按車牌号查詢停車車輛資訊、查詢出入車記錄、查詢場内車輛資訊、查詢等候車輛資訊、退出系統。

(1)linux系統編寫(連結清單、棧、隊列);

(2)進車登記:登記車牌号以及入場時間;

(3)出車登記:計算出停車時間,記錄車輛車牌;

(4)按車牌号查詢車輛資訊:停車時間,是否來過停車場,是否還在停車場

(5)查詢出入記錄:所有車輛,包括已經離開的

(6)查詢場内車輛資訊:列出所有場内車輛資訊

(7)查詢等候車輛資訊:顯示等候車輛數量以及所有車牌号

(8)退出系統。

頭檔案

#ifndef _PARKLOT_H_
#define _PARKLOT_H_

#define maxsize  10
#define success  100001
#define failure  100002
#define waiting  100003

typedef int elemtype;

struct parknode             //parknode
{
    char data[];
    elemtype intime;
    struct parknode *next;
};

typedef struct parknode Park;
typedef Park* LinkPark;

struct stack
{
    LinkPark top;
    int count;
};

typedef struct stack Stack;     

struct waitnode             //waitnode
{
    char data[];
    struct waitnode *next;
};

typedef struct waitnode Wait;
typedef Wait* LinkWait;

struct queue
{
    LinkWait front;
    LinkWait rear;
};

typedef struct queue Queue;

struct record               //memory record
{
    char data[];
    elemtype intime;
    elemtype outtime;
    struct record *next;
};

typedef struct record Record;

struct recordstack
{
    Record *top;
    int count;
};

typedef struct recordstack RecordStack;


int InitStack(Stack **S);
int InitQueue(Queue *Q);
int InitRecordStack(RecordStack **R);
int CheckIn(Stack *P, Queue *W, elemtype t, int i);
int CheckOut(Stack *P, Stack *T, RecordStack *R, elemtype t);
int AutoIn(Stack *P, Stack *T, Queue *W, elemtype i, int l);
int SearchInfo(RecordStack *R, Stack *P, Queue *W);
int SearchAll(RecordStack *R, Stack *P, Queue *W);
int SearchPark(Stack *P, elemtype timenow);
int SearchWait(Queue *W);

#endif
           

接口函數

#include <stdio.h>
#include "ParkLot.h"
#include <stdlib.h>
#include <string.h>


int InitStack(Stack **S)
{
    (*S) = (Stack *)malloc(sizeof(Stack));
    if(NULL == (*S))
    {
        return failure;
    }
    (*S)->top = NULL;
    (*S)->count = ;
    return success;
}

int InitQueue(Queue *Q)
{
    Q->front = (LinkWait)malloc(sizeof(Wait));
    if(NULL == Q->front)
    {
        return failure;
    }
    Q->rear = Q->front;
    Q->rear->next = NULL;
    Q->rear->data[20] = 0;
    return success;
}

int InitRecordStack(RecordStack **R)
{
    (*R) = (RecordStack *)malloc(sizeof(RecordStack));
    if(NULL == (*R))
    {
        return failure;
    }
    (*R)->top = NULL;
    (*R)->count = ;
    return success;
}

int CheckIn(Stack *P, Queue *W, elemtype t, int i)
{
    LinkPark p = (LinkPark)malloc(sizeof(Park));
    LinkWait w = (LinkWait)malloc(sizeof(Wait));
    char name[] = {};
    scanf("%s", name);
    if(strlen(name) > )
    {
        printf("Please Input right number!\n");
        return failure;
    }
    int k = P->count;

    if(NULL == p || NULL == w)
    {
        return failure;
    }

    if(k < i )
    {
        strcpy(p->data, name);
        p->next = P->top;
        p->intime = t;
        P->top = p;
        P->count++;
        return success;
    }
    else
    {
        strcpy(w->data, name);
        w->next = NULL;
        W->rear->next = w;
        W->rear = w;
        return waiting;
    }

}

int CheckOut(Stack *P, Stack *T, RecordStack *R, elemtype t)
{
    char name[] = {};
    scanf("%s", name);
    int k = P->count;
    Record *r = (Record *)malloc(sizeof(Record));
    if(NULL == r)
    {
        return failure;
    }
    while(k > )
    {
        LinkPark p = P->top;
        LinkPark tmp;
        if(strcmp(name, p->data) != 0)
        {
            tmp = p;
            p = p->next;
            P->top = p;
            P->count--;
            tmp->next = T->top;
            T->top = tmp;
            T->count++;
        }
        else
        {
            strcpy(r->data, name);
            r->intime = p->intime;
            r->outtime = t;
            r->next = R->top;
            R->top = r;
            R->count++;
            P->top = p->next;
            free(p);
            P->count--;
            return success;
        }
        k--;
    }

    return failure;
}

int AutoIn(Stack *P, Stack *T, Queue *W, elemtype i, int l)
{
    int j = T->count;
    char name[];
    while(j > )
    {
        LinkPark tmp = T->top;
        T->top = tmp->next;
        T->count--;
        tmp->next = P->top;
        P->top = tmp;
        P->count++;
        j--;
    }

    if(W->front->next != NULL)
    {
        sleep();
        printf("Waiting car is parking....New coming car please wait!\n");

        sleep();
        LinkWait w = W->front->next;
        strcpy(name, w->data);
        W->front->next = w->next;
        free(w);
        if(NULL == W->front->next)
        {
            W->rear = W->front;
        }

        LinkPark p =(LinkPark)malloc(sizeof(Park));
        if(NULL == p)
        {
            return failure;
        }

        strcpy(p->data, name);
        p->intime = i;
        p->next = P->top;
        P->top = p;
        P->count++;
        return success;
    }
    return failure;
}

int SearchInfo(RecordStack *R, Stack *P, Queue *W)
{
    Record *r = R->top;
    LinkPark p = P->top;
    LinkWait w = W->front->next;
    char name[];
    scanf("%s", name);
    if(strlen(name) > )
    {
        printf("Please Input right number!\n");
        return failure;
    }

    while(r != NULL)
    {
        if(strcmp(name, r->data) == 0)
        {
            printf("Licence Plate:%s\nCheckInTime:%d\nCheckOutTime:%d\nParkingTime:%ds\n", r->data, r->intime, r->outtime, r->outtime - r->intime);
            return success;
        }
        r = r->next;
    }

    while(p != NULL)
    {
        if(strcmp(name, p->data) == 0)
        {
            printf("Licence Plate:%s\nCheckInTime:%d\nThis car now is in parklots!\n", p->data, p->intime);
            return success;
        }
        p = p->next;
    }

    while(w != NULL)
    {
        if(strcmp(name, w->data) == 0)
        {
            printf("Licence Plate:%s\nThis car is waiting for parking!\n", w->data);
            return success;
        }
        w = w->next;
    }

    return failure;
}   

int SearchAll(RecordStack *R, Stack *P, Queue *W)
{
    Record *r = R->top;
    LinkPark p = P->top;
    LinkWait w = W->front->next;

    printf("The histroy:\n");
    while(r != NULL)
    {
        printf("Licence Plate:%s  CheckInTime:%d  CheckOutTime:%d  ParkingTime:%ds\n", r->data, r->intime, r->outtime, r->outtime - r->intime);
        r = r->next;
    }

    printf("The now car here:\n");
    while(p != NULL)
    {
        printf("Licence Plate:%s  CheckInTime:%d  \n", p->data, p->intime);
        p = p->next;
    }

    printf("The waiting car:\n");
    while(w != NULL)
    {
        printf("Licence Plate:%s \n", w->data);
        w = w->next;
    }

    return success;
}

int SearchPark(Stack *P, elemtype timenow)
{
    LinkPark p =P->top;

    printf("Parking Information:\n");
    while(p != NULL)
    {
        printf("Licence Plate:%s  CheckInTime:%d ParkingTime:%ds \n", p->data, p->intime, timenow - p->intime);
        p = p->next;
    }

    return success;
}

int SearchWait(Queue *W)
{
    LinkWait w = W->front->next;

    printf("The Waiting Car Information:\n");
    while(w != NULL)
    {
        printf("Licence Plate:%s \n", w->data);
        w = w->next;
    }

    return success;
}
           

主函數

#include <stdio.h>
#include "ParkLot.h"
#include <stdlib.h>


int main()
{
    Stack *park;
    Stack *tmp;
    Queue wait;
    RecordStack *record;
    int ret, n;
    elemtype intime, outtime, nt;

    if(InitStack(&park) != success || InitStack(&tmp) != success || InitQueue(&wait) != success || InitRecordStack(&record) != success)
    {
        printf("Init failed!\n\n");
    }
    else
    {
        printf("Init success\n\n");
    }


    printf("Please Set the Maxsize of ParkLots:\n");
    scanf("%d",&n);
    while(n < )
    {
        printf("Please Input Right Number!\n");
        scanf("%d",&n);
    }

    printf("Set Success!\n");
    sleep();
    system("clear");
    while()
    {
        printf("******************************************************************************\n\n");
        printf("***********************************WELCOME!***********************************\n\n");
        printf("******************************************************************************\n\n");
printf("***1.進車登記                                                2.出車登記*********\n");
printf("***3.按車牌号查詢資訊                                         4.查詢出入記錄*****\n");
printf("***5.查詢場内車輛資訊                                         6.查詢等候車輛資訊**\n");
printf("***7.退出系統                                                               ***\n");
        printf("******************************************************************************\n\n");
        printf("******************************************************************************\n\n");

        int choice;
        scanf("%d", &choice);

        if(choice >  || choice < )
        {
            printf("Error!\n");
            return ;
        }

        switch (choice)
        {
            case : 
                    system("clear");
                    printf("Please Input Incar's Licence Number:\n");
                    intime = time(NULL);
                    ret = CheckIn(park, &wait, intime, n);
                    if(waiting == ret)
                    {
                        printf("Parklot Full !Please wait!\n\n");
                    }
                    else if(success == ret)
                    {
                        printf("Register in Success!\n\n");
                    }
                    else
                    {
                        printf("Register in Failed!\n\n");
                    }

                    printf("The Left Carport is :%d\n\n",n - park->count);
                    break;
            case :
                    system("clear");
                    printf("Pleaese Input Outcar's Licence Number:\n");
                    outtime = time(NULL);
                    ret = CheckOut(park, tmp, record, outtime);
                    if(success == ret)
                    {
                        printf("Register out Success!\n\n");
                    }
                    else
                    {
                        printf("Register out Failed! The Car is not here!\n\n");
                        int j = tmp->count;
                        char name[];
                        while(j > )
                        {
                            LinkPark tmp1 = tmp->top;
                            tmp->top = tmp1->next;
                            tmp->count--;
                            tmp1->next = park->top;
                            park->top = tmp1;
                            park->count++;
                            j--;
                        }
                        sleep();
                        break;

                    }

                    intime = time(NULL);
                    ret = AutoIn(park, tmp, &wait, intime, n);

                    if(success == ret)
                    {
                        printf("WaitingCar has Parked Success!\n");
                    }

                    printf("The Left Carport is :%d\n\n",n - park->count);

                    break;
            case :
                    system("clear");
                    printf("Please Input the licence plate you want to search:\n");

                    ret = SearchInfo(record, park, &wait);
                    if(failure == ret)
                    {
                        printf("Search failed!This car haven't been here!\n\n");
                    }
                    else
                    {
                        printf("Search Success!\n\n");
                    }
                    break;
            case :
                    ret = SearchAll(record, park, &wait);
                    if(failure == ret)
                    {
                        printf("Search Failed!\n\n");
                    }
                    else
                    {
                        printf("Search All Information Success!\n\n");
                    }
                    break;
            case :
                    nt = time(NULL);
                    ret = SearchPark(park, nt);
                    if(failure == ret)
                    {
                        printf("Search Failed!\n\n");
                    }
                    else
                    {
                        printf("Search Parking Information Success!\n\n");
                    }
                    break;
            case :
                    ret = SearchWait(&wait);
                    if(failure == ret)
                    {
                        printf("Search Failed!\n\n");
                    }
                    else
                    {
                        printf("Search Waiting Information Success!\n\n");
                    }
                    break;
            case :
                    exit();
                    break;
            default:
                    printf("Error!\n");
                    break;
        }



    }

    return ;
}