数据结构实验——模拟停车场

“Please use Xcode to run this”

. Experimental purpose:

  • Understand the logical structure and basic operation of stack and queue, and realize the basic functions of stack and queue.
  • Through experiments, we can further understand the logical structure and storage structure of stack and queue, and further improve the ability to use theoretical knowledge to guide and solve practical problems.

二. Experimental content:

Title: Management of simulated parking lot

Problem description:

There is only one narrow and long passage for 5 cars in the parking lot, and only one gate for cars to enter and exit. The cars are arranged in the order of arrival in the parking lot. If several cars have been parked in the parking lot, the later cars can only wait on the sidewalk outside the door. Once a car drives away in the parking lot, the first car on the sidewalk can enter; When a vehicle in the parking lot wants to leave, because the parking lot is a long and narrow channel, the vehicles entering after it must first exit the parking lot to make way for it. After the vehicle leaves the gate, the vehicles giving way for it will enter the parking lot in the original order. Here, it is assumed that cars cannot drive off the sidewalk.

Basic requirements:

The parking lot is simulated by stack, the access road outside the parking lot is simulated by queue, and the simulation management is carried out according to the data sequence input from the terminal. Each set of input data includes three data items: vehicle "arrival" or "departure" information, vehicle license plate number and arrival or departure time. The output information after operating each set of input data is: if the vehicle arrives, output the parking position of the vehicle in the parking lot or on the access road; If the vehicle leaves, the time the vehicle stays in the parking lot and the fees payable (no charge within 1 hour, the charging standard for more than 1 hour is 2 yuan / hour, and no charge for the time on the access road). The stack appears in a sequential structure, and the queue is implemented in a linked list structure.

Test data: 

(1) There are 7 vehicles arriving in succession with license plate numbers of jf001, jf002, jf003, jf004, jf005, jf006 and jf007. The first 5 vehicles should enter parking spaces 1-5, and the sixth and seventh vehicles should stop at positions 1 and 2 of the access road.

(2) When the situation in (1) occurs, let the vehicle with license plate No. jf003 drive away from the parking lot, and display the yield action of jf005 and jf004 and the action of jf006 from the sidewalk to the parking space.

(3) Check the status of parking spaces and access roads at any time. There should be no vacant parking spaces and cars on the access roads.

(4) The test of program fault tolerance, whether there is an error prompt to the user when the key input is wrong, guide the user to operate correctly, and make corresponding treatment to ensure the healthy operation of the program.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAXSIZE 5
#define NAMESIZE 20

typedef struct car{
    char name[NAMESIZE];
    int hour;
    int min;
    int l_hour;
    int l_min;
}car;

typedef struct SeqStack{
    int top;
    struct car parkinglot[MAXSIZE];
}*SeqStack;

SeqStack Init_Stack(){
    SeqStack s;
    s=(SeqStack)malloc(sizeof(SeqStack));
    s->top=-1;
    return s;
}

int Empty_Stack(SeqStack s){
    if (s->top==-1) {
        return 0;  //栈空
    }else return 1;     //栈非空
}

int Push_Stack(SeqStack s,struct car c){
    if (s->top!=MAXSIZE-1) {
        s->top++;
        s->parkinglot[s->top]=c;
        return 1;
    }else return 0;   //入栈失败
}

int Pop_Stack(SeqStack s, struct car* c){
    if (s->top!=-1) {
        *c=s->parkinglot[s->top];
        s->top--;
        return 1;
    }else{
        return 0;   //出栈失败,原因是栈空
    }
}

int Top_Stack(SeqStack s,struct car* x){
    if (s->top!=-1) {
        *x=s->parkinglot[s->top];
        return 1;
    }else{
        return 0;   //取值失败,原因是栈空
    }
}

typedef struct QNode{
    struct car c;
    struct QNode *next;

}*QNode;

typedef struct Queue{
    QNode front,rear;
}*Queue;

