C/C++课程设计:停车场管理系统,290行代码助你轻松管理停车场

Posted 一起学编程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C/C++课程设计:停车场管理系统,290行代码助你轻松管理停车场相关的知识,希望对你有一定的参考价值。

问题描述:

停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;

当停车场内某辆车要离开时,在它之后开入的车辆必须先退出车场为它让路,待该辆车开出大门外,其它车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。

1.基本要求

(1)以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。

(2)每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码及到达或离去的时刻,对每一组输入数据进行操作后的输出数据为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车离去;则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。

(3)栈以顺序结构实现,队列以链表实现。

2.重点难点

重点:针对停车场问题的特点,利用栈的后进先出特点,选择栈这种数据结构来模拟停车场,利用队列先进先出的特点,选择队列这种数据结构来模拟车场外的便道。

难点:离散事件问题的模拟算法设计与求解。

源码分享:

#include<windows.h>
#include<iomanip>
#include<conio.h>
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#define PARKING_MAXSIZE 4
#define lisence_length 10

using namespace std;

float price_hour;
float profit=0;

//汽车的信息
typedef struct

    float hour;
    float minute;
time;

typedef struct

    char lisence[lisence_length];
    time time_enter;
    time time_exit;
car_info;

/

typedef struct        //顺序栈
    car_info a[PARKING_MAXSIZE];
    int top;
SStack;

typedef struct Node

    car_info data;              // 元素数据
    struct Node *next;          // 链式队列中结点元素的指针
QNode, *QueuePtr;

typedef struct

    QueuePtr front;             // 队列头指针
    QueuePtr rear;              // 队列尾指针
LinkQueue;

SStack InitStack(SStack b)
    b.top=0;
    return b;


int NotEmpty(SStack *b)
    if(b->top==0)
        return 0;
        else return 1;


int IsFull(SStack *b)
    if(b->top==PARKING_MAXSIZE)
        return 1;
    else return 0;


car_info Pop(SStack *b,car_info e)
    if(NotEmpty(b))
        e=b->a[--b->top];
        return e;
    
    else cout<<"没有车辆进入!"<<endl;


void Push(SStack *b,car_info e)
    if(IsFull(b))
        cout<<"停车场无法容纳更多车辆!"<<endl;
    else
        b->a[b->top++]=e;


LinkQueue InitQueue(LinkQueue Q)

    Q.front=Q.rear=(QNode*)malloc(sizeof(QNode));
    Q.front->next=NULL;
    return Q;


int QueueEmpty(LinkQueue *Q)

    if(Q->front->next==NULL)
        return 1;
    else
        return 0;


void EnQueue(LinkQueue *Q,car_info e)

    QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
    p->data=e;
    p->next=NULL;
    Q->rear->next=p;
    Q->rear=p;


car_info DeQueue(LinkQueue *Q,car_info e)

    QueuePtr p;
    if(Q->front==Q->rear)
        cout<<"System Error"<<endl;
    p=Q->front->next;
    e=p->data;
    Q->front->next=p->next;
    if(Q->rear==p)
        Q->rear=Q->front;
    free(p);
    return e;


int QueueLength(LinkQueue *Q)
 /* 求队列的长度 */
    int i=0;
    QueuePtr p;
    p=Q->front;
    while(Q->rear!=p)
    
        i++;
        p=p->next;
    
    return i;


void Arrive(SStack *in,LinkQueue *wait)

    car_info e;
    cout<<"输入车牌号:";
    cin>>e.lisence;
    cout<<"输入到达时间(格式:时+空格键+分):";
    cin>>e.time_enter.hour>>e.time_enter.minute;
    if(!IsFull(in))
    
        Push(in,e);
        cout<<"车辆"<<e.lisence<<"在停车场的位置为车位"<<in->top<<endl;
    
    else
    
        EnQueue(wait,e);
        cout<<"车辆"<<e.lisence<<"在便道上的位置为车位"<<QueueLength(wait)<<endl;
    
    cout<<endl;


