数据结构 - 停车场管理

停车场管理

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define FALSE 0
#define TRUE 1
#define  MONEY 1
#define MAX_STOP 2
#define MAX_PAVE 2
//存放汽车牌号
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;
car c;
buffer b;
pavement p;
char C[10];//车牌号 
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;//队尾显示器加一
        p.count++;
        printf ("牌照为%s的汽车停入便道上的%d的位置\n", C, p.rear - 1);
    }
}
void car_come()//车停入停车位
{
    printf("请输入即将停车的车牌号\n");
    scanf("%s",C);
    if(s.top>=MAX_STOP-1)
    stop_to_pave();
    else
    {
        s.top++;
        time_t t1;
        long int t=time(&t1);//标记进入停车场的时间;
        char*t2;
        t2=ctime(&t1);//获取当前时间函数名:
        //ctime 功  能: 把日期和时间转换为字符串
        //用  法: char *ctime(const time_t *time);
        c.time1=t;
        strcpy(s.stop[s.top].plate,C);//见过车牌号登记
        printf("牌照为%s的汽车停入停车位的%d车位,当前时间:%s\n",C,s.top+1,t2);
    }
}
void stop_to_buff(){//停车栈压入临时栈 为要出去的车让道
    while(s.top>=0)
    {
        if(0==strcmp(s.stop[s.top--].plate,C))//*C/C++函数,比较两个字符串
           break;
    //设这两个字符串为str1,str2,
    //若str1==str2,则返回零;
    //若str1<str2,则返回负数;
    //若str1>str2,则返回正数。*/
           // break;
     strcpy(b.help[b.top++].plate,s.stop[s.top+1].plate);
     printf ("牌照为%s的汽车暂时退出停车位\n", s.stop[s.top + 1].plate);
     s.top--;
    }
    //让车的车进入让路栈
   // strcpy(b.help[b.top++].plate,s.stop[s.top++].plate);
    // printf ("牌照为%s的汽车暂时退出停车位\n", s.stop[s.top + 1].plate);
    //b.top--;
    // 如果停车位中的车都让了道,说明停车位中无车辆需要出行
        
        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(p.count==0) 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);
    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 welcome(){
    printf("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n",MAX_STOP,s.top+1,(p.rear+MAX_PAVE-p.front)%MAX_PAVE);
    printf("\t          1.停车      2.离开      3.返回      4.退出            \n");
}
int main(){//初始化
    s.top   = -1;
    b.top   =  0;
    p.rear  =  0;
    p.count =  0;
    p.front =  0;
    while(1)
    {
        system("cls");//清屏 
        welcome();
        int i,cho;
        scanf("%d",&i);
        if(i==1) car_come();
        if(i==2) car_leave(); 
        if(i==4) break;
        scanf("%d",&cho);
        if(cho==3)
		continue; 
    }
    return 0;
}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值