苏嵌项目实战 学习日志4
姓名:杨子逊
日期: 2018/09/06
今日学习任务
学习编写停车场管理系统的程序。
今日任务完成情况
编写完成停车场管理系统的程序,初步运行测试结果符合预期。
课堂代码
main.c
#include "park.h"
int main(void)
{
char choice[16] = { 0 };
stack park_stack,leave_stack;
queue wait_queue;
welcome(); //欢迎界面
init(&park_stack, &leave_stack, &wait_queue); //初始化栈和队列
while (1)
{
menu();
printf("选择功能: ");
memset(choice, 0, 16); //清空
scanf("%s", choice);
printf("\n");
switch (choice[0])
{
case '1':
EnterPark(&park_stack, &wait_queue);
break;
case '2':
OutPark(&park_stack, &leave_stack, &wait_queue);
break;
case '3':
ShowParkInfo(park_stack);
break;
case '4':
ShowWaitInfo(wait_queue);
break;
case '5':
bye();
break;
default :
break;
}
}
return 0;
}
park.c
#include "park.h"
void welcome(void)
{
int i;
for (i = 0; i < 5; i++)
{
ClearScreen;
printf("\n\n\n");
printf("********************************\n");
printf("********** *********\n");
printf("********************************\n");
Sleep(100);
ClearScreen;
printf("\n\n\n");
printf("********************************\n");
printf("*** 欢迎进入停车管理系统 ***\n");
printf("********************************\n");
Sleep(100);
}
Sleep(2000);
}
void menu(void)
{
ClearScreen;
printf("\n\n\n");
printf("********************************\n");
printf("*** 停车管理系统菜单页面 ***\n");
printf("********************************\n");
printf(" *\n");
printf(" 1、停车 *\n");
printf(" *\n");
printf(" 2、出车 *\n");
printf(" *\n");
printf(" 3、场内车辆信息 *\n");
printf(" *\n");
printf(" 4、等候车辆信息 *\n");
printf(" *\n");
printf(" 5、退出系统 *\n");
printf(" *\n");
printf(" 计费标准:20元/秒 *\n");
printf("********************************\n");
}
void bye(void)
{
int i;
for (i = 0; i < 5; i++)
{
ClearScreen;
printf("\n\n\n");
printf("*********************************\n");
printf("********** ***********\n");
printf("*********************************\n");
Sleep(100);
ClearScreen;
printf("\n\n\n");
printf("*********************************\n");
printf("********** 再见 ***********\n");
printf("*********************************\n");
Sleep(100);
}
Sleep(1000);
exit(1); //退出程序
}
//初始化停车栈、让路栈、等候队列
void init(stack *s1, stack *s2, queue *q)
{
int ret;
ret = InitStack(s1);
if (ret == FAILURE)
{
printf("Init Park Stack Failure!\n");
Sleep(2000);
}
ret = InitStack(s2);
if (ret == FAILURE)
{
printf("Init Leave Stack Failure!\n");
Sleep(2000);
}
ret = InitQueue(q);
if (ret == FAILURE)
{
printf("Init Wait Queue Failure!\n");
Sleep(2000);
}
}
void EnterPark(stack *s, queue *q)
{
char id[32] = { 0 }; //保存车牌号
int ret;
printf("请输入车牌号: ");
scanf("%s", id);
printf("\n");
ret = push(s, id);
if (ret == FAILURE)
{
printf("Push Failure!\n");
Sleep(2000);
}
else if (ret == FULL)
{
printf("停车场满,进入等候区!\n");
printf("按回车键继续。。。\n");
EnterQueue(q, id);
getchar();
getchar();
}
}
void ShowParkInfo(stack s)
{
int i;
ClearScreen;
printf("\n\n\n");
printf("********************************\n");
printf("*** 停车场车辆信息 ***\n");
printf("********************************\n");
printf("\n");
if (s.top == -1)
{
printf("停车场没有车辆!\n");
printf("\n");
printf("按回车键回到菜单。。。\n");
getchar();
getchar();
return;
}
for (i = 0; i <= s.top; i++)
{
printf("车牌号: %s\n", s.CarData[i].id);
printf("已停车时间: %llu小时%llu分钟%llu秒\n", (time(NULL) - s.CarData[i].t)/3600,(time(NULL) - s.CarData[i].t)%3600/60, (time(NULL) - s.CarData[i].t)%3600%60);
printf("停车计费:%d元\n", (int)(time(NULL) - s.CarData[i].t) * 20);
printf("********************************\n");
}
printf("\n");
printf("按回车键回到菜单。。。\n");
getchar();
getchar();
}
void ShowWaitInfo(queue q)
{
ClearScreen;
printf("\n\n\n");
printf("********************************\n");
printf("*** 等候区车辆信息 ***\n");
printf("********************************\n");
printf("\n");
node *p = q.front->next;
while (p)
{
printf("车牌号: %s\n", p->id);
printf("********************************\n");
p=p->next;
}
printf("\n");
printf("按回车键回到菜单。。。\n");
getchar();
getchar();
}
void OutPark(stack *ps, stack *ls, queue *q)
{
carinfo* tmp;
char *str;
char id[32] = { 0 };
int i = 0;
if (NULL == ps || NULL == ls || NULL == q)
{
return;
}
printf("请输入车牌号: ");
scanf("%s", id);
printf("\n");
while (1)
{
if (!strcmp(ps->CarData[ps->top].id, id)) //如果车牌号相同
{
tmp = pop(ps);
free(tmp);
while (EmptyStack(*ls) != SUCCESS)
{
tmp = pop(ls);
push(ps, tmp->id);
ps->CarData[ps->top].t = tmp->t;
free(tmp);
}
if (EmptyQueue(*q) != SUCCESS)
{
str = DelQueue(q);
push(ps, str);
free(str);
}
break;
}
else
{
tmp = pop(ps);
push(ls, tmp->id);
ls->CarData[ls->top].t = tmp->t;
free(tmp);
if (EmptyStack(*ps) == SUCCESS)
{
while (EmptyStack(*ls) != SUCCESS)
{
tmp = pop(ls);
push(ps, tmp->id);
ps->CarData[ps->top].t = tmp->t;
free(tmp);
}
printf("相关车辆不存在!请按回车键返回菜单。。。\n");
getchar();
getchar();
break;
}
}
}
}
stack.c
#include "park.h"
int InitStack(stack *s)
{
if (NULL == s)
{
return FAILURE;
}
s->top = -1;
return SUCCESS;
}
int EmptyStack(stack s)
{
return (s.top == -1) ? SUCCESS : FAILURE;
}
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;
}
carinfo* pop(stack *s)
{
carinfo *tmp = (carinfo *)malloc(sizeof(carinfo));
if (NULL == s)
{
return NULL;
}
if (s->top == -1)
{
return NULL;
}
strcpy(tmp->id, s->CarData[s->top].id);
tmp->t = s->CarData[s->top].t;
s->top--;
return tmp;
}
queue.c
#include "park.h"
int InitQueue(queue *q)
{
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;
}
if (q->rear == NULL)
{
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;
}
int EmptyQueue(queue q)
{
return (q.front == q.rear) ? SUCCESS : FAILURE;
}
char* DelQueue(queue *q)
{
if (NULL == q)
{
return NULL;
}
if (q->front == q->rear)
{
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 (q->rear == p)
{
q->rear = q->front;
}
return id;
}
park.h
#ifndef PARK_H
#define PARK_H
#define WINDOWS 1
#ifdef WINDOWS
#include <windows.h>
#else
//#include <unistd.h>
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 5
#define SUCCESS 1000
#define FAILURE 1001
#define FULL 1002
#define ClearScreen system("cls")
typedef struct CarInfo //保存车辆信息
{
char id[32]; //车牌号
time_t t; //停车时间
}carinfo;
typedef struct Stack //停车/让路栈
{
carinfo CarData[MAXSIZE];
int top;
}stack;
typedef struct Node //表示等候队列结点信息
{
char id[32];
struct Node *next; //指针域
}node;
typedef struct Queue
{
node *front; //队头指针
node *rear; //队尾指针
}queue;
void welcome(void);
void menu(void);
void bye(void);
void init(stack *s1, stack *s2, queue *q);
int InitStack(stack *s);
int InitQueue(queue *q);
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 EmptyStack(stack s);
int EmptyQueue(queue q);
carinfo* pop(stack *s);
int push(stack *s, char *id);
char* DelQueue(queue *q);
#endif // !PARK_H
今日开发中出现的问题汇总
程序整体运行需要各个子函数进行协调,这些子函数之间的协调需要仔细运行调试。
今日开发收获
通过应用理解了C语言的各类数据结构与使用技巧。
自我评价
基本完成任务目标,课后仍需继续努力。