停车场程序--自行输入时间

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define maxlen 15
#define max_stop 2
#define max_pave 3
#define price 5//每小时收费5元
typedef struct
{
    char name[maxlen];
    int inhour;
    int inminute;
    int outhour;
    int outminute;
} Car;
typedef struct
{
    Car data[max_stop];
    int top;
} Stack_Car,Stack_Carhelp;

typedef struct
{
    Car data[max_pave-1];
    int front;
    int rear;
    int cout;
} Queue_Car;

Stack_Car *c;//停车场
Stack_Carhelp *h;//辅助栈
Queue_Car *p;//便道
char carname[maxlen];
int tempih[max_stop];//各车辆进入停车场的小时
int tempim[max_stop];//各车辆进入停车场的分钟
int tempoh;//车辆离开停车场的小时
int tempom;//车辆离开停车场的分钟
int totaltime;
Stack_Car *init()//栈初始化
{
    Stack_Car *car=(Stack_Car*)malloc(sizeof(Stack_Car));
    //Stack_Car *car;
    car->top=-1;
    return car;
}
Queue_Car *init1()//队列初始化
{
    Queue_Car *car=(Queue_Car*)malloc(sizeof(Queue_Car));
    //Queue_Car *car;
    car->front=car->rear=max_pave-1;
    car->cout=0;//记录便道的车辆个数
    return car;
}
void Car_into_pave(Queue_Car *p,char name[maxlen])//车辆进入便道
{
    if(p->cout>0&&p->front==(p->rear+1)%max_pave)//便道已满
    {
        printf("便道已满,欢迎下次光临~\n");
        return;
    }
    else
    {
        p->rear=(p->rear+1)%max_pave;
        p->cout++;
        strcpy(p->data[p->rear].name,carname);
        printf("欢迎光临%s号车\n",carname);
        printf("牌照为%s的汽车开入便道%d号车位\n",carname,p->rear+1);//停车场编号从1开始
    }
}
void Car_into_stack()//车辆进入停车场
{
    printf("请输入将要停车的车牌号:\n");
    scanf(" %s",carname);
    if(c->top==max_stop-1)//判断停车场是否满位
        Car_into_pave(p,carname);
    else
    {
        printf("停车场有空闲位置,请输入此时的时间(h,m):\n");
        c->top++;
        scanf(" %d:%d",&c->data[c->top].inhour,&c->data[c->top].inminute);//输入进入停车场各车辆时间
        tempih[c->top]=c->data[c->top].inhour;
        tempim[c->top]=c->data[c->top].inminute;//记录进入停车场各车辆时间
        strcpy(c->data[c->top].name,carname);//存储各车辆牌号信息
        printf("欢迎光临%s号车\n",carname);
        printf("牌照为%s的汽车开入停车场%d号车位\n",carname,c->top+1);
    }
}
void Car_mid()
{
    int i, find = 0;
    while(c->top>=0)
    {
        if(strcmp(c->data[c->top--].name,carname)==0)//寻找要离开的车辆,若找到则跳出此循环
        {
            find = 1;
            break;
        }
        else
        {
            strcpy(h->data[++h->top].name,c->data[c->top+1].name);//要离开的车后面车辆压入辅助栈
            printf("牌照为%s的汽车暂时退出停车场\n",c->data[c->top+1].name);
        }
    }
    if(!find)//完全遍历后未找到此车,找到的位置是c->top+1
    {
        printf("查无此车!\n");
        while(h->top>=0)//从辅助栈压入临时停放的车辆
        {
            strcpy(c->data[++c->top].name,h->data[h->top--].name);
        }
        return;
    }
    else
    {
        printf("请输入此时离开的时间:\n");
        scanf("%d:%d",&c->data[c->top+1].outhour,&c->data[c->top+1].outminute);
        tempoh=c->data[c->top+1].outhour;//记录离开车的小时
        tempom=c->data[c->top+1].outminute;//记录离开车的分钟
        if(tempih[c->top+1]==tempoh)//出去和进入的小时是相同的,不满一小时
        {
            totaltime=1;
        }
        else
        {
            totaltime=tempoh-tempih[c->top+1]+(tempom>tempim[c->top+1]?1:0);//计算停车的时间,小时
        }
        printf("牌照为%s的汽车从停车场开走\n",c->data[c->top+1].name);
        printf("停车%d小时,应收费%d元\n",totaltime,price*totaltime);
        if(c->top+1>=0)
        {
            for(i=c->top+1; i<max_stop-1; i++)//c->top+1是离开的车辆位置,离开位置后的车辆进入时间需改变
            {
                tempih[i]=tempih[i+1];//离开车辆位置后的车辆进入时间往前移一位
                tempim[i]=tempim[i+1];//记录原来车辆的进入时间
            }
            tempih[max_stop-1]=tempoh;
            tempim[max_stop-1]=tempom;//最外头的是新进来车辆的时间;先是辅助栈进入停车场,然后是便道的车辆进入停车场

        }
        while(h->top>=0)//从辅助栈压入临时停放的车辆
        {
            strcpy(c->data[++c->top].name,h->data[h->top--].name);
        }
        if(c->top<=max_stop-1&&p->cout>0)//
        {
//        if(p->cout==0)
//            break;//便道内无车辆
//        else
            p->front=(p->front+1)%max_pave;
            strcpy(c->data[++c->top].name,p->data[p->front].name);//将便道的车停入停车场
            printf("牌照为%s的汽车停回停车场%d号车位\n",p->data[p->front].name,c->top+1);//若便道有车辆,则p->data[p->front].name始终是便道第一车位的车辆,符合先到先出(这里有便道信息的更新处理)
            if(p->cout>1)
            {
                for(i=p->front+1; i<p->cout; i++)
                {
                    strcpy(p->data[i-1].name,p->data[i].name);//便道的车停入停车场后,便道车位信息更新,便道车向前移
                }
                p->rear--;
                p->front--;//未加此语句时是原先离开后面的车辆信息,经过便道车辆信息处理后,display输出的是移动后车辆的信息
                p->cout--;
            }
            else//便道中只有一辆车,开出后便道无车,为最初的初始化值,车辆数为1
            {
                p->rear=max_pave-1;
                p->front=max_pave-1;
                p->cout=0;
            }
        }
    }
}
void Car_leave()//车辆离开
{
    printf("请输入将要离开的车牌号:\n");
    scanf(" %s",carname);
    if(c->top<0)//停车场已空
        printf("停车场已空!\n");
    else
        Car_mid();
}
void Display()
{
    int i;
    if(c->top==-1)
    {
        printf("停车场空\n");
    }
    else
    {
        printf("停车场有%d个车位,已停%d个车位,便道已停%d个车位\n",max_stop,c->top+1,p->cout);
        if(p->cout>0)
        {
            printf("停车场车辆情况:\n");
            for(i=0; i<=c->top; i++)
            {
                printf("%d号停车车位:%s\n",i+1,c->data[i].name);
            }
            printf("便道车辆情况:\n");
            for(i=0; i<p->cout; i++)
            {
                printf("%d号便道车位:%s\n",i+1,p->data[i].name);
            }
        }
        else
        {
            printf("停车场车辆情况:\n");
            for(i=0; i<=c->top; i++)
            {
                printf("%s\n",c->data[i].name);
            }
            printf("便道为空!\n");
        }
    }
}
void welcome()
{
    printf ("\t*******************目前停车场状况***********************\n");
    printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n",max_stop, c->top+1, p->cout);
    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");
}

