C语言栈和队列问题:停车场停车问题

C语言栈和队列问题:停车场停车问题c语言程序题现有一自动停车场,基本结构是宽度只能停放一辆汽车的狭长通道,只有一个大门可供汽车进出。
现有一自动停车场,基本结构是宽度只能停放一辆汽车的狭长通道,只有一个大门可供汽车进出。汽车在停车场内按到达的先后顺序依次排列,若车场内已停满汽车,则新到达的汽车只能在门外的便道上等候(假设便道无限长),一旦停车场内有汽车离场,则排在便道上的第一辆汽车即可进入;当停车场内某辆汽车要离场时(假设只有进入停车场的汽车才能离场),由于停车场是狭长的通道,在它之后进场的汽车必须先退出车场让路,待离场汽车离开停车场后,让路的汽车再按原次序进入车场。请编写程序模拟对上述停车场的管理。

注意:

1. 车辆信息自定义 ;

2. 程序功能需使用栈和队列的基本操作实现 ;

3. 栈使用顺序存储结构,队列使用链式存储结构 ;

4. 要求能够模拟车辆进场、车辆离开及查看停车场的信息 。

#ifndef __PLOT_H__#define __PLOT_H__#define FALSE 0#define TRUE  1#define MONEY    1          // 单价可以自己定义#define MAX_STOP 10#define MAX_PAVE 100// 存放汽车牌号typedef struct{   

    int time1;              // 进入停车场时间
    int time2;              // 离开停车场时间
    char plate[10];  
    // 汽车牌照号码,定义一个字符指针类型}Car;// 停放栈typedef struct{
    Car Stop[MAX_STOP-1];   // 各汽车信息的存储空间
    int top;                // 用来指示栈顶位置的静态指针}Stopping;// 等候队列typedef struct{    int count;              // 用来指示队中的数据个数
    Car Pave[MAX_PAVE-1];   // 各汽车信息的存储空间
    int front, rear;        // 用来指示队头和队尾位置的静态指针}Pavement;// 让路栈typedef struct{
    Car Help[MAX_STOP-1];   // 各汽车信息的存储空间
    int top;                // 用来指示栈顶位置的静态指针}Buffer;

