iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言实现停车场项目
  • 407
分享到

C语言实现停车场项目

2024-04-02 19:04:59 407人浏览 独家记忆
摘要

本文实例为大家分享了C语言实现停车场项目的具体代码,供大家参考,具体内容如下 停车场项目需求 问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。

本文实例为大家分享了C语言实现停车场项目的具体代码,供大家参考,具体内容如下

停车场项目需求

问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。

基本功能要求:

(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)

功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、查询场内车辆信息、查询等候车辆信息、退出系统。

(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。

分析,底层需要写以下内容:

两个栈,停放栈(车牌号,出场时间,入场时间)[初始化,判断是否为空,出栈,入栈,查栈] 让路栈(车牌号)[初始化,出栈,入栈,判断是否为空]
一个队列,[初始化,出队列,入队列](车牌号)
一个链表,(车牌号,出入厂状态,入场时间,出场时间)[初始化,入链表,查找数据,遍历打印]

windows下代码

工程分为三个文件,分别是main.c fun.c pack.h

main.c:

#include<stdio.h>
#include"park.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>

void main() 
{
    int ret=0;
    char number[10];
    park_init();
    //所有数据结构初始化
    //存放用户输入的字符
    while(1) 
    {
        
        printf("##################          停车场  v1.0           ##################\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                                   #\n");
        printf("#           1-----------------进车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           2-----------------出车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           3-----------------车辆查询-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           4-----------------出入记录-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           5-----------------场内车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           6-----------------等候车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           7-----------------退出系统-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                  201808           #\n");
        printf("#####################################################################\n");
        gets( number );
        switch ( *number ) //选择需要什么功能 
        {
            case '1': //进车登记 
            {
                system("cls");
                ret=car_in();
                if(ret==FAILURE) 
                {
                    printf("输入错误!");
                }
                printf("入车成功!\n");
                getchar();
                system("cls");
            }
            break;
            case '2': //出车登记 
            {
                system("cls");
                ret=car_out();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                printf("出车成功!\n");
                getchar();
                system("cls");
            }
            break;
            case '3': //查找车辆 
            {
                system("cls");
                ret=find_car();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                //printf("-------------------------------------------------------------------------------------------------------------");
                getchar();
                system("cls");
            }
            break;
            case '4': //出入记录 
            {
                system("cls");
                ret=record_all();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                system("cls");
            }
            break;
            case '5': //场内车辆 
            {
                system("cls");
                ret=car_park();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                system("cls");
            }
            break;
            case '6': 
            {
                system("cls");
                //等候车辆
                ret=Car_wait();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                system("cls");
            }
            break;
            case '7':
                        printf("欢迎下次使用\n");
            break;
            default:
                            system("cls");
            printf( "操作错误,此项不存在!\n" );
            getchar();
            system("cls");
            break;
        }
        if ( strcmp( number, "7" ) == 0 )
                        break;
    }
}

park.h

#ifndef _PARK_H
#define _PAEK_H
#include <time.h>
#include <stdio.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
#define SIZE 6   //车场大小

struct car_info 
{
    char name[10];
    //  车牌
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
}
;
typedef struct car_info CAR_I;

struct sequencestack 
{
    int top;
    CAR_I *date;
}
;
typedef struct sequencestack ST;

struct car_wait 
{
    char name[10];
    struct car_wait *next;
}
;
typedef struct car_wait CAR_W;

struct linkqueue 
{
    CAR_W *front;
    CAR_W *rear;
}
;
typedef struct linkqueue LQ;

struct all_info 
{
    char name[10];
    char sit[10];
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
    struct all_info *next;
}
;
typedef struct all_info A_INFO;
int car_in();
int car_out();
int find_car();
int record_all();
int car_park();
int Car_wait();
int STinit(ST **q);
int STempty(ST *q);
int STinsert(ST *q, char na[] ,time_t time);
int STout(ST *q,char *a, time_t *time);
int LQinit(LQ **q);
int LQinsert(LQ *q, char na[]);
int LQout(LQ *q, char *a);
int LLinit(A_INFO **q);
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB);
int LLfind(A_INFO *q, char na[]);
int LLget(A_INFO *q, int n, A_INFO **k);
int LLtra(A_INFO *q);
int parkadd(CAR_I car[], char na[], time_t time);
int parkdel(CAR_I car[] ,char na[],time_t time);
int print(CAR_I car[]);
int park_init();
struct tm * local_time(time_t *tmpcal_ptr);
int exchange(A_INFO *q, char na[], char s[]);
#endif