Queue Init_Queue(){
    QNode p;
    Queue q;
    p=(QNode)malloc(sizeof(QNode));
    q=(Queue)malloc(sizeof(Queue));
    p->next=NULL;
    q->front=p;
    q->rear=p;
    return q;
}

void In_Queue(Queue q, struct car c){
    QNode new;
    new=(QNode)malloc(sizeof(QNode));
    new->c=c;
    new->next=NULL;
    q->rear->next=new;
    q->rear=new;
}

int Empty_Queue(Queue q){
    if (q->front==q->rear) {
        return 1;
    }else return 0;
}

int Out_Queue(Queue q, struct car* x){
    QNode p;
    if (Empty_Queue(q)==1) {
        return 0;
    }else{
        p=q->front->next;
        q->front->next=p->next;
        *x=p->c;
        free(p);
        if (q->front->next==NULL) {
            q->rear=q->front;

        }
        return 1;
    }
}


typedef struct LinkList{
    char data[NAMESIZE];
    int time;
    struct LinkList* next;
}*LinkList,LNode;

LinkList Init(){
    LinkList head;
    head=(LinkList)malloc(sizeof(LNode));
    head->next=NULL;
    return head;
}
void Add(LinkList l,char data[NAMESIZE],int time){
    LinkList node=(LinkList)malloc(sizeof(LNode));
    node->next=NULL;
    for(int i=0;i<NAMESIZE;i++){
        node->data[i]=data[i];
    }
    LinkList head;
    head=l;
    while (head->next!=NULL) {
        head=head->next;
    }
    node->time=time;
    head->next=node;
}

void Delete(LinkList l,char data[NAMESIZE]){
    LinkList point;
    point=l;
    while (point->next->next!=NULL&&strcmp(data, point->next->data)!=0) {
        point=point->next;
    }
    LinkList kill;
    kill=point->next;
    point->next=point->next->next;
    free(kill);
}

void Delete_s(LinkList l){
    LinkList point;
    point=l;
    while (point->next->next!=NULL) {
        point=point->next;
    }
    LinkList kill;
    kill=point->next;
    point->next=point->next->next;
    free(kill);
}


typedef struct Field{
    LinkList monitor;
    Queue road;
    SeqStack parkinglot;
}Field;

struct Field Init_Field(){
    struct Field field;
    field.parkinglot=Init_Stack();
    field.road=Init_Queue();
    field.monitor=Init();
    return field;
}

int Count_s(SeqStack s, char data[NAMESIZE]){
    int result=0,k=0;
    int flag=0;
    while (k<MAXSIZE) {
        if (strcmp(s->parkinglot[k].name, data)==0) {
            result=k;
            flag=1;
            break;
        }else k++;
    }
    if (flag==0) {
        return 0;
    }
    return result+1;
}


int Count_q(Queue q,char data[NAMESIZE]){
    int counter=1;
    QNode o=q->front->next;
    while (o!=q->rear&&o) {
        o=o->next;
        counter++;
    }
    return counter;
}

struct car Get_Car(LinkList monitor){
    int time;
    struct car c1;
    printf("请输入车牌号:\n");
    scanf("%s",c1.name);
    printf("请输入到来时间(时):\n");
    scanf("%d",&c1.hour);
    printf("请输入到来时间(分):\n");
    scanf("%d",&c1.min);
    while (c1.hour>24||c1.min>60) {
        printf("输入的时间有误,请重新输入\n");
        printf("请输入到来时间(时):\n");
        scanf("%d",&c1.hour);
        printf("请输入到来时间(分):\n");
        scanf("%d",&c1.min);
    }
    time=c1.hour*60+c1.min;
    Add(monitor, c1.name,time);
    return c1;
}

struct car Get_Car1(LinkList monitor,int hour,int min){
    struct car c1;
    printf("请输入车牌号:\n");
    scanf("%s",c1.name);
    c1.hour=hour;
    c1.min=min;
    Add(monitor, c1.name,0);
    return c1;
}

