小小项目3:停车场管理

来源:互联网 发布:淘宝返现卡 编辑:程序博客网 时间:2024/04/30 21:50

这个项目没有什么实际意义,关键是为了体验栈和队的实际使用 。

我就发个主函数和关键几个函数。

先是栈和队列的头文件

/*本题我需要一个栈和一个队列栈用来停车和让车队用来候车*/#ifndef __STACKQUEUE_H__#define __STACKQUEUE_H__#include"error.h"#define SIZE_MAX 2//停车信息typedef struct _node{int id;char car[10];long int time;}NODE;//------------队列(用链表)typedef struct node_queue    //链表结点{NODE data;struct node_queue *next;}NODEQUEUE;typedef struct _queue   {NODEQUEUE *front;NODEQUEUE *rear;}QUEUE;//------------栈(用数组)typedef struct stack{int top;NODE arr[SIZE_MAX];}STACK;//————————————————————————————————————————————————————————队列//创建链表队列QUEUE * Crate_queue();//清空队列int QueueEmpty(QUEUE *q);//进队int PushQueue(QUEUE *q,NODE data);//出队(出队是从front开始的)int PopQueue(QUEUE *q,NODE *data);//删除队列int DelQueue(QUEUE *q);//————————————————————————————————————————————————————————栈//清空栈int Init_stack(STACK * s);//判断是否空栈int Stack_Empty (STACK * s);//判断是满空栈int Stack_Full (STACK * s);//入栈int PushStcak(STACK * s,NODE data);//出栈int PopStcak(STACK * s,NODE *data);#endif

然后是关键函数头文件

#ifndef __CAR_H__#define __CAR_H__#define D (24*60*60)#define H (60*60)#define M (60)//停车void stop_char(QUEUE * wait,STACK * stop);//进入候车int wait_car(QUEUE* wait);//进入停车int in_car(STACK* stop,NODE car);//获取当前时间long int now_time();//离车int leave_car(QUEUE * wait,STACK * stop,STACK * give);//找到离开的车(返回:离车所要求车所需要出栈的次数)int find(STACK * stop,int x);//输出情况int Risplay(STACK * stop,QUEUE * wait);//查看一辆车的数据void look_car(STACK * stop,int x);#endif


