这几天 又写了一个停车场项目的程序 起初听到这样的名字 我感到很惊讶 程序难道也可以实现停车场里的一些
基本操作吗 简直不可思议 但是想想也对 现在的软件应用等各种电子信息平台或多或少都离不开程序的编写 所以写这
样的停车场程序是锻炼我们的思维及应用能力 其实想了许久以后觉得思路还是很好构造出来的 有了思路就要严谨的
去编写程序了 下面是一些主干思路:
(1)考虑到停车场的停车 让车 等候车列的安排 因此得需要有三个结构体来存放 停车和让车有着先进后出的特点
所以准备放在两个栈数组里 而候车则是先进先出 并且数量不知 因而放在一个循环队列最好了 接着就是给所有的栈和
队列进行初始化操作 并且要做好空栈满栈的判断的准备
(2)停车场里的每辆车都会有自己的停放时间 这里我在网上查找了相关资料 用这样一个函数调用来实现车辆的计时
功能:tingtime() t ;t = time(NULL); 为了方便显示出每辆车的标号和时间显示还得定义一个专门存放这两个变量的
结构体放在栈和队列之中
(3)做好停车场的基本界面 让人有一种一目了然的感觉 给别人进行选择的方式:停车 查看 离开 退出等自己想到的
一些基本需求选择
(4)开始写主函数 为了使程序一直处在运行之中 需要一个大的while循环 循环之中采用switch case 结构来对主界面
中客户的选择进行一个“承接”,这里有些个人喜欢的小应用 清屏:system(“clear”) 睡眠 :sleep(n) 以及各种
操作提示之后的小图案来使得整个界面与众不同 更加吸引人的眼球 程序中设置的礼貌用语要用的到位 让人听后很欣
慰舒心 做到人性化的服务
(5)程序中还有很多的细节值得你去注意 下面就是我自己写的程序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 10
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
typedef int DATATYPE ;
typedef struct
{
DATATYPE num ; //停车序号
DATATYPE time ; //停车时间
}Car ;
typedef struct
{
Car data[STACK_SIZE] ; //停车栈数组
int top1 ; //停车栈栈顶下标
}TingChe ;
typedef struct
{
Car data[STACK_SIZE] ; //让车栈数组
int top2 ; //让车栈栈顶下标
}RangChe ;
typedef struct node
{
DATATYPE data ; //候车队列结点数据
struct node *next ; //结点链指针
}HouChe ;
typedef HouChe * PHouChe ;
typedef struct
{
PHouChe rear ; //队尾指针
PHouChe front ; //队头指针
}HouZhiZhen ;
//停车栈置空
int InitStack1 (TingChe * p)
{
if(p == NULL)
{
return ERROR ;
}
p->top1 = -1 ;
return OK ;
}
//判断停车栈空栈
int StackEmpty1(TingChe * p)
{
if(p == NULL)
{
return ERROR ;
}
return p->top1 == -1 ;
}
//判断停车栈满栈
int StackFull1(TingChe * p)
{
if(p == NULL)
{
return ERROR ;
}
return p->top1 == STACK_SIZE-1 ;
}
//让车栈置空
int InitStack2 (RangChe * r)
{
if(r == NULL)
{
return ERROR ;
}
r->top2 = -1 ;
return OK ;
}
//判断让车栈空栈
int StackEmpty2 (RangChe * r)
{
if(r == NULL)
{
return ERROR ;
}
return r->top2 == -1 ;
}
//判断让车栈满栈
int StackFull2(RangChe * r)
{
if(r == NULL)
{
return ERROR ;
}
return r->top2 == STACK_SIZE-1 ;
}
//置空候车队列
int InitQueue(HouZhiZhen * h)
{
if(h == NULL)
{
return ERROR ;
}
h->rear = NULL ;
h->front = NULL ;
return OK ;
}
//判断候车队列是否空队
int QueueEmpty(HouZhiZhen * h)
{
if(h == NULL)
{
return ERROR ;
}
return h->front == NULL ;
}
//封面
void Interface()
{
printf("\n\n");
printf("\t| ******************************************************** |\n");
printf("\t| * 欢迎来到停车场界面! * |\n");
printf("\t| * ( WELCOME TO PARKING LOT INTREFACE !) * |\n");
printf("\t| * * |\n");
printf("\t| * * |\n");
printf("\t| * (A) 停 车 * |\n");
printf("\t| * * |\n");
printf("\t| * (B) 离 开 * |\n");
printf("\t| * * |\n");
printf("\t| * (C) 查 看 * |\n");
printf("\t| * * |\n");
printf("\t| * (D) 退 出 * |\n");
printf("\t| * * |\n");
printf("\t| * * |\n");
printf("\t| ******************************************************** |\n");
printf("\t| |\n");
printf("\t| |\n");
}
//获取当前停车时间
int tingtime()
{
time_t t ;
t = time(NULL);
return t ;
}
//将车停入停车场
int Push1(TingChe * p, DATATYPE m , DATATYPE time )
{
if(p == NULL)
{
return ERROR ;
}
if(StackFull1(p))
{
return ERROR ;
}
p->top1++ ;
p->data[p->top1].num = m ;
p->data[p->top1].time = time ;
return OK ;
}
//将车停入候车场
int Push3(HouZhiZhen * h ,DATATYPE m)
{
if(h == NULL)
{
return ERROR ;
}
PHouChe node = (PHouChe)malloc(sizeof(HouChe)/sizeof(char));
if(node == NULL)
{
return MALLOC_ERROR ;
}
node->data = m ;
node->next = NULL ;
if(QueueEmpty(h))
{
h->front = node;
h->rear = node;
}
else
{
h->rear->next = node;
h->rear = node;
}
return OK;
}
Car kongche_information = {-5,0};
//候车场车进入停车栈
int DeQueue(HouZhiZhen * h)
{
if(h == NULL)
{
return ERROR ;
}
if(QueueEmpty(h))
{
return -5;
}
PHouChe p = h ->front ;
int data = p->data ;
h->front = p->next ;
if(h->front == NULL)
{
h->rear = NULL ;
}
return data ;
}
//将车离开停车场
Car Pop1(TingChe * p)
{
if(p == NULL)
{
return ;
}
if(StackEmpty1(p))
{
return kongche_information ;
}
Car data1 ;
data1 = p->data[p->top1] ;
p->top1-- ;
return data1 ;
}
//将车停入让车栈
int Push2(RangChe * r ,Car data)
{
if(r == NULL)
{
return ERROR ;
}
if(StackFull2(r))
{
return ERROR ;
}
r->top2++;
r->data[r->top2].num = data.num ;
r->data[r->top2].time = data.time ;
return OK ;
}
//让车栈的车出栈
Car Pop2(RangChe * r)
{
if(r == NULL)
{
return ;
}
if(StackEmpty2(r))
{
return kongche_information ;
}
Car data1 = r->data[r->top2] ;
r->top2-- ;
return data1 ;
}
//主函数
int main()
{
TingChe park ;
if(InitStack1(&park)!=OK)
{
return -1 ;
}
RangChe rang ;
if(InitStack2(&rang)!=OK)
{
return -1 ;
}
HouZhiZhen hou ;
if(InitQueue(&hou) != OK)
{
return -1 ;
}
int run = 1 ;
char i[100] ;
int m = 1 ;
int v = 0 ;
int j ;
char choice[2] ;
int count = 0 ;
Car data ;
Car a[10];
int k ;
int count1 ;
while(run)
{
Interface() ;
printf("\t| |\n");
printf("\t| 请输入您选择的编号: ");
scanf("%s",choice) ;
switch(choice[0])
{
case 'A' :
{
system("clear") ;
printf("\n") ;
if(Push1(&park,m,tingtime()) != OK)
{
system("clear") ;
printf("\n") ;
printf("\t\t对不起 当前停车位已满 第%d号车已进入候车位!\n",m);
if(Push3(&hou,m)!= OK)
{
return ERROR ;
}
printf(" 返回主界面 请按任意键 :");
scanf("%s",i);
}
else
{
system("clear") ;
printf("\n") ;
printf("\t\t恭喜您 %d号豪车已成功进入停车车位!\n\n\n\n\n",m);
count++ ;
printf(" * * * * ");
printf(" * * * * ");
printf(" * * *** *** * * ");
printf(" * * * * ");
printf(" * * * * ");
printf(" * * * * * * ");
printf(" * * ***** * * ");
printf(" * * * * " );
printf(" \n\n\n\n\n\n\n");
printf(" 返回主界面 请按任意键 :");
scanf("%s",i);
}
m++;
break ;
}
case 'B' :
{
system("clear") ;
printf("\n\n\n\n") ;
printf("请输入您选择的车号离开停车场:");
scanf("%d", &v);
count1 = count ;
int temp = 1 ;
while(count1--)
{
data = Pop1(&park) ;
if(data.num != v)
{
Push2(&rang ,data) ;
}
else
{
temp = 0 ;
while((data = Pop2(&rang)).num != kongche_information.num )
{
Push1(&park,data.num,data.time) ;
}
k = DeQueue(&hou);
if(k != kongche_information.num)
{
Push1(&park,k,tingtime());
count++;
}
count1 = 0 ;
}
}
if(temp == 1)
{
printf("\n\n\n");
printf("\t\t对不起 停车场没有您选择的%d号车",v);
while((data = Pop2(&rang)).num != kongche_information.num)
{
Push1(&park,data.num,data.time);
}
}
else
{
printf("\n\n\n") ;
printf("\t\t\t恭喜您 成功开走了%d号豪车!",v);
count-- ;
}
printf(" * * * * * *");
printf(" * * * * * * ");
printf(" * * * * * * ");
printf(" * * * * * ");
printf(" * * * * * * * * * * ");
printf(" * * * ");
printf(" * * * ");
printf(" * * * * ");
printf(" * * * \n\n\n");
printf(" 返回主界面 请按任意键 :");
scanf("%s",i);
break ;
}
case 'C' :
{
system("clear");
printf("\n\n\n\n\n\n");
printf("豪车停车状态:\n\n\n\n\n");
printf("车号:");
for(j=0;j<count;j++)
{
a[j] = Pop1(&park) ;
printf("%8d",a[j].num);
}
printf("\n\n\n") ;
printf("时间: ");
for(j=0;j<count;j++)
{
printf("%8d",tingtime()-a[j].time);
}
for(j=count-1;j>=0;j--)
{
Push1(&park,a[j].num,a[j].time);
}
printf("\n\n\n\n\n\n\n") ;
printf(" 返回主界面 请按任意键 :");
scanf("%s",i);
break ;
}
case 'D' :
{
system("clear") ;
printf(" \n\n\n * * * * * *");
printf(" * * * * * * ");
printf(" * * * * * * ");
printf(" * * * * * ");
printf(" * * * * * * * * * * ");
printf(" * * * ");
printf(" * * * ");
printf(" * * * * ");
printf(" * * * ");
printf("\t\t\t\t欢迎下次光临!祝您旅途愉快,一路顺风!\n");
printf(" ***");
printf(" ***");
printf(" ***");
printf(" ***");
printf(" ***");
printf(" *** ");
printf(" *** ***");
printf(" *** ***");
printf(" *** ***");
printf(" *** ***");
printf(" ***");
run = 0 ;
printf("\n") ;
break ;
}
default :
{
system("clear") ;
printf("\n\n\t\t对不起 您的操作有误 请重试");
printf(" * * * * * *");
printf(" * * * * * * ");
printf(" * * * * * * ");
printf(" * * * * * ");
printf(" * * * * * * * * * * ");
printf(" * * * ");
printf(" * * * ");
printf(" * * * * ");
printf(" * * * \n\n\n");
printf(" \n\n\t\t返回停车场主界面 请按任意键 :");
scanf("%s",i);
break ;
}
}
}
return 0 ;
}