今日学习任务:
学习并编写关于停车场的代码 。
今日完成情况:
学习了整个项目的开发流程。
任务完成,完成量约400行。
今日开发中出现的问题汇总:
编写程序时不够快,容易出错 。
今日未解决问题:
车辆出车问题未解决 。
自我评价:
有了一个比较完整的编程思路。
学习到了很多新知识,能编写出程序,能找出错误 。
接下来继续努力,认真完成老师所给任务。
今日程序编写;
main.c
#include "park.h"
#include <stdio.h>
int main()
{
int choice;
stack park_stack,leave_stack; //停车、让路栈
queue wait_queue; //等候队列
welcome();
Init(&park_stack,&leave_stack,&wait_queue); //初始化
while(1)
{
menu();
scanf("%d",&choice);
switch(choice)
{
case 1:
EnterPark(&park_stack,&wait_queue);
break;
case 2:
LeavePark(&park_stack,&leave_stack,&wait_queue);
break;
case 3:
ShowParkInfo(&park_stack);
break;
case 4:
ShowWaitInfo(&wait_queue);
break;
case 5:
bye();
break;
default:
printf("输入有误\n");
break;
}
}
return 0;
}
park.c
#include "park.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
void welcome()
{
system("clear"); //清屏
printf("\n\n\n\n\n\n");
printf("\t\t\t*******************************\n");
printf("\t\t\t************WELCOME************\n");
printf("\t\t\t*******************************\n");
sleep(2);
}
void menu()
{
system("clear");
printf("\n\n\n");
printf("\t\t1、停车\n");
printf("\t\t2、出车\n");
printf("\t\t3、查看场内车辆信息\n");
printf("\t\t4、查看等候车辆信息\n");
printf("\t\t5、退出系统\n");
printf("请输入你的选择:");
}
void bye()
{
system("clear");
printf("\n\n\n\n\n\t\t\tbyebye...\n");
sleep(1);
system("clear");
exit(1);
}
void Init(stack *s1,stack *s2,queue *q)
{
int ret;
ret = InitStack(s1); //初始化停车栈
if(FAILURE == ret)
{
printf("Init Stack Failure!\n");
}
ret = InitStack(s2); //初始化让路栈
if(FAILURE == ret)
{
printf("Init Stack Failure!\n");
}
ret = InitQueue(q); //初始化等候队列
if(FAILURE == ret)
{
printf("Init Queue Failure!\n");
}
}
void EnterPark(stack *s,queue *q)
{
char id[10] = {0};
if(NULL == s||NULL == q)
{
return ;
}
printf("请输入车牌号:");
scanf("%s", id);
int ret = push(s,id);
if(ret == FAILURE)
{
printf("进场失败!\n");
}
else if(ret == FULL)
{
printf("停车场已满,进入等待队列\n");
sleep(1);
EnterQueue(q,id); //把车放在等候队列
}
else
{
printf("停车成功!\n");
sleep(1);
}
}
void ShowParkInfo(stack *s)
{
int i;
if(NULL == s)
{
return;
}
for(i=0;i<= s->top;i++)
{
printf("车牌号:%s\n",s->data[i].number);
printf("停车时长: %d\n",(int)(time(NULL) - s->data[i].park_time));
printf("****\n");
}
printf("Press Enter Continue...\n");
getchar();
getchar();
}
void ShowWaitInfo(queue *q)
{
if(NULL == q)
{
return;
}
if(EmptyQueue(q) == SUCCESS)
{
printf("等候队列没有车辆\n");
sleep(1);
}
else
{
car *p = q->front->next;
while(p)
{
printf("车牌号:%s\n",p->number);
p = p->next; //指针p指向下一辆车
}
printf("Press Enter Continue...\n");
getchar();
getchar();
}
}
void LeavePark(stack *s1,stack *s2,queue *q)
{
char id[10] = {0};
int i;
if(NULL == s1 || NULL == s2 || NULL == q)
{
return;
}
printf("请输入车牌号:");
scanf("%s", id);
int length =s1->top;
for(i = 0;i<= length;i++)
{
if(!strcmp(id, s1->data[s1->top].number)) //匹配到车辆
{
pop(s1); //匹配到的车辆出栈
while(EmptyStack(s2) != SUCCESS)
{
car c = pop(s2); //从让路栈出来
push(s1,c.number); //进入停车栈
}
/*如果等候队列不为空,则第一辆车进栈*/
if(EmptyQueue(q) != SUCCESS)
{
char *id = DelQueue(q);
push(s1,id);
free(id);
}
break;
}
else
{
car c = pop(s1); //不匹配则出栈并且进入让路栈
push(s2, c.number);
}
}
}
park.h
#ifndef PARK_H
#define PARK_H
#include <time.h>
#include <string.h>
#define MAXSIZE 5
#define SUCCESS 1000
#define FAILURE 1001
#define FULL 1002
struct carinfo
{
char number[10]; //车牌号
time_t park_time; //进场时间
struct carinfo *next; //指向下一辆车
};
typedef struct carinfo car;
struct stackinfo
{
car data[MAXSIZE]; //结构他数组
int top; //栈顶指针
};
typedef struct stackinfo stack;
struct queueinfo
{
car *front; //队头指针
car *rear; //队尾指针
};
typedef struct queueinfo queue;
void welcome();
void menu();
void bye();
void Init(stack *s1,stack *s2,queue *q);
int InitQueue(queue *q);
int InitStack(stack *s);
void EnterPark(stack *s,queue *q);
int push(stack *s,char *id);
int EnterQueue(queue *q,char *id);
void ShowParkInfo(stack *s);
void ShowWaitInfo(queue *q);
int EmptyQueue(queue *q);
void LeavePark(stack *s1,stack *s2,queue *q);
car pop(stack *s);
int EmptyStack(stack *s);
char *DelQueue(queue *q);
#endif
queue.c
#include "park.h"
#include <stdlib.h>
#include <string.h>
/*初始化队列*/
int InitQueue(queue *q)
{
if(NULL == q)
{
return FAILURE;
}
car *p = (car *)malloc(sizeof(car));
if(NULL == p)
{
return FAILURE;
}
p->next =NULL;
q->front = q->rear = p;
return SUCCESS;
}
int EnterQueue(queue *q,char *id)
{
if(NULL == q || NULL == id)
{
return FAILURE;
}
car *p = (car *)malloc(sizeof(car));
if(NULL == p)
{
return FAILURE;
}
strcpy(p->number,id);
p->next = NULL;
q->rear->next = p;
q->rear = p;
return SUCCESS;
}
/*判断队列是否为空*/
int EmptyQueue(queue *q)
{
if(NULL == q)
{
return FAILURE;
}
return (q->front == q->rear) ? SUCCESS : FAILURE;
}
/*出队操作,返回车牌号*/
char *DelQueue(queue *q)
{
if(NULL == q)
{
return NULL;
}
char *id =(char *)malloc(sizeof(char) * 10);
car *c = q->front->next;
if(c != NULL)
{
strcpy(id,c->number); //保存车牌号
q->front->next = c->next;
free(c);
}
if(c == q->rear) //如果队列只剩一辆车
{
q->rear = q->front;
}
return id;
}
stack.c
#include "park.h"
#include <string.h>
/*栈的初始化*/
int InitStack(stack *s)
{
if(NULL == s)
{
return FAILURE;
}
s->top = -1;
return SUCCESS;
}
/*进栈的操作*/
int push(stack *s,char *id)
{
if(NULL == s)
{
return FAILURE;
}
if(s->top == MAXSIZE - 1)
{
return FULL;
}
strcpy(s->data[s->top + 1].number,id); //拷贝车牌号
s->data[s->top + 1].park_time = time(NULL); //记录进场时间
s->top++;
return SUCCESS;
}
/*出栈操作*/
car pop(stack *s)
{
car c;
if(NULL == s)
{
return c;
}
if(s->top == -1)
{
return c;
}
strcpy(c.number, s->data[s->top].number);
c.park_time = s->data[s->top].park_time;
s->top--;
return c;
}
int EmptyStack(stack *s)
{
if(NULL == s)
{
return FAILURE;
}
return (s->top == -1) ? SUCCESS : FAILURE;