void Get_Car2(struct car* c,int h,int min,LinkList monitor){
    //便道中车辆进入时间
    c->hour=h;
    c->min=min;
    int time= h*60+min;
    Add(monitor, c->name, time);
}

struct car Get_Car3(LinkList monitor, char str[NAMESIZE]){
    struct car c1;
    printf("请输入到来时间(时):\n");
    scanf("%d",&c1.hour);
    printf("请输入到来时间(分):\n");
    scanf("%d",&c1.min);
    //c1.name=str;
    for (int i=0; i<NAMESIZE; i++) {
        c1.name[i]=str[i];
    }
    Add(monitor, c1.name,0);
    return c1;
}

struct car Get_Car4(LinkList monitor){
    struct car c1;
    printf("请输入车牌号:\n");
    scanf("%s",c1.name);
    Add(monitor, c1.name,0);
    return c1;
}

int TimeMoney(struct car* c,LinkList monitor){
    int money,time;
    printf("请输入车辆离开时间(时):\n");
    scanf("%d",&c->l_hour);
    printf("请输入车辆离开时间(分):\n");
    scanf("%d",&c->l_min);
    while (c->hour>24||c->min>60) {
        printf("输入的时间有误,请重新输入\n");
        printf("请输入离开时间(时):\n");
        scanf("%d",&c->l_hour);
        printf("请输入离开时间(分):\n");
        scanf("%d",&c->l_min);
    }
    while (((c->l_hour*60+c->l_min)-(c->hour*60+c->min))<0) {
        printf("离开时间输入有误,早于来到时间,请重新输入\n");
        printf("请输入车辆离开时间(时):\n");
        scanf("%d",&c->l_hour);
        printf("请输入车辆离开时间(分):\n");
        scanf("%d",&c->l_min);
    }
    time=c->l_hour*60+c->l_min;
    LinkList monitor1=monitor->next;
    while (monitor1->next!=NULL) {           //有问题
        if (time<monitor1->time) {
            printf("离开时间输入有误,早于前面车辆的来到时间,请重新输入\n");
            printf("请输入车辆离开时间(时):\n");
            scanf("%d",&c->l_hour);
            printf("请输入车辆离开时间(分):\n");
            scanf("%d",&c->l_min);
        }
        monitor1=monitor1->next;
    }
    time=((c->l_hour*60+c->l_min)-(c->hour*60+c->min))/60;
    char s[NAMESIZE]="sssss";
    Add(monitor, s, time*60);
    money=(time)*2;
    return money;
}

void ComeIn(SeqStack s,Queue q,LinkList monitor){
    LinkList monitor1=monitor;
    struct car c1,c_last;
    if (Empty_Stack(s)==1) {
        Top_Stack(s, &c_last);
    }else{
        c_last.hour=0;
        c_last.min=0;
    }
    if (s->top+1<5) {
        c1=Get_Car(monitor);
        while ((c_last.hour*60+c_last.min)>(c1.hour*60+c1.min)) {
            printf("您输入的进入时间早于上一辆车的进入时间,请重新输入:\n");
            Delete_s(monitor);
            c1=Get_Car3(monitor,c1.name);
        }
        while (monitor1->next!=NULL) {
            if (strcmp(monitor1->data, c1.name)==0) {
                printf("您输入的车牌号已经存在,请重新输入:\n");
                Delete_s(monitor);
                c1=Get_Car1(monitor,c1.hour,c1.min);
            }
            monitor1=monitor1->next;
        }
        Push_Stack(s, c1);
        printf("您的车:%s已成功进入停车场!\n",c1.name);
    }else{
        printf("停车场已满,您即将进入便道等待...\n");
        
        
//        c1=Get_Car(monitor);
//        int time=c1.hour*60+c1.min;
//        monitor1=monitor->next;
//        while (monitor1->next!=NULL) {
//            if (time<=monitor1->time) {
//                printf("您输入的进入时间早于先前车的进入时间,请重新输入:\n");
//                Delete_s(monitor);
//                c1=Get_Car3(monitor,c1.name);
//                time=c1.hour*60+c1.min;
//            }
//            monitor1=monitor1->next;
//        }
        
        
        
        c1=Get_Car4(monitor);
        
        
        
        
        monitor1=monitor->next;
        while (monitor1->next!=NULL) {
            if (strcmp(monitor1->data, c1.name)==0) {
                printf("您输入的车牌号已经存在,请重新输入:\n");
                Delete_s(monitor);
                c1=Get_Car1(monitor,c1.hour,c1.min);
            }
            monitor1=monitor1->next;
        }
        In_Queue(q, c1);
        printf("您的车:%s已成功进入便道!您当前位置是%d\n",c1.name,Count_q(q, c1.name));
    }
}

