前端之家收集整理的这篇文章主要介绍了
【数据结构】关于停车场的管理,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- #include<stdio.h>
- #include<stdlib.h>
- #include<time.h>
- #include<string.h>
- #include<windows.h>
- #define SIZE 100
- #define PRICE 10
- typedef struct
- {
- char ID;
- clock_t start;
- }Nodestack;
- typedef struct
- {
- Nodestack* top;
- Nodestack* base;
- int size;
- }SQStack;
- typedef struct node
- {
- char data;
- struct node *next;
- }node;
- typedef struct
- {
- node *front;
- node *rear;
- }Linkqueue;
- /////////////////////////////////
- void Initstack(SQStack &l)
- {
- l.base=(Nodestack *)malloc(sizeof(Nodestack)*SIZE);
- l.top=l.base;
- l.size=SIZE;
- }
- void push(SQStack &l,Nodestack p)
- {
- *(l.top)=p;
- l.top++;
- }
- void popstack(SQStack &l,Nodestack &a)
- {
- a=*(--l.top);
- }
-
- char gettop(SQStack &l)
- {
- if(l.top!=l.base)
- {
- Nodestack p;
- p=*(l.top-1);
- return p.ID;
- }
- return 0;
- }
- int stackempty(SQStack &l)
- {//空返回1
- if(l.top==l.base) return 1;
- else return 0;
- }
- void traverstack(SQStack l)
- {
- long temp_1;
- char temp_2;
- int i=0;
- Nodestack temp_3;
- if(stackempty(l)==1) printf("栈中无元素\n");
- else
- {
- for(i=0;l.top-i!=l.base;i++)//非空时执行
- {
- temp_3=*(l.top-i-1);
- temp_1=temp_3.start;
- temp_2=temp_3.ID;
- printf("%c %ld\n",temp_2,temp_1);
- }
- }
- }
- int lenghtstack(SQStack l)
- {
- int i=0;
- while(l.top-i!=l.base)
- {
- i++;
- }
- return i;
- }
- /////////////////////////////////////
- void Initqueue(Linkqueue &l)
- //初始化队列
- {
- l.front=l.rear=(node *)malloc(sizeof(node));
- if(!l.front) exit(0);
- l.front->next=NULL;
- }
- int queueempty(Linkqueue &l)
- //判断队列是否为空,空返回1,非空返回0
- {
- if(l.front==l.rear) return 1;
- else return 0;
- }
- int lenghtqueue(Linkqueue &l)
- //返回队列的长度
- {
- int i=0;
- node *p;
- p=l.front->next;
- while(p!=NULL)
- {
- i++;
- p=p->next;
- }
-
- return i;
- }
- void enqueue(Linkqueue &l,char e)
- //对尾入栈
- {
- node *p;
- p=(node*)malloc(sizeof(node));
- p->data=e;
- l.rear->next=p;
- p->next=NULL;
- l.rear=p;
- }
-
- char dequeue(Linkqueue &l)
- //对头出栈
- {
- node *p;
- if(l.front==l.rear) return 0;
- char q;
- p=l.front->next;
- q=(*p).data;
- l.front->next=p->next;
- if(p==l.rear) l.front=l.rear;
- free(p);
- return q;
- }
- void traversequeue(Linkqueue &l)
- //遍历队列
- {
- node *p=l.front->next;
- if(queueempty(l)==1) printf("队列里没有元素");
- while(p!=NULL&&queueempty(l)==0)
- {
- printf("%c",(*p).data);
- p=p->next;
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- void function1(SQStack &l,Linkqueue &s,int n)
- {
- char temp;
- static int max=0;
- clock_t start;
- Nodestack nodes;
- //node nodeq;
- printf("请输入需要停车的车牌号:\t");
- scanf("%c",&temp);
- getchar();
- max=lenghtstack(l);
- if(max<n)
- {
- start=clock();
- nodes.ID=temp;
- nodes.start=start;
- push(l,nodes);
- printf("在停车场的第%d个位置,进入时间为:%ld",max+1,start);
-
- }
- else
- {
- start=clock();
- enqueue(s,temp);
- printf("在便道的第%d个位置,lenghtqueue(s),start);
- }
- Sleep(2000);
- }
- ////////////////////////////////////////////////////////////////////////////
- int function2(SQStack &l,SQStack &m)
- {
- char car_id,temp_1;
- double staytime;
- Nodestack temp_2,del;
- clock_t start,finish;
- printf("请输入离开车辆的车牌号:");
- scanf("%c",&car_id);
- getchar();
- temp_1=gettop(l);
- while(temp_1!=car_id)
- {
- if(stackempty(l)==1)
- {
- printf("不存在车牌号为%c的车辆",car_id);
- return 0;
- }
- else
- {
- popstack(l,temp_2);
- push(m,temp_2);
- temp_1=gettop(l);
- }//else
- }//while
- if(temp_1==car_id)
- {
- popstack(l,del);
- while(stackempty(m)==0)
- {
- popstack(m,temp_2);
- push(l,temp_2);
- }
- if(queueempty(s)==0)
- {
- temp_1=dequeue(s);
- start=clock();
- temp_2.ID=temp_1;
- temp_2.start=start;
- push(l,temp_2);
- }
- finish=clock();
- staytime=(double)(finish-del.start)/CLOCKS_PER_SEC;
- printf("车牌号:%c\t停留时长:%f sec\t应付款:¥%f\t",del.ID,staytime,staytime*PRICE );
- }
- Sleep(2000);
- return 0;
- }
- ////////////////////////////////////////////////
- void function3(SQStack &l,Linkqueue &s)
- {
- printf("便道上的车辆:\n");
- traversequeue(s);
- printf("\n");
- printf("停车道上的车辆:\n");
- traverstack(l);
- printf("\n");
- Sleep(2000);
- }
- ////////////////////////////////////
- void meun()
- {
- system("cls");
- printf("\t\t停车场管理目录\n");
- printf("\tA: 停车\n");
- printf("\tD: 离开\n");
- printf("\tC: 查看车场情况\n");
- printf("\tE: 退出系统\n");
- }
- ///////////////////////////////////////////////////////////////////////////
- void main()
- {
- SQStack stack1,stack2;
- Linkqueue queue;
-
- int n;//车库的容量
- char choice;
- Initstack(stack1);
- Initstack(stack2);
- Initqueue(queue);
-
- printf("请输入停车场的容量:\t");
- scanf("%d",&n);
- getchar();
- meun();
- scanf("%c",&choice);
- getchar();
- while(choice!='E')
- {
- switch(choice)
- {
- case 'A':function1(stack1,queue,n); break;
- case 'D':function2(stack1,stack2); break;
- case 'C':function3(stack1,queue);break;
- default:;
- }
- if(choice=='E')
- printf("欢迎下次使用!\n");
- meun();
- scanf("%c",&choice);
- getchar();
- }
- }
弊端是输入的车牌号只是一位字符,可以做适当的优化