本停车场只有10个车位,只有1个大门,车停满了只能在候车便道等着,有车走了再能继续进入。另外,停车场中的车要离开时,由于通道窄,在它后面的车要先退出来,等它走后再依次进入(我不管,我的停车场就是这么傲娇,我宁愿请工人来帮车主开进开出的让道也不要花点钱扩大的通道)。
本来的要求是:用停放队列,让路栈和一个等候队列来实现停车,离开和查看停车情况的基本功能。但个人觉得,既然是后面的车要出来让路,那必然是停放栈啦,怎么可能是停放队呢,于是我便坚持己见。当然,不负众望,还是成功了。只不过,我的车牌号只能是int数字啊!这怎么可以嘛,于是苦思冥想,将栈中的一个数字车牌号数组改为了字符型车牌号二维数组,虽然过程很困难,但最终停车场终于能正常营业了(价钱高?不存在的,我还有那么多工人要养活呢)。
1、park.h
#ifndef __PARK_H__ #define __PARK_H__ #define MAX 10 // 栈 typedef struct _pack { char num[MAX][MAX]; int time[MAX]; int top; }Stack; // 便道节点 typedef struct _node { char bnum[MAX]; int btime; struct _node *next; }Node; // 便道队列 typedef struct _queue { Node *front; Node *rear; int count; }BianD; // 建便道队列 BianD* Create_BianD(); // 打印主界面 void interface(); // 置空栈 //int InitTingC(TingC *t); int InitStack(Stack *t); int InitStack(Stack *r); // 进入等候队列 int Wait (BianD *q, char* x, int btime); // 停车(入栈) int Pack (Stack *t, BianD *q); // 进入让路栈 int Tmp_Pack (Stack *r, char* x, int time); // 离开 int Leave (Stack *t, Stack *r, int *cost); // 重新进队 void RePack (Stack *t, Stack *r, BianD *q); // 带参数入栈 int getin (Stack *t, char* x, int time); // 查看停车状况 int display (Stack *t, Stack *r, BianD *q); // 销毁等待队列 int destroy (BianD *q); void quit (); void begin(); #endif 2、park.c #include "park.h" #include <time.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void interface() { printf("\t************ 天价停车场 ************\n"); printf("\t* *\n"); printf("\t* 1、停车 *\n"); printf("\t* 2、取车 *\n"); printf("\t* 3、查看停车状况 *\n"); printf("\t* 4、退出 *\n"); printf("\t* *\n"); printf("\t* By LYG *\n"); printf("\t************************************\n"); printf("\t请输入指令:"); } int InitStack1(Stack *t) { if (t == NULL) { errno = ERROR; return FALSE; } t->top = -1; return TRUE; } int InitStack2(Stack *r) { if (r == NULL) { errno = ERROR; return FALSE; } r->top = -1; return TRUE; } BianD* Create_BianD() { BianD * q = (BianD*)malloc(sizeof(BianD)/sizeof(char)); if (q == NULL) { errno = MALLOC_ERROR; return NULL; } // 置空队 q->count = 0; q->front = NULL; q->rear = NULL; return q; } int Wait (BianD *q, char *x, int btime) { if (q == NULL) { errno = ERROR; return FALSE; } Node * node = (Node*)malloc(sizeof(Node)/sizeof(char)); if (node == NULL) { errno = MALLOC_ERROR; return FALSE; } strcpy (node->bnum, x); node->btime = btime; 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 Pack (Stack *t, BianD *q) { char x[MAX]; printf ("\t请输入您的车牌号:"); scanf ("%s",x); getchar(); if (t == NULL) { errno = ERROR; return FALSE; } int tmp_time = (unsigned int)time(NULL); // 记录停车时间 // 判断是否停满 if (t->top == MAX-1) { Wait (q, x, tmp_time); printf ("\t停车场已满,车辆已驶入等待区\n"); printf ("\t您前方共有%d辆车\n\n", q->count-1); } else { // 车辆入栈 t->time[t->top+1] = tmp_time; strcpy(t->num[t->top+1], x); t->top++; printf ("\n\t停车成功!\n"); } return TRUE; } // 离开停车场 int Leave (Stack *t, Stack *r, int *cost) { char x[MAX]; if (t->top == -1) { printf ("\t停车场内尚无车辆,无法执行取车操作\n"); errno = EMPTY_STACK; return FALSE; } printf ("\t请输入您的车牌号:"); scanf ("%s",x); getchar(); if (t == NULL) { errno = ERROR; return FALSE; } while (strcmp(t->num[t->top],x)) { if (t->top == -1) { errno = ERROR; printf("\n\t车辆信息错误,请确认信息后重新输入\n"); return FALSE; } // 该车之前的车全部出栈,并进入让路栈 Tmp_Pack (r, t->num[t->top], t->time[t->top]); t->top--; } *cost = ((unsigned int)time(NULL) - t->time[t->top]); //该车出栈 t->top--; printf ("\n\t您总共停车 %d 分钟\n", *cost); printf("\t本次停车总共花费%d元,祝您出行安全!\n",(*cost)/2); return TRUE; } int Tmp_Pack (Stack *r, char *x, int time) { if (r == NULL) { errno = ERROR; return FALSE; } r->time[r->top+1] = time; strcpy(r->num[r->top+1], x); r->top++; return TRUE; } int getin (Stack *t, char *x, int time) { if (t == NULL) { errno = ERROR; return FALSE; } t->time[t->top+1] = time; strcpy(t->num[t->top+1], x); t->top++; return TRUE; } // 让路栈与等待队列车辆进入停车场 void RePack (Stack *t, Stack *r, BianD *q) { while (r->top != -1) { getin (t, r->num[r->top],r->time[r->top]); r->top--; } if (q->front != NULL) { int tmp_time = (unsigned int)time(NULL); Node *p = q->front; getin (t, p->bnum, tmp_time); q->front = p->next; free(p); q->count--; if (q->front == NULL) q->rear = NULL; } } int display (Stack *t, Stack *r, BianD *q) { if (t->top == -1) { printf ("\t停车场内尚无车辆\n"); return FALSE; } char str[50]; printf ("\t本停车场共有10个车位\n"); printf ("\t停车场内现停有%d辆车\n", t->top+1); printf ("\t等待区共有%d辆车\n\n", q->count); while (t->top != -1) { printf ("\t车牌号:%s\n",t->num[t->top]); printf ("\t停车时间:%d min\n\n",(unsigned int)time(NULL) - t->time[t->top]); Tmp_Pack (r, t->num[t->top], t->time[t->top]); t->top--; } while (r->top != -1) { getin (t, r->num[r->top],r->time[r->top]); r->top--; } } int destroy (BianD *q) { if (q == NULL) { errno = ERROR; return FALSE; } while (q->front != NULL) { Node *p = q->front; q->front = p->next; free(p); q->count--; if (q->front == NULL) q->rear = NULL; } free(q); return TRUE; } void begin() { system ("clear"); printf("\t************ 天价停车场 ************\n\n"); } void quit() { printf("\n\t***** 输入回车键退出:"); char c[50]; fgets(c,50,stdin); } 3、main.c #include <stdio.h> #include "park.h" #include <string.h> int main() { // 建停车栈 Stack t; // 建让路栈 Stack r; // 建等候队列 BianD* q = Create_BianD(); int i,k; char num[MAX] = "Q12A45T7E"; k = (unsigned int)time(NULL)-10; for (i = 2; i < 10; i += 2) { char num1[MAX]; strncpy (num1,num,i); printf("%s\n",num1); getin (&t, num1, k+i); } int cost = 0; char ord[2] = {0}; while (1) { system ("clear"); interface(); char str[50]; fgets (ord, 50, stdin); switch (ord[0]) { case '1': begin(); Pack (&t, q); quit(); break; case '2': begin(); Leave (&t, &r, &cost); RePack (&t, &r, q); quit(); break; case '3': begin(); display (&t, &r, q); quit(); break; case '4': destroy (q); return 0; } } return 0; }