fun.c

#include<stdio.h>
#include"park.h"
#include<stdlib.h> 
#include<time.h>
#include<string.h>
int num = 0;
//场内车辆数
ST *stack_park;
//停放栈
ST *stack_exchange;
//交换栈
LQ *queue_wait;
//等候队列
A_INFO  *all_car;
//存放所有信息的链表

int STinit(ST **q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    *q = (ST *)malloc(sizeof(ST)*1);
    //为结构体指针分配空间
    (*q)->top = -1;
    (*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE);
    //为内容指针分配空间
    return SUCCESS;
}

int STempty(ST *q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    return(q->top == -1)? TRUE:FALSE;
    //空返回true   不空返回false
}

int STinsert(ST *q, char na[] ,time_t time) 
{
    if(q == NULL || q->top >= SIZE-1) 
    {
        return FAILURE;
    }
    strcpy( q->date[q->top+1].name, na );
    q->date[q->top+1].time1 = time;
    q->top++;
    return SUCCESS;
}

int STout(ST *q,char *a, time_t *time) 
{
    if(q == NULL) 
    {
        return FAILURE;
        //错误返回failure
    }
    if(q->top == -1) 
    {
        return FALSE;
        //空返回false
    }
    strcpy(a, q->date[q->top].name);
    //a被修改为出场的车牌
    *time = q->date[q->top].time1;
    //time被修改为入场时间
    q->top--;
    return SUCCESS;
}

int LQinit(LQ **q) 
{
    CAR_W (*p);
    (*q) = (LQ *)malloc(sizeof(LQ));
    if( (*q) == NULL ) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if(p == NULL) 
    {
        return FAILURE;
    }
    p->next = NULL;
    (*q)->front = (*q)->rear =p;
    return SUCCESS;
}

int LQinsert(LQ *q, char na[]) 
{
    CAR_W *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if (NULL == p) 
    {
        return FAILURE;
    }
    strcpy(p->name, na);
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
    return SUCCESS;
}

int LQout(LQ *q, char *a) 
{
    //  char na[10];
    CAR_W *p = q->front->next;
    if (NULL == q ) 
    {
        return FAILURE;
    }
    if(q->rear == q->front) 
    {
        return FALSE;
    }
    strcpy(a, p->name);
    q->front->next = p->next;
    if (NULL == p->next) 
    {
        q->rear = q->front;
        //用参数a保存出去的车牌
    }
    free(p);
    return SUCCESS;
}

int LLinit(A_INFO **q) 
{
    (*q) = (A_INFO *)malloc(sizeof(A_INFO));
    if( (*q) == NULL) 
    {
        return FAILURE;
    }
    (*q)->next = NULL;
    return SUCCESS;
}

int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB) 
{
    A_INFO *l;
    A_INFO *p = q;
    int k = 1;
    if (NULL == q) 
    {
        return FAILURE;
    }
    while(k < n && p != NULL) 
    {
        p=p->next;
        k++;
    }
    if(k > n || p == NULL) 
    {
        return FAILURE;
    }
    l = (A_INFO *)malloc(sizeof(A_INFO)*1);
    if (NULL == l) 
    {
        return FAILURE;
    }
    strcpy(l->name, na);
    strcpy(l->sit ,s);
    l->time1 = timeA;
    l->time2 = timeB;
    l->next = p->next;
    p->next = l;
    return SUCCESS;
}

int LLfind(A_INFO *q, char na[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            return len;
            //找到返回位置
        }
        p = p->next;
        len++;
    }
    return FALSE;
    //未找到返回false
}

int LLget(A_INFO *q, int n, A_INFO **k) 
{
    int i;
    A_INFO *p = q;
    if (NULL == q) 
    {
        return FAILURE;
    }
    for (i = 0; i < n && p != NULL ; i++) 
    {
        p = p->next;
    }
    if(!p) 
    {
        return FAILURE;
    }
    (*k) = p;
    //用k指针保存找到的结构体地址
    return SUCCESS;
}

