苏嵌项目实战 学习日志4
姓名:吴谋升 日期:20180906
今日学习任务 | 结合前几天学习的指针、栈、队列等知识,设计一个停车场管理系统。 |
今日任务完成情况 (详细说明本日任务是否按计划完成, 开发的代码量) | 按计划完成了本日任务。 设计了一个停车场管理系统。 今日开发的代码量总计约为400行。 |
今日开发中出现的问题汇总 | 涉及到形参的地方常出现问题,不太清楚什么时候该用取地址符。 |
今日未解决问题 | 虽然在课堂上打出了代码,但代码的具体含义还没完全理解,课后还需继续复习。 |
今日开发收获 | 设计了一个停车场管理系统,具有停车、出车、车辆等候、场内车辆查询、等候车辆查询等功能。 |
自我评价 (是否按开发规范完成既定任务, 需要改进的地方,与他人合作效果等) | 按开发规范完成了既定任务; 课后还需继续复习课堂上学习的知识; 进步空间还很大,要继续努力。 |
其他 | 程序代码以及结果展示如下: |
目录
一、程序代码
1、main.c
#include "park.h"
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char choice[16] = {0};
stack park_stack,leaving_stack;
queue wait_queue;
welcome();
init(&park_stack,&leaving_stack,&wait_queue);//初始化栈和队列
while(1)
{
menu();
printf("\n请选择功能:\n");
memset(choice,0,8); //清空
scanf("%s",choice);
switch (choice[0])
{
case '1':
EnterPark(&park_stack,&wait_queue);
break;
case '2':
OutPark(&park_stack,&leaving_stack,&wait_queue);
break;
case '3':
ShowParkInfo(&park_stack);
break;
case '4':
ShowWaitInfo(&wait_queue);
break;
case '5':
bye();
break;
}
}
return 0;
}
2、park.c
#include "park.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
void welcome()
{
system("clear"); //清除屏幕
printf("\n\n");
printf("***********************************\n");
printf("*****正在进入停车系统,请稍等...***\n");
printf("***********************************\n");
sleep(1);
}
//************************************************
void menu()
{
system("clear");
printf("\n\n");
printf("\t 1、停车\n");
printf("\t 2、出车\n");
printf("\t 3、场内车辆信息\n");
printf("\t 4、等候车辆信息\n");
printf("\t 5、退出系统\n");
}
//************************************************
void bye()
{
system("clear");
printf("\t\t byebye!!\n");
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)
{
int ret;
char id[32] = {0}; //保存车牌号
printf("请输入车牌号:\n");
scanf("%s",id);
ret = push(s, id);
if (ret == FAILURE)
{
printf("push failure!\n");
}
else if (ret == FULL) //栈满
{
printf("停车场满,进入等候区。\n");
sleep(2);
EnterQueue(q,id);
}
}
//************************************************
void ShowParkInfo(stack *s)
{
if (NULL == s)
{
return;
}
if (s->top == -1)
{
printf("没有车辆!\n");
sleep(2);
return;
}
int i;
for (i = 0;i <= s->top;i++)
{
printf("\n车牌号:%s\n",s->CarData[i].id);
printf("停车时长:%d\n",(int)(time(NULL)- s->CarData[i].t));
printf("*************\n");
}
printf("Press Enter to Continue..\n");
getchar();
getchar();
}
//************************************************
void OutPark(stack *s1,stack *s2,queue *q)
{
char *str;
char id[32] = {0};
//int i = 0;
if (NULL == s1 || NULL == s2 || NULL == q)
{
return;
}
printf("输入要驶出停车场的车牌号:\n");
scanf("%s",id);
while (1)
{
if (!strcmp(s1->CarData[s1->top].id,id))//车牌号相同
{
str = pop(s1); //车辆出栈
free(str);
while (EmptyStack(s2) != SUCCESS)//让路栈不为空
{
str = pop(s2);
push(s1,str);
free(str);
}
if (EmptyQueue(q) != SUCCESS)
{
str = DelQueue(q);
push(s1,str);
free(str);
}
break;
}
else
{
str = pop(s1);
push(s2,str);
free(str);
}
if (EmptyStack(s1) == SUCCESS)
{
printf("车辆不存在!\n");
sleep(2);
while (EmptyStack(s2) != SUCCESS)//车辆回到停车栈
{
str = pop(s2);
push(s1,str);
free(str);
}
break;
}
}
}
//************************************************
3、stack.c
#include "park.h"
#include <stdio.h>
#include <stdlib.h>
int InitStack(stack *s)
{
if (NULL == s)
{
return FAILURE;
}
s->top = -1; //初始化成空栈
return SUCCESS;
}
//************************************************
int push(stack *s,char *id)
{
if (NULL == s || NULL == id)
{
return FAILURE;
} //入参判断
if (s->top == MAXSIZE - 1) //停车场满
{
return FULL;
}
strcpy(s->CarData[s->top + 1].id,id);
s->CarData[s->top + 1].t = time(NULL);
s->top++;
return SUCCESS;
}
//************************************************
char *pop(stack *s)
{
char *id = (char *)malloc(32);
if (NULL == id)
{
return NULL;
}
if (NULL == s)
{
return NULL;
}
if (s->top == -1)
{
return NULL;
}
strcpy(id,s->CarData[s->top].id);
s->top--;
return id;
}
//************************************************
int EmptyStack(stack *s)
{
if (NULL == s)
{
return FAILURE;
}
return (s->top == -1) ? SUCCESS : FAILURE;
}
//*************************************
4、queue.c
#include "park.h"
#include <stdio.h>
#include <stdlib.h>
int InitQueue(queue *q)
{
if (NULL == q)
{
return FAILURE;
}
node *p = (node *)malloc(sizeof(node));//分配头结点
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;
}
node *p = (node *)malloc(sizeof(node));
if (NULL == p)
{
return FAILURE;
}
strcpy(p->id, id); //保存车牌号
p->next = NULL;
q->rear->next = p;
q->rear = p;
return SUCCESS;
}
//************************************************
void ShowWaitInfo(queue *q)
{
if (NULL == q)
{
return;
}
node *p = q->front->next;
while (p) //while (p ! = NULL)
{
printf("等候车牌号:%s\n",p->id);
p = p->next;
}
getchar();
getchar();
}
//************************************************
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(32);
if (NULL == id)
{
return NULL;
}
node *p = q->front->next;
q->front->next = p->next;
strcpy(id,p->id);
free(p);
if (p == q->rear)
{
q->rear = q->front;
}
return id;
}
//************************************************
5、park.h
#ifndef PARK_H
#define PARK_H
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 5
#define SUCCESS 1000
#define FAILURE 1001
#define FULL 1002
struct CarInfo //保存车辆信息
{
char id[32]; //车牌号
time_t t; //停车时间
};
typedef struct CarInfo info;
struct Stack //停车/让路栈
{
info CarData[MAXSIZE];
int top;
};
typedef struct Stack stack;
struct Node //表示等候队列结点信息
{
char id[32];
struct Node *next; //指针域
};
typedef struct Node node;
struct Queue //等候队列
{
node *front; //队头指针
node *rear; //队尾指针
};
typedef struct Queue queue;
void welcome();
void menu();
void bye();
int InitQueue(queue *q);
int InitStack(stack *s);
void init(stack *s1,stack *s2,queue *q);
int EnterQueue(queue *q,char *id);
int push(stack *s,char *id);
void EnterPark(stack *s, queue *q);
void ShowParkInfo(stack *s);
void ShowWaitInfo(queue *q);
void OutPark(stack *s1,stack *s2,queue *q);
int EmptyStack(stack *s);
char *pop(stack *s);
int EmptyQueue(queue *q);
char *DelQueue(queue *q);
#endif
二、结果展示
1.欢迎界面
2.功能菜单选择界面
3.停车功能
4.场内车辆信息
5.等候车辆信息
6.出车功能
7.等候车辆信息
end