void Leave(SStack *in,SStack *out,LinkQueue *wait)

    char room[10];
    float time,park_price;
    car_info e,f;
    SStack S,*temp_stack;
    LinkQueue Q,*temp_queue;
    S=InitStack(S);temp_stack=&S;
    Q=InitQueue(Q);temp_queue=&Q;

    if(in->top>0)    //停车场有车
    
        cout<<"停车场:此时停车场中车的数量为"<<in->top<<endl;
        cout<<"输入要离开车的车牌号:";
        cin>>room;
        cout<<"输入离开时间(格式:时+空格键+分):";
        cin>>f.time_exit.hour>>f.time_exit.minute;
        while(in->top>=1&&(strcmp(in->a[in->top-1].lisence,room))!=0)
        
            e=Pop(in,e);
            Push(temp_stack,e);
        
        if(in->top>=1)
        
            e=Pop(in,e);
            time=f.time_exit.hour-e.time_enter.hour+(f.time_exit.minute-e.time_enter.minute)/60;
            park_price=time*price_hour;
            profit+=park_price;
            printf("\\n------------------------停车场收费明细----------------------------\\n");
            printf(" --------------------------车牌号:%s-------------------------\\n ",e.lisence);
            printf("|  进入时间  |  离开时间  |  停留时间  |   应收款  |\\n");
            printf("=======================================================================\\n");
            printf(" |    %.0f:%.0f     |    %.0f:%.0f    |   %.2f小时    |    %.2f元    |\\n"
                   ,e.time_enter.hour,e.time_enter.minute,f.time_exit.hour,f.time_exit.minute,time,park_price);
            printf("---------------------------------------------------------------------\\n");
        
        else cout<<"停车场中无此车。"<<endl;
        while(NotEmpty(temp_stack))
        
            e=Pop(temp_stack,e);
            Push(in,e);
        
     
    if(!QueueEmpty(wait))
    
        while((!QueueEmpty(wait)&&strcmp(wait->front->next->data.lisence,room))!=0)
        

            e=DeQueue(wait,e);
            EnQueue(temp_queue,e);
        
        if(!QueueEmpty(wait))
        
            e=DeQueue(wait,e);
            time=f.time_exit.hour-e.time_enter.hour+(f.time_exit.minute-e.time_enter.minute)/60;
            printf("车辆%s在便道停留的时间为%.2fh,所收停车费为0.\\n",e.lisence,time);
        
        while(!QueueEmpty(wait))
        
            e=DeQueue(wait,e);
            EnQueue(temp_queue,e);
        
        while(!QueueEmpty(temp_queue))
        
            e=DeQueue(temp_queue,e);
            EnQueue(wait,e);
        
    
    if(!IsFull(in)&&!QueueEmpty(wait))
        while(!IsFull(in)&&!QueueEmpty(wait))
        
            e=DeQueue(wait,e);
            e.time_enter=f.time_exit;
            Push(in,e);
        
        cout<<endl;


void Infomation(SStack *in,LinkQueue *wait)

    car_info e;
    SStack S,*P_S;
    LinkQueue Q,*P_Q;
    S=InitStack(S);P_S=&S;
    Q=InitQueue(Q);P_Q=&Q;
    printf("此时停车场内有%d辆车,便道上有%d辆车。\\n",in->top,QueueLength(wait));
    while(NotEmpty(in))
    
        e=Pop(in,e);
        Push(P_S,e);
        printf("车辆%s:进入停车场时间%.f:%.f\\n",e.lisence,e.time_enter.hour,e.time_enter.minute);
    
    printf("\\n");
    while(NotEmpty(P_S))
    
        e=Pop(P_S,e);
        Push(in,e);
    
    if(!QueueEmpty(wait))
    
        while(!QueueEmpty(wait))
        
            e=DeQueue(wait,e);
            EnQueue(P_Q,e);
            printf("车辆%s:进入便道时间%.f:%.f\\n",
                   e.lisence,e.time_enter.hour,e.time_enter.minute);
        
        printf("\\n");
        while(!QueueEmpty(P_Q))
        
            e=DeQueue(P_Q,e);
            EnQueue(wait,e);
        
    


int main()

    SStack in,out,*p_in=&in,*p_out=&out;
    LinkQueue wait,*p_wait;
    char ch;
    in=InitStack(in);out=InitStack(out);
    wait=InitQueue(wait);p_wait=&wait;
    cout<<"--------------------------停车场管理系统--------------------------"<<endl<<endl;
    cout<<"                      请输入停车场的收费标准:";
    scanf("%f",&price_hour);

    while(scanf("%c",&ch))
    switch(ch)
    
        case 'A': Arrive(p_in,p_wait);break;
        case 'L': Leave(p_in,p_out,p_wait);break;
        case 'I': Infomation(p_in,p_wait);break;
        case 'R': cout<<"输入修改后的价格:";cin>>price_hour;break;
        case 'E': cout<<"谢谢使用!"<<endl;exit(0);
        default:  continue;
    
    

运行截图:

写在最后:对于准备学习C/C++编程的小伙伴,如果你想更好的提升你的编程核心能力(内功)不妨从现在开始!

C语言C++编程学习交流圈子,QQ群:805343586点击进入】微信公众号:C语言编程学习基地

整理分享(多年学习的源码、项目实战视频、项目笔记,基础入门教程)

欢迎转行和学习编程的伙伴,利用更多的资料学习成长比自己琢磨更快哦!

编程学习视频分享:

 

以上是关于C/C++课程设计:停车场管理系统,290行代码助你轻松管理停车场的主要内容,如果未能解决你的问题,请参考以下文章

C/C++ 课程设计 | 银行管理系统

实战案例!1行Python代码识别车牌号码,轻松写一个停车场管理系统,YYDS

课程设计必备之数据库操作代码模板

课程设计 | 学生成绩管理系统

毕业设计 基于51单片机智能停车场管理车位引导系统设计

毕业设计 基于51单片机智能停车场管理车位引导系统设计