用两栈和两队列实现简易停车场管理系统

来源:互联网 发布:java常见的接口 编辑:程序博客网 时间:2024/04/29 22:28

问题描述:
停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车 要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、查询场内车辆信息、查询等候车辆信息、退出系统。
(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。

在出车程序中,此程序较为凝练。
对于离开的车辆,其实也完全可以采用链表方式存储数据哦;

学习要点:时间函数
time_t t;
struct tm *pt;
time(&t);
pt = localtime(&t);

Linux 终端彩色代码
这里写图片描述

#ifndef _PARKING_H_#define _PARKING_H_#include  <time.h>#include <stdlib.h>#include <string.h>#define SUCCESS     1000001#define FAILURE     1000002#define MAXSIZE     5
struct node{    int year,month,day,hour,min,sec;    char id[10];    struct node *next;};typedef struct node Node;struct park{    Node *top;    int count;};typedef struct park Park;struct queue{    Node *front;    Node *rear;};typedef struct queue Queue;
void show();void PrintInfo();int InitStack(Park *s);int InitQueue(Queue *s);int car_in(Park *s,Queue *e1,Queue *e2);int car_leave(Park *s1,Park *s2,Queue *e1,Queue *e2);int look_in_car(Park *s1);int look_wait_car(Queue *e);int see_record(Queue *e2);int search_car(Queue *e2,Park *s1);#endif
#include <stdio.h>#include "Parking.h"int InitStack(Park *s){//  s = (Park *)malloc(sizeof(Park));    s->top = NULL;    s->count = 0;//  printf("%d\n",s->count);    return SUCCESS;}int InitQueue(Queue *s){    s->front = (Node *)malloc(sizeof(Node));    s->rear = s->front;    if(NULL == s->rear)        return FAILURE;    else        return SUCCESS;}
void show(){    printf("\033[0;34m%s\033[0m","*****************************************************\n");    printf("*************WELCOME TO PARKING SYSTEM***************\n");    printf("\033[0;34m%s\033[0m","*****************************************************\n");    sleep(1);    system("clear");}void PrintInfo(){    printf("\033[0;35m%s\033[0m","*****************************************************\n");    printf("\033[0;35;1m%s\033[0m","*********1.进车登记           2.出车登记*************\n");    printf("\033[0;35;1m%s\033[0m","*********3.查询车辆信息       4.查询出入记录*********\n");    printf("\033[0;35;1m%s\033[0m","*********5.查询场内车辆信息   6.查询等候车辆信息*****\n");    printf("\033[0;35;1m%s\033[0m","*********7.退出                                  ****\n");    printf("\033[0;35m%s\033[0m","*****************************************************\n");    printf("\033[0;31m%s\033[0m","Please input your choice:\n");}
int Copy(Node *p,struct tm *it)//复制时间{    p->year = it->tm_year+1900;    p->month = it->tm_mon+1;    p->day = it->tm_mday;    p->hour = it->tm_hour;    p->min = it->tm_min;    p->sec = it->tm_sec;}
int car_in(Park *s,Queue *e1,Queue *e2){    time_t t;//生成时间    struct tm *it;    time (&t);    it = localtime(&t);//  printf("%d\n",s->count);    if(s->count < MAXSIZE)    {        Node *p=(Node *)malloc(sizeof(Node));        Node *e=(Node *)malloc(sizeof(Node));        if(p == NULL || e == NULL)            return FAILURE;        printf("Please input the number \n");        scanf("%s",p->id);        Copy(p,it);        p->next = s->top;        s->top = p;        s->count++;        strcpy(e->id,p->id);//记录队列工作        Copy(e,it);        e->next = NULL;          e2->rear->next = e;        e2->rear = e;        return SUCCESS;    }    else    {        printf("车位已满,进入等候队列\n");//      if(e1->rear == NULL)//          return FAILURE;        Node *p=(Node *)malloc(sizeof(Node));        if(p == NULL )            return FAILURE;        printf("Please input the number \n");        scanf("%s",p->id);//进入等候队列,记录车牌 不记录时间,不进入记录队列        p->next = NULL ;        e1->rear->next = p;        e1->rear = p;        return SUCCESS;    }}
int car_leave(Park *s1,Park *s2,Queue *e1,Queue *e2){    time_t t;    struct tm *it;    time (&t);    it = localtime(&t);    char d[10];    Node *n=s1->top;    Node *tem = NULL;//    printf("Please input your number:\n");    scanf("%s",d);    while(n != NULL)    {        if(strcmp(d,n->id) == 0)        {            printf("停留时间:%d年/%d月/%d日 %d时%d分%d秒\n",it->tm_year+1900-n->year,it->tm_mon+1-n->month,it->tm_mday-n->day,it->tm_hour-n->hour,it->tm_min-n->min,it->tm_sec-n->sec);            while(s1->top != n)//目标前面的车先进入让路栈            {                tem = s1->top->next;                s1->top->next = s2->top;                s2->top = s1->top;                s1->top = tem;                s1->count--;                s2->count++;            }            s1->top = n->next;//目标车出库            s1->count--;            while(s2->top != NULL)//让路栈的车入库            {                tem = s2->top->next;//关键点                s2->top->next = s1->top;                s1->top = s2->top;                s2->top = tem;                s1->count++;                s2->count--;            }            Node *e=(Node *)malloc(sizeof(Node));//记录队列工作            strcpy(e->id,n->id);            Copy(e,it);//shi jian qu qiao      ying gai strcpy(e->year, n->yrear)...            e->next = NULL;            e2->rear->next = e;            e2->rear = e;            free(n);            if(e1->front)            {                Node *p = e1->front->next;//等候队列的车进入停车场                e1->front->next = p->next;                p->next = s1->top;                s1->top = p;                s1->count++;                Copy(p,it);                Node *e=(Node *)malloc(sizeof(Node));//记录队列工作                strcpy(e->id,p->id);                Copy(e,it);                e->next = NULL;                  e2->rear->next = e;                e2->rear = e;            //  p=e1->front->next;                if(p == NULL)                {                    e1->rear = e1->front;                }            }            return SUCCESS;        }        n=n->next;    }    printf("No Find the number!\n");}
int look_in_car(Park *s1){    Node *n=s1->top;    if(n == NULL)        return FAILURE;    while(n)    {        printf("%s 进入时间:%d年/%d月/%d%d%d%d秒\n",n->id,n->year,n->month,n->day,n->hour,n->min,n->sec);        n=n->next;    }    return SUCCESS;}
int look_wait_car(Queue *e){    Node *n=e->front->next;    if(n == NULL)        return FAILURE;    int length=0;    while(n)    {        printf("车牌号:%s\n",n->id);        n=n->next;        length++;    }    printf("等候车辆数量:%d\n",length);    return SUCCESS;}
int see_record(Queue *e2){    Node *n = e2->front->next;    if(n == NULL)        return FAILURE;    while(n)    {        printf("车牌:%s 时间:%d/%d/%d %d:%d:%d\n",n->id,n->year,n->month,n->day,n->hour,n->min,n->sec);        n = n->next;    }    return SUCCESS;}
int search_car(Queue *e2,Park *s1){    char p[10];    Node *e=e2->front->next;    Node *s=s1->top;    printf("Please input your number:\n");    scanf("%s",p);    while(e)    {        if(strcmp(e->id,p) == 0)        {            printf("这辆车来过停车场\n");            printf("停车时间:%d年/%d月/%d%d%d%d秒\n",e->year,e->month,e->day,e->hour,e->min,e->sec);            while(s)            {                if(strcmp(s->id,p) == 0)                {                    printf("这辆车还在停车场内\n");                    return SUCCESS;                }                s=s->next;            }            printf("这辆车已经离开停车场\n");            return SUCCESS;        }        e=e->next;    }    printf("这辆车没来过停车场\n");    return SUCCESS;}
#include <stdio.h>#include "Parking.h"int main(){    char choice[10];    int ret;    Park  sq1,sq2;    Queue se1,se2;    system("clear");    if(InitStack(&sq1) != SUCCESS || InitQueue(&se1) != SUCCESS || InitStack(&sq2) != SUCCESS || InitQueue(&se2) != SUCCESS )        printf("Init failure\n");    else        printf("Init success\n");    show();    //获取时间 //  time_t t;//  struct tm *it;//  time (&t);//获取 unix 时间//  it = localtime(&t);//转为时间结构    while(1)    {        PrintInfo();        scanf("%s",choice);        switch(atoi(&choice[0]))        {            case 1:                ret = car_in(&sq1,&se1,&se2);                if(ret == FAILURE)                    printf("car in FAILURE \n");                else                    printf("car in SUCCESS \n");                break;            case 2:                ret = car_leave(&sq1,&sq2,&se1,&se2);                if(ret == FAILURE)                    printf("car leave FAILURE \n");                if(ret == SUCCESS)                      printf("car leave SUCCESS \n");                break;            case 3:                ret = search_car(&se2,&sq1);                if(ret == SUCCESS)                    printf("search SUCCESS!\n");                break;            case 4:                ret = see_record(&se2);                if(ret == FAILURE)                    printf("see record FAILURE\n");                else                    printf("see record SUCCESS\n");                break;            case 5:                ret = look_in_car(&sq1);                if(ret == FAILURE)                    printf("no car!\n");                else                    printf("look within car success!\n");                break;            case 6:                ret = look_wait_car(&se1);                if(ret == FAILURE)                    printf("no car!\n");                else                    printf("look waiting car success!\n");                break;            case 7:                exit(1);            default :                printf("Unknow choice!\n");                break;        }    }    return 0;}
原创粉丝点击