现在的位置: 首页 > 综合 > 正文

停车场(二做)

2012年09月29日 ⁄ 综合 ⁄ 共 3146字 ⁄ 字号 评论关闭
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 20//停车场容量
#define PRICE 20//20元/小时

typedef struct{
    float hour;
    float min;
}Time;

typedef struct{
    char license[10];
    Time reach,leave;
}CarNode;

typedef struct{
    CarNode *base;
    CarNode *top;
    int len;
}SqStack;

void InitStack(SqStack &S)
{
    S.base=(CarNode*)malloc(MAX*sizeof(CarNode));
    if(!S.base)
        exit(0);
    S.top=S.base;
    S.len=MAX;
}

int emptystack(SqStack S)
{
    if(S.top==S.base)
        return 1;
    else return 0;
}

void push(SqStack &S,CarNode e)
{
    *S.top++=e;
}

void pop(SqStack &S,CarNode &e)
{
    e=*--S.top;
}

typedef struct node{
    CarNode data;
    node *next;
}QNode,*Queueptr;

typedef struct{
    Queueptr front;
    Queueptr rear;
}LinkQueue;

void InitQueue(LinkQueue &Q)
{
    Q.rear=(Queueptr)malloc(sizeof(CarNode));
    if(!Q.rear) exit(0);
    Q.front=Q.rear;
    Q.front->next=NULL;
}

void EnQueue(LinkQueue &Q,CarNode e)
{
    Queueptr p;
    p=(Queueptr)malloc(sizeof(CarNode));
    if(!p)
        exit(0);
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
}

void DeQueue(LinkQueue &Q,CarNode &e)
{
    Queueptr p;
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(p==Q.rear)
        Q.front=Q.rear;
    free(p);
}

int emptyqueue(LinkQueue Q)
{
    if(Q.front==Q.rear)
        return 1;
    else return 0;
}

void Reach(SqStack &park,LinkQueue &pave)
{
    CarNode e;
    printf("请输入车牌号:");
    scanf("%s",e.license);
    if(park.top-park.base<park.len)
    {//停车场还有空位
        printf("请输入%s车进停车场时间:",e.license);
        scanf("%f%f",&e.reach.hour,&e.reach.min);
        printf("请%s车在位置%d停车!\n",e.license,park.top-park.base+1);
        push(park,e);
    }
    else{
        printf("请到便道等候!\n");
        DeQueue(pave,e);
    }
}

void Leave(SqStack &park,LinkQueue &pave,SqStack &temp)
{
    int locate;
    float pri;
    CarNode e;
    if(!emptystack(park)){
        printf("请输入要离开的车位号(1~%d):",park.top-park.base);
        scanf("%d",&locate);
        while(park.top-park.base>locate)
        {
            pop(park,e);
            push(temp,e);
        }
        pop(park,e);
        printf("请输入%s车离开时间:",e.license);
        scanf("%f%f",&e.leave.hour,&e.leave.min);
        pri=((e.leave.hour-e.reach.hour)+(e.leave.min-e.reach.min)/60)*PRICE;
        printf("车牌号\t进入时间 离开时间 缴纳费用\n");
        printf("%s\t%.0f:%.0f\t%.0f:%.0f\t%.3f\n",e.license,e.reach.hour,e.reach.min,e.leave.hour,e.leave.min,pri);
        while(temp.top-temp.base>=1)
        {
            pop(temp,e);
            push(park,e);
        }
        if(park.top-park.base==park.len-1&&pave.front!=pave.rear)
        {
            printf("请便道上的车进入车位%d",park.len);
            DeQueue(pave,e);
            printf("请便道上的%s车进入位置%d停车!\n",e.license,park.len);
            printf("请输入%s车进停车场时间:",e.license);
            scanf("%f%f",&e.reach.hour,&e.reach.min);
            push(park,e);
        }
        else
        {
            printf("便道内没有车在等候!\n");
        }
    }
    else
        printf("停车场已经没有车!\n");
}

void seepark(SqStack S)
{
    int i;
    CarNode *p;
    p=S.base;
    if(emptystack(S))
        printf("停车场为空!\n");
    for(i=1;i<=S.top-S.base;i++)
    {
        printf("车牌号\t进入时间\n");
        printf("%s\t%.0f:%.0f\n",p->license,p->reach.hour,p->reach.min);
        p++;
    }
}
void seepave(LinkQueue Q)
{
    Queueptr p;
    p=Q.front->next;
    if(emptyqueue(Q))
        printf("便道为空!\n");
    while(p)
        printf("车牌号:%s\n",p->data.license);
}


int main()
{
    SqStack temp,park;
    LinkQueue pave;
    int choice;
    InitStack(park);
    InitStack(temp);
    InitQueue(pave);
    printf("-------欢迎光临本停车系统----------\n");
    printf("\t 1.车辆到达登记\n");
    printf("\t 2.车辆离开统计\n");
    printf("\t 3.显示停车场信息\n");
    printf("\t 4.显示便道车辆信息\n");
    printf("\t 0.退出系统\n");
    do
    {
        printf ("请输入要查看的信息编号(1 2 3 4 ),按0退出\n");
        scanf ("%d",&choice);
        if(choice>4&&choice<0)
        {
            printf("输入有误,请重来!\n");
            continue;
        }
        switch(choice)
        {
            case 1: Reach(park,pave);break;
            case 2: Leave(park,pave,temp);break;
            case 3: seepark(park);break;
            case 4: seepave(pave);break;
            case 0: exit(0);//退出主程序
            default :break;
        }
    }while(1);
    return 0;
}

 

抱歉!评论已关闭.