void ComeOut(SeqStack s, Queue q, LinkList monitor){
    if (s->top==-1) {
        printf("停车场为空,不能进行出车操作\n");
        return;
    }
    char str[NAMESIZE];
    struct car temp[6];
    struct car c1;
    printf("请输入您的车牌号:\n");
    scanf("%s",str);
    int i,k;
    i=Count_s(s, str);
    if (i==0) {
        printf("您输入的车辆不存在");
        return;
    }else{
        printf("正在为您的车让路...\n");
        i=i-1;
        k=s->top-i;
        int init=k;
        while (k>0) {
            Pop_Stack(s, &c1);
            printf("%s正在为您让路……",c1.name);
            temp[k]=c1;
            k--;
        }
        Pop_Stack(s, &c1);
        printf("%s正在驶出\n",c1.name);
        Delete(monitor, c1.name);
        printf("您需要缴纳%d元\n", TimeMoney(&c1,monitor));
        struct car temp_l=c1;
        k++;
        while (k<=init) {
            Push_Stack(s, temp[k]);
            k++;
        }
        if (Empty_Queue(q)!=0) {
            return;         //队列为空时,防止将刚出的车辆在压入栈中
        }
        Out_Queue(q, &c1);
        Get_Car2(&c1, temp_l.l_hour, temp_l.l_min,monitor);
        Push_Stack(s, c1);

    }
}

void Display(SeqStack s,Queue q){
    int k=0;
    printf("车库中的车辆有:\n");
    while (k<=s->top) {
        printf("车辆%s于%d时%d分进入车库\n",s->parkinglot[k].name,s->parkinglot[k].hour,s->parkinglot[k].min);
        k++;
    }
    if (Empty_Queue(q)==0) {
        printf("便道中的车辆有:\n");
        QNode o=q->front->next;
        while (o!=NULL) {
            printf("车辆%s在便道\n",o->c.name);
            o=o->next;
        }
    }
}


int main(){
    struct Field field=Init_Field();
    while (1) {
        int function;
        printf("欢迎使用停车场系统:\n");
        printf("0.退出\n");
        printf("1.进车\n");
        printf("2.出车\n");
        printf("3.显示\n");
        printf("4.调试\n");
        printf("请输入功能号:\n");
        scanf("%d",&function);
        while (function>4||function<0) {
            printf("输入的功能号不存在,请重新输入:");
            scanf("%d",&function);
        }
        if (function==0) {
            break;
        }else if (function==1){
            ComeIn(field.parkinglot, field.road,field.monitor);
        }else if (function==2){
            ComeOut(field.parkinglot, field.road,field.monitor);
        }else if (function==3){
            Display(field.parkinglot,field.road);
        }else if (function==4){
            LinkList monitor1=field.monitor->next;
            while (monitor1->next!=NULL) {           //有问题
                printf("%d\n",monitor1->time);
                monitor1=monitor1->next;
            }
        }

    }//while循环的后括号

    return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值