Stopping s;
Pavement p;
Buffer   b;
Car      c;char     C[10];void stop_pave    ();       // 车停入便道void car_come     ();       // 车停入停车位void stop_to_buff ();       // 车进入让路栈void car_leave    ();       // 车离开void welcome      ();       // 主界面函数void Display      ();       // 显示车辆信息#endif123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
源文件 PLot.c
#include "PLot.h"#include <stdio.h>#include <time.h>                           // 包含时间函数的头文件#include <string.h>#include <stdlib.h>void stop_to_pave()                         // 车停入便道{    // 判断队满
    if (p.count > 0 && (p.front == (p.rear + 1) % MAX_PAVE))
    {        printf ("便道已满,请下次再来\n");
    }    else
    {        strcpy(p.Pave[p.rear].plate, C);
        p.rear = (p.rear + 1) % MAX_PAVE;   // 队尾指示器加1
        p.count++;                          // 计数器加1
        printf ("牌照为%s的汽车停入便道上的%d的位置\n", C, p.rear);
    }
}void car_come()                             // 车停入停车位{    printf ("请输入即将停车的车牌号:");        // 输入车牌号
    scanf ("%s", &C);    if (s.top >= MAX_STOP - 1)              // 如果停车位已满,停入便道
    {
        stop_to_pave();                     // 停车位->便道  函数
    }    else
    {
        s.top++;                            // 停车位栈顶指针加1
        time_t t1;        long int t = time (&t1);            // 标记进入停车场的时间
        char* t2;
        t2 = ctime (&t1);                   // 获取当前时间
        c.time1 = t;        strcpy(s.Stop[s.top].plate, C);     // 将车牌号登记                   
        printf ("牌照为%s的汽车停入停车位的%d车位, 当前时间:%s\n", C, s.top + 1, t2);
    }    return ;
}void stop_to_buff()                         // 车进入让路栈{    // 停车位栈压入临时栈,为需要出栈的车辆让出道
    while (s.top >= 0)  
    {   
        if (0 == strcmp(s.Stop[s.top--].plate, C))
        {            break;
        }        // 让出的车进入让路栈
        strcpy(b.Help[b.top++].plate, s.Stop[s.top + 1].plate);        printf ("牌照为%s的汽车暂时退出停车位\n", s.Stop[s.top + 1].plate);
    }   
    b.top --;    // 如果停车位中的车都让了道,说明停车位中无车辆需要出行
    if (s.top < -1)
    {        printf ("停车位上无此车消息\n");
    }    else 
    {        printf ("牌照为%s的汽车从停车场开走\n", s.Stop[s.top + 1].plate);
    }    // 将让路栈中的车辆信息压入停车位栈
    while (b.top >= 0)
    {        strcpy(s.Stop[++s.top].plate, b.Help[b.top--].plate);        printf ("牌照为%s的汽车停回停车位%d车位\n", b.Help[b.top + 1].plate, s.top + 1);
    }    // 从便道中 -> 停车位
    while (s.top < MAX_STOP-1)
    {        if (0 == p.count)               // 判断队列是否为空
        {            break;  
        }        // 不为空,将便道中优先级高的车停入停车位
        else                    
        {            strcpy(s.Stop[++s.top].plate, p.Pave[p.front].plate);            printf ("牌照为%s的汽车从便道中进入停车位的%d车位\n", p.Pave[p.front].plate, s.top+1);
            p.front = (p.front + 1) % MAX_PAVE;
            p.count--;
        }
    }
}void car_leave()                        // 车离开{    printf ("请输入即将离开的车牌号:\n");    scanf ("%s", &C);    if (s.top < 0)                      // 判断停车位是否有车辆信息
    {        printf ("车位已空,无车辆信息!\n");
    }    else
    {
        stop_to_buff();
    }

    time_t t1;      
    long int t = time (&t1);                
    c.time2 = t;                        // 标记离开停车场的时间
    char* t2;
    t2 = ctime (&t1);                   // 获取当前时间
    printf ("离开时间%s\n需付%ld元\n", t2, MONEY * (c.time2 - c.time1) / 10);
}void Display()
{    int i = s.top;    if (-1 == i)
    {        printf ("停车场为空\n");
    }
    time_t t1;    long int t = time(&t1);             // 标记显示时的时间
    printf ("\t车牌号\t\t\t停放时间\t\t当前所需支付金额\n");    while (i != -1)
    {       
        printf ("\t%s\t\t%d秒\t\t\t%d元\n", s.Stop[i].plate, t - c.time1, MONEY * (t - c.time1) / 10);
        i--;
    }
}void welcome()
{        printf ("\t*******************目前停车场状况***********************\n");        printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)
        % MAX_PAVE);        printf ("\t********************************************************\n");        printf ("\t---------------Welcome to our Car Parking---------------\n");  
        printf ("\t*                     1.Parking                        *\n");  
        printf ("\t*                     2.leaving                        *\n");  
        printf ("\t*                     3.situation                      *\n");  
        printf ("\t*                     4.exit                           *\n");  
        printf ("\t--------------------------------------------------------\n");


123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
主函数 main.c
/**********************************************************
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,
汽车按到达的先后次序停放。若车场满了,车要在门外的便道上等候
,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由
于通道窄,在它后面的车要先退出,待它走后依次进入。汽车离开
时按停放时间收费。
基本功能要求:
    1)建立三个数据结构分别是:停放队列,让路栈,等候队列
    2)输入数据模拟管理过程,数据(入或出,车号)。
***********************************************************/#include "PLot.h"int main()
{    // 初始化
    s.top   = -1;
    b.top   =  0;
    p.rear  =  0;
    p.count =  0;
    p.front =  0;    while(1)
    {
        system("clear");
        welcome();        int i, cho;
        scanf ("%d", &i);        if (1 == i) car_come(); 
        if (2 == i) car_leave();        if (3 == i) Display();        if (4 == i) break;

        printf ("返回请输入1\n");
        scanf ("%d", &cho);        if (1 == cho)
        {            continue;
        }        else
        {
            printf ("您的输入有误,请重新输入\n");
            scanf ("%d", &cho);            continue;
        }
    }    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答