int LLtra(A_INFO *q) 
{
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow;
    A_INFO *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q;
    while (p->next) 
    {
        p = p->next;
        ////////////////////记得改这里
        time1 = local_time(&p->time1);
        if(time1 != NULL) 
        {
            printf("车牌:%s   状态:%s   入场时间:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
            time2 = local_time(&p->time2);
            if(time2 != NULL) 
            {
                cost = difftime(p->time2,p->time1);
                printf("  出场时间:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec);
                printf("  停车时间:%f秒\n",cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
            } else 
            {
                time(&timenow);
                cost = difftime(timenow,p->time1);
                printf("  停车时间为:%f秒\n" ,cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
                //return 0;
            }
        } else 
        {
            printf("车牌:%s  排队中\n",p->name);
            printf("-------------------------------------------------------------------------------------------------------------\n");
            //  return 0;
        }

    }
    return SUCCESS;
}

int parkadd(CAR_I car[], char na[], time_t time) 
{
    if(num>=6) 
    {
        return FAILURE;
    }
    strcpy(car[num].name, na);
    car[num].time1 = time;
    num++;
    return SUCCESS;
}

int parkdel(CAR_I car[] ,char na[],time_t time) 
{
    int i;
    if(num == 0) 
    {
        return FAILURE;
    }
    for (i = 0; i < num; i++) 
    {
        if(strcmp(car[i].name, na)==0) 
        {
            for (; i<num; i++) 
            {
                car[i] = car[i+1];
            }
            break;
        }
    }
    num--;
    return SUCCESS;
}

int print(CAR_I car[]) 
{
    int i;
    for (i = 0; i<num; i++) 
    {
        printf("场内车辆信息");
    }
    return SUCCESS;
}

int exchange(A_INFO *q, char na[], char s[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            strcpy( p->sit, s ) ;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}

int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time2 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}

int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time1 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}

int car_in() 
{
    int i;
    char a[12];
    //记录车牌号
    int ret;
    time_t tmpcal_ptr;
    time_t tmpcal_ptr1;
    time_t tmpcal_ptr2;
    //为了给出车时间赋空值
    printf("请输入车牌号:\n");
    gets(a);
    time(&tmpcal_ptr);
    ret=STinsert(stack_park, a ,tmpcal_ptr);
    if(ret==FAILURE) 
    {
        printf("停车场已满,排队中\n");
        ret=LQinsert(queue_wait, a);
        LLinsert(all_car, 1, a, "排队中",tmpcal_ptr1, tmpcal_ptr2);
        // exchange(all_car, a, "排队中");
        if(FAILURE==ret) 
        {
            return FAILURE;
        }
    } else 
    {
        LLinsert(all_car, 1, a, "在场中",tmpcal_ptr, tmpcal_ptr2);
    }
    for (i=0; i<stack_park->top+1; i++) 
    {
        printf("车牌 :%s\n",stack_park->date[i].name);
    }
    return SUCCESS;
}

int car_out() 
{
    char a[12];
    char b[12];
    char c[12];
    int ret;
    time_t tmpcal_ptr3;
    //出场时间
    time_t tmpcal_ptr4;
    //
    printf("\n请输入出车车牌号:\n\n");
    gets(a);
    time(&tmpcal_ptr3);
    //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    //  while(strcmp(b,a)!=0)
    while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE) 
    {
        if(strcmp(b,a)==0) 
        {
            break;
        }
        STinsert(stack_exchange, b, tmpcal_ptr4);
        //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    }
    if(ret == FALSE) 
    {
        printf("未找到该车!\n\n");
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        return FAILURE;
    } else 
    {
        ret = exchange(all_car , b ,"出场了");
        ret = exchange1(all_car ,b ,tmpcal_ptr3);
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        if((ret = LQout(queue_wait, c))!=FALSE) 
        {
            time(&tmpcal_ptr4);
            STinsert(stack_park, c,tmpcal_ptr4);
            ret = exchange(all_car , c ,"在场中");
            ret = exchange2(all_car ,c ,tmpcal_ptr3);
        }
    }
    return SUCCESS;
}

int find_car() 
{
    int len;
    char a[12];
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow;
    A_INFO *k;
    int ret;
    printf("请输入你要查询的车牌:\n");
    gets(a);
    len = LLfind(all_car, a);
    if(len == FALSE) 
    {
        printf("未来过\n\n");
        return FAILURE;
    }
    ret = LLget(all_car, len, &k);
    time1 = local_time(&k->time1);
    if(time1 != NULL)
    {
    printf("车牌:%s\n状态:%s\n入场时间:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
    time2 = local_time(&k->time2);
    if(time2 != NULL) 
    {
        cost = difftime(k->time2,k->time1);
        printf("出场时间:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec);
        printf("停车时间:%f\n秒",cost);
    } else 
    {
        time(&timenow);
        cost = difftime(timenow,k->time1);
        printf("出场时间:未出场\n停车时间:%f秒\n",cost);
        return 0;
    }
    }
    else
        printf("等候中");
    return SUCCESS;
}////////////////////////
查看出入登记函数
功能:遍历打印存放所有信息的链表
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
查看停车场内车辆信息函数
功能:遍历打印存放停车场内信息的数组
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
查看等候队列内车辆信息函数
功能:遍历打印等候队列中车辆
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
初始化函数
功能:初始化
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
时间函数
功能:转换成当地时间
入参:time_t *tmpcal_ptr(等待转换的时间)
返回值:time_local(当地时间)
///////////////////////////////////
void main() 
{
    int ret=0;
    char number[10];
    park_init();
    //所有数据结构初始化
    //存放用户输入的字符
    while(1) 
    {
        
        printf("##################          停车场  v1.0           ##################\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                                   #\n");
        printf("#           1-----------------进车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           2-----------------出车登记-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           3-----------------车辆查询-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           4-----------------出入记录-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           5-----------------场内车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           6-----------------等候车辆-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#           7-----------------退出系统-------------------           #\n");
        printf("#                                                                   #\n");
        printf("#####################################################################\n");
        printf("#                                                  201808           #\n");
        printf("#####################################################################\n");
        gets( number );
        switch ( *number ) //选择需要什么功能 
        {
            case '1': //进车登记 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=car_in();
                if(ret==FAILURE) 
                {
                    printf("输入错误!");
                }
                printf("入车成功!\n");
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '2': //出车登记 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=car_out();
                if(ret!=FAILURE) 
                {
                    printf("出车成功!\n");  
                }

                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '3': //查找车辆 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=find_car();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                //printf("-------------------------------------------------------------------------------------------------------------");
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '4': //出入记录 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=record_all();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '5': //场内车辆 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                ret=car_park();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '6': 
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                //等候车辆
                ret=Car_wait();
                if(ret==FAILURE) 
                {
                    printf("输入错误!\n");
                }
                getchar();
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
            break;
            case '7':
                        printf("欢迎下次使用\n");
            break;
            default:
                            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            printf( "操作错误,此项不存在!\n" );
            getchar();
            printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            break;
        }
        if ( strcmp( number, "7" ) == 0 )
                        break;
    }
}

fun_u.c

#include<stdio.h>
#include"park_u.h"
#include<stdlib.h> 
#include<time.h>
#include<string.h>
int num = 0;
//场内车辆数
ST *stack_park;
//停放栈
ST *stack_exchange;
//交换栈
LQ *queue_wait;
//等候队列
A_INFO  *all_car;
//存放所有信息的链表

int STinit(ST **q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    *q = (ST *)malloc(sizeof(ST)*1);
    //为结构体指针分配空间
    (*q)->top = -1;
    (*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE);
    //为内容指针分配空间
    return SUCCESS;
}

int STempty(ST *q) 
{
    if(q == NULL) 
    {
        return FAILURE;
    }
    return(q->top == -1)? TRUE:FALSE;
    //空返回true   不空返回false
}

int STinsert(ST *q, char na[] ,time_t time) 
{
    if(q == NULL || q->top >= SIZE-1) 
    {
        return FAILURE;
    }
    strcpy( q->date[q->top+1].name, na );
    q->date[q->top+1].time1 = time;
    q->top++;
    return SUCCESS;
}

int STout(ST *q,char *a, time_t *time) 
{
    if(q == NULL) 
    {
        return FAILURE;
        //错误返回failure
    }
    if(q->top == -1) 
    {
        return FALSE;
        //空返回false
    }
    strcpy(a, q->date[q->top].name);
    //a被修改为出场的车牌
    *time = q->date[q->top].time1;
    //time被修改为入场时间
    q->top--;
    return SUCCESS;
}

int LQinit(LQ **q) 
{
    CAR_W (*p);
    (*q) = (LQ *)malloc(sizeof(LQ));
    if( (*q) == NULL ) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if(p == NULL) 
    {
        return FAILURE;
    }
    p->next = NULL;
    (*q)->front = (*q)->rear =p;
    return SUCCESS;
}

int LQinsert(LQ *q, char na[]) 
{
    CAR_W *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = (CAR_W *)malloc(sizeof(CAR_W));
    if (NULL == p) 
    {
        return FAILURE;
    }
    strcpy(p->name, na);
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
    return SUCCESS;
}

int LQout(LQ *q, char *a) 
{
    //  char na[10];
    CAR_W *p = q->front->next;
    if (NULL == q ) 
    {
        return FAILURE;
    }
    if(q->rear == q->front) 
    {
        return FALSE;
    }
    strcpy(a, p->name);
    q->front->next = p->next;
    if (NULL == p->next) 
    {
        q->rear = q->front;
        //用参数a保存出去的车牌
    }
    free(p);
    return SUCCESS;
}

int LLinit(A_INFO **q) 
{
    (*q) = (A_INFO *)malloc(sizeof(A_INFO));
    if( (*q) == NULL) 
    {
        return FAILURE;
    }
    (*q)->next = NULL;
    return SUCCESS;
}

int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB) 
{
    A_INFO *l;
    A_INFO *p = q;
    int k = 1;
    if (NULL == q) 
    {
        return FAILURE;
    }
    while(k < n && p != NULL) 
    {
        p=p->next;
        k++;
    }
    if(k > n || p == NULL) 
    {
        return FAILURE;
    }
    l = (A_INFO *)malloc(sizeof(A_INFO)*1);
    if (NULL == l) 
    {
        return FAILURE;
    }
    strcpy(l->name, na);
    strcpy(l->sit ,s);
    l->time1 = timeA;
    l->time2 = timeB;
    l->next = p->next;
    p->next = l;
    return SUCCESS;
}

int LLfind(A_INFO *q, char na[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            return len;
            //找到返回位置
        }
        p = p->next;
        len++;
    }
    return FALSE;
    //未找到返回false
}

int LLget(A_INFO *q, int n, A_INFO **k) 
{
    int i;
    A_INFO *p = q;
    if (NULL == q) 
    {
        return FAILURE;
    }
    for (i = 0; i < n && p != NULL ; i++) 
    {
        p = p->next;
    }
    if(!p) 
    {
        return FAILURE;
    }
    (*k) = p;
    //用k指针保存找到的结构体地址
    return SUCCESS;
}

int LLtra(A_INFO *q) 
{
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow=0;
    A_INFO *p;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q;
    while (p->next) 
    {
        p = p->next;
        time1 = local_time(&p->time1);
        if(time1 != NULL) 
        {
            printf("车牌:%s   状态:%s   入场时间:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
            time2 = local_time(&p->time2);
            if(!(time2->tm_hour==8&&time2->tm_min==0&&time2->tm_sec==0)) 
            {
                cost = difftime(p->time2,p->time1);
                printf("  出场时间:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec);
                printf("  停车时间:%f秒\n",cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
            } else 
            {
                time(&timenow);
                cost = difftime(timenow,p->time1);
                printf("  停车时间为:%f秒\n" ,cost);
                printf("-------------------------------------------------------------------------------------------------------------\n");
                //return 0;
            }
        } else 
        {
            printf("车牌:%s  排队中\n",p->name);
            printf("-------------------------------------------------------------------------------------------------------------\n");
            //  return 0;
        }

    }
    return SUCCESS;
}

int parkadd(CAR_I car[], char na[], time_t time) 
{
    if(num>=6) 
    {
        return FAILURE;
    }
    strcpy(car[num].name, na);
    car[num].time1 = time;
    num++;
    return SUCCESS;
}

int parkdel(CAR_I car[] ,char na[],time_t time) 
{
    int i;
    if(num == 0) 
    {
        return FAILURE;
    }
    for (i = 0; i < num; i++) 
    {
        if(strcmp(car[i].name, na)==0) 
        {
            for (; i<num; i++) 
            {
                car[i] = car[i+1];
            }
            break;
        }
    }
    num--;
    return SUCCESS;
}

int print(CAR_I car[]) 
{
    int i;
    for (i = 0; i<num; i++) 
    {
        printf("场内车辆信息");
    }
    return SUCCESS;
}

int exchange(A_INFO *q, char na[], char s[]) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            strcpy( p->sit, s ) ;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}

int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time2 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}

int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr) 
{
    A_INFO *p;
    int len;
    if (NULL == q) 
    {
        return FAILURE;
    }
    p = q->next;
    len = 1;
    while(p) 
    {
        if(strcmp(p->name, na) == 0) 
        {
            p->time1 = tmpcal_ptr;
            return SUCCESS;
        }
        p = p->next;
        len++;
    }
    return FALSE;
}

int car_in() 
{
    int i;
    char a[12];
    //记录车牌号
    int ret;
    time_t tmpcal_ptr;
    time_t tmpcal_ptr1;
    time_t tmpcal_ptr2;
    //为了给出车时间赋空值
    printf("请输入车牌号:\n");
    gets(a);
    time(&tmpcal_ptr);
    ret=STinsert(stack_park, a ,tmpcal_ptr);
    if(ret==FAILURE) 
    {
        printf("停车场已满,排队中\n");
        ret=LQinsert(queue_wait, a);
        LLinsert(all_car, 1, a, "排队中",tmpcal_ptr1, tmpcal_ptr2);
        // exchange(all_car, a, "排队中");
        if(FAILURE==ret) 
        {
            return FAILURE;
        }
    } else 
    {
        LLinsert(all_car, 1, a, "在场中",tmpcal_ptr, tmpcal_ptr2);
    }
    for (i=0; i<stack_park->top+1; i++) 
    {
        printf("车牌 :%s\n",stack_park->date[i].name);
    }
    return SUCCESS;
}

int car_out() 
{
    char a[12];
    char b[12];
    char c[12];
    int ret;
    time_t tmpcal_ptr3;
    //出场时间
    time_t tmpcal_ptr4;
    //
    printf("\n请输入出车车牌号:\n\n");
    gets(a);
    time(&tmpcal_ptr3);
    //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    //  while(strcmp(b,a)!=0)
    while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE) 
    {
        if(strcmp(b,a)==0) 
        {
            break;
        }
        STinsert(stack_exchange, b, tmpcal_ptr4);
        //  ret =STout(stack_park, b, &tmpcal_ptr4 );
    }
    if(ret == FALSE) 
    {
        printf("未找到该车!\n\n");
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        return FAILURE;
    } else 
    {
        ret = exchange(all_car , b ,"出场了");
        ret = exchange1(all_car ,b ,tmpcal_ptr3);
        while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE) 
        {
            STinsert(stack_park, b,tmpcal_ptr4 );
        }
        if((ret = LQout(queue_wait, c))!=FALSE) 
        {
            time(&tmpcal_ptr4);
            STinsert(stack_park, c,tmpcal_ptr4);
            ret = exchange(all_car , c ,"在场中");
            ret = exchange2(all_car ,c ,tmpcal_ptr3);
        }
    }
    return SUCCESS;
}

int find_car() 
{
    int len;
    char a[12];
    double cost;
    struct tm *time1;
    struct tm *time2;
    time_t timenow;
    A_INFO *k;
    int ret;
    printf("请输入你要查询的车牌:\n");
    gets(a);
    len = LLfind(all_car, a);
    if(len == FALSE) 
    {
        printf("未来过\n\n");
        return FAILURE;
    }
    ret = LLget(all_car, len, &k);
    time1 = local_time(&k->time1);
    if(time1 != NULL)
    {
    printf("车牌:%s\n状态:%s\n入场时间:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
    time2 = local_time(&k->time2);
    if(time2 != NULL) 
    {
        cost = difftime(k->time2,k->time1);
        printf("出场时间:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec);
        printf("停车时间:%f\n秒",cost);
    } else 
    {
        time(&timenow);
        cost = difftime(timenow,k->time1);
        printf("出场时间:未出场\n停车时间:%f秒\n",cost);
        return 0;
    }
    }
    else
        printf("等候中");
    return SUCCESS;
}


int record_all() 
{
    LLtra(all_car);
    return SUCCESS;
}

int car_park() 
{
    int i;
    double cost;
    struct tm *time1;
    time_t timenow;
    for (i=0; i<stack_park->top+1; i++) 
    {
        time1 = local_time(&stack_park->date[i].time1);
        printf("车牌:%s     入场时间:%d:%d:%d",stack_park->date[i].name, time1->tm_hour, time1->tm_min, time1->tm_sec);
        time(&timenow);
        cost = difftime(timenow,stack_park->date[i].time1);
        printf("  未出场,停车时间为:%f秒\n\n",cost);
        printf("-------------------------------------------------------------------------------------------------------------\n");
    }
    return SUCCESS;
}

int Car_wait() 
{
    CAR_W *p;
    p = queue_wait->front;
    while(p->next) 
    {
        p=p->next;
        printf("车牌:%s\n\n",p->name);
    }
    return SUCCESS;
}

int park_init() 
{
    STinit(&stack_park);
    STinit(&stack_exchange);
    LQinit(&queue_wait);
    LLinit(&all_car);
    return SUCCESS;
}

struct tm * local_time(time_t *tmpcal_ptr) 
{
    struct tm *time_local = NULL;
    time_local = localtime(tmpcal_ptr);
    //转换成当地时间
    return time_local;
}

park_u.h

#ifndef _PARK_H
#define _PAEK_H
#include <time.h>
#include <stdio.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
#define SIZE 6   //车场大小

struct car_info 
{
    char name[10];
    //  车牌
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
}
;
typedef struct car_info CAR_I;

struct sequencestack 
{
    int top;
    CAR_I *date;
}
;
typedef struct sequencestack ST;

struct car_wait 
{
    char name[10];
    struct car_wait *next;
}
;
typedef struct car_wait CAR_W;

struct linkqueue 
{
    CAR_W *front;
    CAR_W *rear;
}
;
typedef struct linkqueue LQ;

struct all_info 
{
    char name[10];
    char sit[10];
    time_t time1;
    //  入场时间
    time_t time2;
    //  出场时间
    struct all_info *next;
}
;
typedef struct all_info A_INFO;
int car_in();
int car_out();
int find_car();
int record_all();
int car_park();
int Car_wait();
int STinit(ST **q);
int STempty(ST *q);
int STinsert(ST *q, char na[] ,time_t time);
int STout(ST *q,char *a, time_t *time);
int LQinit(LQ **q);
int LQinsert(LQ *q, char na[]);
int LQout(LQ *q, char *a);
int LLinit(A_INFO **q);
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB);
int LLfind(A_INFO *q, char na[]);
int LLget(A_INFO *q, int n, A_INFO **k);
int LLtra(A_INFO *q);
int parkadd(CAR_I car[], char na[], time_t time);
int parkdel(CAR_I car[] ,char na[],time_t time);
int print(CAR_I car[]);
int park_init();
struct tm * local_time(time_t *tmpcal_ptr);
int exchange(A_INFO *q, char na[], char s[]);
#endif

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: C语言实现停车场项目

本文链接: https://www.lsjlt.com/news/142578.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C语言实现停车场项目
    本文实例为大家分享了C语言实现停车场项目的具体代码,供大家参考,具体内容如下 停车场项目需求 问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。...
    99+
    2024-04-02
  • C语言怎么实现停车场管理
    这篇文章主要介绍了C语言怎么实现停车场管理的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇C语言怎么实现停车场管理文章都会有所收获,下面我们一起来看看吧。1.问题描述停车场内只有一个可停放n辆汽车的狭长通道,且只...
    99+
    2023-06-29
  • C语言实现简易停车场管理系统
    本文实例为大家分享了C语言实现停车场管理系统的具体代码,供大家参考,具体内容如下 问题描述: 设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按...
    99+
    2024-04-02
  • C语言实现简单的停车场管理系统
    本文实例为大家分享了C语言实现简单停车场管理系统的具体代码,供大家参考,具体内容如下 1.问题描述 1)有一个两层的停车场,每层有6个车位,当第一层车位停满后才允 许使用第二层(停车...
    99+
    2024-04-02
  • OpenCV停车场车位实时检测项目实践
    目录1. 写在前面2. 整体流程梳理3. 数据预处理3.1 背景过滤3.2 Canny边缘检测3.3 停车场区域提取3.4 霍夫变换检测直线3.5 以列为单位,划分停车位3.6 锁定...
    99+
    2024-04-02
  • OpenCV停车场车位实时检测项目分析
    这篇“OpenCV停车场车位实时检测项目分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“OpenCV停车场车位实时检测项目...
    99+
    2023-06-29
  • 怎么用C语言实现简易停车场管理系统
    本篇内容主要讲解“怎么用C语言实现简易停车场管理系统”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用C语言实现简易停车场管理系统”吧!问题描述:设停车场内只有一个可停放n辆汽车的狭长通道,且...
    99+
    2023-06-29
  • C语言实现停车管理系统
    本题为大家分享了C语言实现停车管理系统的具体代码,供大家参考,具体内容如下 设计题目 设停车场是一个可以停放n辆汽车的南北方向的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按...
    99+
    2024-04-02
  • 如何用C语言实现简单的停车场管理系统
    本篇内容主要讲解“如何用C语言实现简单的停车场管理系统”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何用C语言实现简单的停车场管理系统”吧!1.问题描述1)有一个两层的停车场,每层有6个车位,...
    99+
    2023-06-29
  • C++如何实现停车场管理系统
    本文小编为大家详细介绍“C++如何实现停车场管理系统”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++如何实现停车场管理系统”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。一、案例需求描述停车场管理系统就是模拟...
    99+
    2023-07-06
  • C语言课程设计之停车场管理问题
    C语言课程设计之停车场管理问题,供大家参考,具体内容如下 1.问题描述 停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,...
    99+
    2024-04-02
  • C语言实现扫雷项目
    本文实例为大家分享了C语言实现扫雷项目的具体代码,供大家参考,具体内容如下 核心思想就是,雷的分布看作是“答案”,落子的棋盘看作是“考卷”; 答卷的同时,需要去对一下“答案”,这个位...
    99+
    2024-04-02
  • python实现停车场管理系统
    目录一、需求二、代码2.1 初始化模块__init__.py2.2 主程序模块main.py2.3 停车管理模块ParkingManagementSystem.py2.4 车元素模块...
    99+
    2024-04-02
  • C语言实现扫雷小项目
    本文实例为大家分享了C语言实现扫雷小项目的具体代码,供大家参考,具体内容如下 游戏的基本设计流程如下: 菜单实现: void menu() { printf("#######...
    99+
    2024-04-02
  • C/C++利用栈和队列实现停车场管理系统
    目录纯c语言版包含的功能运行效果源码c++版包含的功能运行效果源码纯c语言版 包含的功能 1、停车功能 如果停车场满,能够暂时存放到便道内 2、开走车功能 将指定车开走后打印收据,便...
    99+
    2024-04-02
  • C++实现停车场管理系统的示例代码
    目录一、案例需求描述1.1、汽车信息模块1.2、普通用户模块1.3、管理员用户模块二、案例分析三、案例代码实现3.1、汽车信息类及方法实现3.2、普通用户类及方法实现3.3、管理员用...
    99+
    2023-05-15
    C++实现停车场管理系统 C++停车场管理系统 C++管理系统
  • C语言实现停车管理系统的代码怎么写
    本篇内容主要讲解“C语言实现停车管理系统的代码怎么写”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C语言实现停车管理系统的代码怎么写”吧!设计题目设停车场是一个可以停放n辆汽车的南北方向的狭长通...
    99+
    2023-06-29
  • C语言版停车位管理系统
    本文实例为大家分享了C语言实现停车位管理系统的具体代码,供大家参考,具体内容如下 简单功能介绍 1、录入车辆信息2、查找车辆信息3、删除车辆信息4、修改车辆信息5、查找区域车辆信息6...
    99+
    2024-04-02
  • C语言实现通讯录小项目
    本文实例为大家分享了C语言实现通讯录小项目的具体代码,供大家参考,具体内容如下 编写程序实现通讯录的基本功能,可以做到增,删,查,改,打印通讯录,等等功能 test.c #inc...
    99+
    2024-04-02
  • C语言实现猜数字小项目
    学习完c语言循环、选择内容,我们对c语言有个初步的认识了,让我们来实现一个小项目吧!!! 1.猜数字小项目分析: 我们利用c语言制造出随机数来猜,在只知道范围的前提下我们无非有以下三...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作