int main()
{
    totaltime=0;
    c=init();
    h=init();
    p=init1();//栈和队列初始化处理
    while(1)
    {
        system("cls");//友好界面清评操作
        welcome();
        int i, choice=0;
        scanf ("%d", &i);
        if (i==1) Car_into_stack();
        if (i==2) Car_leave();
        if (i==3) Display();
        if (i==4) break;

        printf ("返回请输入1:\n");
        fflush(stdin);
        scanf("%d", &choice);
        while (choice != 1)
        {
            printf("您的输入有误,请重新输入\n");
            scanf("%d", &choice);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单的车位管理程序 随着家庭购买汽车的增加,停车场车位紧张的问题越来越突出。请根据题目要求完成简单的车位管理程序。 1.停车场有若干停车位(为说明问题,假定为3个),每个位置可以存放不同种类的的汽车,包括卡车Truck,客车Carriage和小轿车Car,但同一时刻一个位置只能存放0或1辆汽车。 2.管理系统模拟实际车辆停车的情况:新来车辆时如果有空位,按顺序为该车分配停车位; 车辆开走时,交纳相应停车费;统计各类车辆的数量。 3.定义描述停车场的类Park,其中有3个位置用于存放各类车辆。 4.定义基类Automobile,至少包括纯虚函数Pay用于显示车辆信息并交纳相应停车费。 5.定义派生类Truck,Carriage和Car,这些车辆除了拥有车牌号(字符串)、车辆已使用年数(整数)之外, Truck还拥有载重量(浮点数,单位吨)属性,Carriage还拥有乘坐人数(整数,单位人)属性,Car还拥有排气量(浮点数,单位L)属性。具体实现上述纯虚函数Pay,显示每类车辆的相应信息,并给出计价提示,其中Truck收费2元/小时,Carriage收费1.5元/小时,Car收费1元/小时。 6.重载输入“>>”操作符,使得可以通过cin直接读入每类车辆的相应信息。 7.编写main函数,测试上述所要求的各种功能,即根据菜单命令为新来车辆分配停车位、开走车辆(输入车位编号)时付费、显示停车场中各类车辆的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值