简易停车场

来源:互联网 发布:小米 查看网络制式 *# 编辑:程序博客网 时间:2024/04/19 06:49

问题描述:停车场是一个能放辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。基本功能要求如图:


我准备用两个顺序栈和链式队列去完成这个小作业,下面是我的程序:

头文件部分:parking.h

#ifndef __PARKING_H__#define __PARKING_H__#include <time.h>#define SIZE  2#define FALSE   0#define TRUE    1 typedef struct _information{char  name[SIZE];char  num[SIZE];time_t    now_time;time_t     end_time;}Infor;// 停车栈和让路栈typedef struct _stack{Infor data[SIZE];  // 栈数组int top;           // 栈顶元素下标}Stack;// 链式队列-- 等候区typedef struct _node{Infor data;struct _node *next;}Node;typedef struct _queue{Node *front;Node *rear;int count;}Queue;// 置停车空栈int InitStack1 (Stack *t); // 置让路空栈int InitStack2 (Stack *r); // 判栈是否空栈int StackEmpty (Stack *t);// 判栈是否栈满int StackFull (Stack *t);  // 进栈--停车int stop (Stack *t,Queue *q);// 停车栈进栈int t_push(Stack *t, Infor x);// 停车栈出栈int t_pop (Stack *t, Infor *x); // 让路--进栈int r_push (Stack *t,Infor x); // 让路栈出栈int r_pop (Stack *r, Infor *x); // 创建等候队列Queue* Create_Queue();// 判空队int QueueEmpty (Queue *q);   // 进队int EnQueue (Queue *q);  // 出队int DeQueue (Queue *q, Infor *x); // 显示停车场信息void Display (Stack *t,Queue *q);// 主界面void interface();#endif 


parking.h