关键函数
#include<stdio.h>#include<stdlib.h>#include<time.h>#include"StackQueue.h"#include"Car.h"//-------------------------------------------------------------停车int stop_car(QUEUE * wait,STACK * stop){    system("clear\n");if(Stack_Full (stop) == TRUE){printf("目前已没有车位,请进入候车道等待。\n\n");wait_car(wait);printf("请耐心等待。谢谢配合!\n");}else{printf("欢迎停车,请进。\n");NODE car;car.id = 0;printf("请输出车牌号:");scanf("%s",car.car);car.time = 0;in_car(stop,car);printf("停车成功,祝您停车愉快!\n");}getchar();getchar();return TRUE;}//进入候车int wait_car(QUEUE * wait){NODE car;car.id = 0;printf("请输出车牌号:");scanf("%s",car.car);car.time = 0;PushQueue(wait,car);return TRUE;}//进入停车int in_car(STACK* stop,NODE car){static int i = 1;car.id = i++;printf("您的停车号是%d\n",car.id);car.time = now_time();printf("您的停车时间为:%s\n",ctime(&(car.time)));//入栈PushStcak(stop,car);}//获取当前时间long int now_time(){time_t t1;time(&t1);return t1;}//-------------------------------------------------------------离车int leave_car(QUEUE * wait,STACK * stop,STACK * give){system("clear\n");printf("请输出您要结束停车的停车号:");int x ;scanf("%d",&x);int num = find(stop,x);if (num == 0){printf("系统在三秒后返回。\n");sleep(3);return FALSE;}//车进入让车栈int i;NODE data;for (i=0;i<num-1;i++){PopStcak(stop,&data);PushStcak(give,data);}//车出栈PopStcak(stop,&data);long int now = now_time();int time = now - data.time;int d = time/D;time = time%D;int h = time/H;time = time%H;int m = time/M;int s = time%M;float money = 20*d + 3*h + 0.1*m + 2;printf("谢谢本次停车!\n");printf("您停车时间为:%s\n",ctime(&(data.time)));printf("结束停车时间为:%s\n",ctime(&now));printf("总共停车%d天%d时%d分%d秒。收费%.1f元。欢迎下次光临铠源集团。\n\n",d,h,m,s,money);//车回停车栈for (i=0;i<num-1;i++){PopStcak(give,&data);PushStcak(stop,data);}//候车队停车if(wait->front != NULL){PopQueue(wait,&data);printf("欢迎车牌号为%s的车入场停车。\n",data.car);in_car(stop,data);printf("停车成功,祝您停车愉快!\n");}getchar();getchar();return TRUE;}//找到离开的车(返回:离车所要求车所需要出栈的次数)int find(STACK * stop,int x){int i = 0;int num = 0;int tmp = stop->top;int flag = 0;for(i=tmp;i>-1;i--){num++;if(stop->arr[i].id == x){flag = 1;break;}}if(flag == 0){printf("请输出正确的停车号,查无此车。\n");return 0;}return num;}//-------------------------------------------------------------查看车int Risplay(STACK * stop,QUEUE * wait){system("clear\n");if(stop->top == -1){printf("目前没有车停。\n");printf("系统在三秒后返回。\n");sleep(3);return FALSE;}int i;int max = SIZE_MAX;printf("本停车场一共有%d个车位。\n",max);printf("目前停车场有%d辆车。\n",stop->top+1);for(i=0;i<=stop->top;i++){look_car(stop,i);}NODEQUEUE *tmp = wait->front;if(tmp != NULL){printf("等候车辆:\n");while(tmp != NULL){printf("等候车牌号:%s\n\n",tmp->data.car);tmp = tmp->next;}}else{printf("没有等候车辆。\n");}getchar();getchar();}//查看一辆车的数据void look_car(STACK * stop,int x){printf("停车号:%d\n",stop->arr[x].id);printf("车牌号:%s\n",stop->arr[x].car);long int time = stop->arr[x].time;printf("进入停车场时间:%s\n\n",ctime(&(time)));}


最后主函数

/*2017年8月1日08:55:43停车场管理 问题描述:停车场是一个能放n辆车的狭长通道, 只有一个大门,汽车按到达的先后次序停放。若 车场满了,车要停在门外的便道上等候,一旦有 车走,则便道上第一辆车进入。当停车场中的车 离开时,由于通道窄,在它后面的车要先退出, 待它走后再依次进入。汽车离开时按停放时间收费。 基本功能要求: (1) 建立三个数据结构分别是:停放栈、让路 栈、等候队列。 (2) 输入数据模拟管理过程,数据(入或出,车号)。*/#include<stdio.h>#include<stdlib.h>#include"StackQueue.h"int main(){QUEUE* wait = Crate_queue();  //创建候车队列STACK* stop = (STACK *)malloc(sizeof(STACK)/sizeof(char)); //创建停车栈STACK* give = (STACK *)malloc(sizeof(STACK)/sizeof(char)); //创建让车栈Init_stack(stop);   //清空栈Init_stack(give);char *str[] = {"                        -----------------------------------------------\n","                       |               铠源集团停车管理系统            |\n","                       |                                               |\n","                       |                                               |\n","                       |      1:停车                 2:离车          |\n","                       |                                               |\n","                       |                                               |\n","                       |      3:看停车情况           4:退出          |\n","                       |                                               |\n","                       |                                               |\n","                        -----------------------------------------------\n"};while(1){system("clear\n");int i;for(i=0;i<11;i++){printf("%s",str[i]);}char ch;printf("请输出命令\n");scanf("%c",&ch);switch (ch){case '1':stop_car(wait,stop);break;case '2':leave_car(wait,stop,give);break;case '3':Risplay(stop,wait);break;case '4':return 0;default :break;}}return 0;}

原创粉丝点击