#include <stdio.h>#include <stdlib.h>#include "parking.h"#include <time.h>#include <stdlib.h>// 置停车空栈int InitStack1 (Stack *t){if (t == NULL){return FALSE;}t->top = -1;   }// 置让路空栈int InitStack2 (Stack *r){if (r == NULL){return FALSE;}r->top = -1;  } // 判停车栈是否空栈int StackEmpty (Stack *t){if (t == NULL){return FALSE;}return t->top == -1;}// 判停车栈是否满int StackFull (Stack *t){if (t == NULL){return FALSE;}return t->top == (SIZE-1);}// 进队int EnQueue (Queue *q){if (q == NULL){return FALSE;}Node * node = (Node*)malloc(sizeof(Node)/sizeof(char));if (node == NULL){return FALSE;}printf ("请输入姓名\n");scanf ("%s",node->data.name);printf ("请输入车牌号\n");scanf ("%s",node->data.num);node->next = NULL;if (q->front == NULL){q->front = node;q->rear  = node;}else{q->rear->next = node;q->rear = node;}q->count++;return TRUE;}    // 停车 int stop (Stack *t,Queue *q){if (t == NULL){return FALSE;}if (StackFull(t)){EnQueue (q);printf ("停车场已满,已经停到等候区\n");}else {t->top++;printf ("请输入姓名\n");scanf ("%s",&(t->data[t->top].name));    printf ("请输入车牌号\n");scanf ("%s",&(t->data[t->top].num));//t->data[t->top+1] = name;//t->data[t->top+1] = num;t->data[t->top].now_time = time(NULL);//t->top++;printf ("已经停车成功\n");}return TRUE;}// 停车栈进栈int t_push(Stack *t, Infor x){if (t == NULL){return FALSE;}// 判断是否满栈if (StackFull(t)){return FALSE;}t->data[++t->top] = x;return TRUE;}// 停车栈出栈int t_pop (Stack *t, Infor *x){if (t == NULL){return FALSE;}// 判断是否空栈if (StackEmpty(t)){return FALSE;}*x = t->data[t->top];t->top--;return TRUE;}// 让路栈进栈int r_push (Stack *r,Infor x){if (r == NULL){return FALSE;}// 判断是否满栈if (StackFull(r)){return FALSE;}r->data[++r->top] = x;return TRUE;}// 让路栈出栈int r_pop (Stack *r, Infor *x){if (r == NULL){return FALSE;}// 判断是否空栈if (StackEmpty(r)){return FALSE;}*x = r->data[r->top];r->top--;return TRUE;}// 出等候队int DeQueue (Queue *q, Infor *x){if (q == NULL){return FALSE;}if (QueueEmpty(q)){return FALSE;}Node *p = q->front;*x = p->data;q->front = p->next;free(p);q->count--;if (q->front == NULL)q->rear = NULL;return TRUE;}// 离开停车场int leave (Stack *t,Stack *r,Queue *q){if (t == NULL && t == NULL && q == NULL){return FALSE;}// 判停车栈是否为空if (StackEmpty(t)){printf ("停车场没有车\n");return FALSE;}char num[SIZE];printf ("请输入要离开的车牌号\n");scanf ("%s",num);Infor x;int i;int flag = 1;  // 0代表有这辆车 1代表没有for (i = 0;i <= t->top;i++){if(strcmp(t->data[i].num ,num) == 0){flag = 0;break;}}if (flag == 1){printf ("输入数据错误,没有这辆车\n");    return FALSE;}while(t->top != -1){if(strcmp(t->data[t->top].num ,num)){t_pop(t,&x);r_push(r,x);}else{break;}}t->data[t->top].end_time = time(NULL);int time = (int)difftime(t->data[t->top].end_time, t->data[t->top].now_time);int cost =time * SIZE;printf ("您一共停了: %d 秒\n",time);printf ("您一共消费: %d 元\n",cost);t_pop(t,&x);printf ("您的车已离开车库\n");// 让路栈车进停车栈while (r->top != -1){r_pop(r,&x);t_push(t,x);}// 判等候队列有没有车,有车就进来一辆if (QueueEmpty(q)){printf ("等候区没有车\n");}else{DeQueue(q, &x);t_push(t, x);}}// 创建等候队列Queue* Create_Queue(){Queue * q = (Queue*)malloc(sizeof(Queue)/sizeof(char));if (q == NULL){return NULL;}// 置空队q->front = NULL;q->rear  = NULL;return q;}// 判空队int QueueEmpty (Queue *q){if (q == NULL){return FALSE;}return q->front == NULL;} //显示车辆信息void Display (Stack *t,Queue *q){int i;printf ("停车场一共3个车位\n");printf ("停车场里有 %d 辆车\n",t->top+1);printf ("等待区里有 %d 辆车\n",q->count);int shijian = (unsigned int)time(NULL);for (i = 0;i <= t->top;i++){printf ("停车姓名:%s\n",t->data[i].name);printf ("车牌号:%s\n",t->data[i].num);printf ("停车时间:%d s\n ", shijian - t->data[i].now_time);}}// 主界面void interface(){system ("clear");printf("\t\t|--------------------------------------|\n");printf("\t\t|**********停了还想停的停车场**********|\n");printf("\t\t|**************************************|\n");printf("\t\t|------welcome to our car parking------|\n");printf("\t\t|1----------停车-----------------------|\n");printf("\t\t|2----------离开-----------------------|\n");printf("\t\t|3----------查看停车场停车状况---------|\n");printf("\t\t|4----------退出-----------------------|\n");printf("\t\t|--------------------------------------|\n");}

main.c

#include <stdio.h>#include "parking.h"#include <stdlib.h>int main(){int x;Stack t;if (InitStack1(&t) == FALSE){printf ("置空失败\n");}Stack r;if (InitStack2(&r) == FALSE){printf ("置空失败\n");}// 建等候队列Queue *q = Create_Queue();while (1){interface();printf("请输入数字1~4 :");scanf ("%d",&x);switch (x){case 1:stop (&t,q); break;case 2:leave (&t,&r,q); break;case 3:Display(&t,q);break;case 4:exit(4);break;}printf ("请随便输入一个字符返回主界面\n");char ch;scanf ("%c",&ch);scanf ("%c",&ch);}return 0;}