学习日志 姓名:顾迎星 日期:2018 9.5
一.今日学习任务:编写停车场系统
二.今日任务完成情况
1.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 //bao cun che liang xin xi
{
char id[32]; //che pai hao
time_t t; //ting che shi jian
};
typedef struct CarInfo info;
struct Stack //ting che/rang lu zhan
{
info CarData[MAXSIZE];
int top;
};
typedef struct Stack stack;
struct Node //deng dai dui lie jie dian xin xi
{
char id[32];
struct Node *next; //zhi zhen yu
} ;
typedef struct Node node;
struct Queue
{
node *front; //dui tou zhi zhen
node *rear; //duiweizhizhen
};
typedef struct Queue queue;
void welcom();
void menu();
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);
int EmptyStack(stack *s);
char *pop(stack *s);
int EmptyQueue(queue *q);
char *DelQueue(queue *q);
#endif
2.park.c
#include <stdio.h>
#include "park.h"
void welcom()
{
system("clear"); //qing chu ping mu
printf("\n\n\n");
printf("**********\n");
printf("fdjnksmdkd\n");
printf("*************\n");
sleep(2);
}
void menu()
{
system("clear");
printf("\n\n\n");
printf("\t\t 1,tingche\n");
printf("\t\t 2,chu che\n");
printf("\t\t 3,chang nei che liang xin xi\n");
printf("\t\t 4,deng hou che liang xin xi\n");
printf("\t\t 5,tui chu xi tong\n");
}
void bye()
{
system("clear");
printf("\t\t\t byebye!!\n");
exit(1); //tui chu cheng xu
}
//chu shi hua ting che zhan ,rang lu zhan ,deng hou dui lie
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[32] = {0}; //bao cun che pai hao
int ret;
printf("shu ru che pai hao: \n");
scanf("%s",id);
ret = push(s,id);
if(ret == FAILURE)
{
printf("push failure!\n");
}
else if (ret == FULL) //zhan man
{
printf("ting che chang man ,jin ru deng hou qu.\n");
sleep (2);
EnterQueue(q, id);
}
}
void ShowParkInfo(stack *s)
{
if (NULL == s)
{
return;
}
if (s->top == -1)
{
printf("mei you che liang!\n");
sleep(2);
return;
}
int i;
for(i=0;i<=s->top;i++)
{
printf("che pai hao: %s\n",s->CarData[i].id);
printf("ting che shi chang: %d\n",(int)(time(NULL) - s->CarData[i].t));
printf("*********\n");
}
printf("Press Enter Continue..\n");
getchar();
}
void ShowWaitInfo(queue *q)
{
if (NULL == q)
{
return;
}
node *p = q->front->next;
while(p)
{
printf("che pai hao: %s\n",p->id);
p = p->next;
}
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 ("shu ru che pai hao: \n");
scanf("%d",id);
while(1)
{
if(!strcmp(s1->CarData[s1->top].id,id)) //che pai hao xiang deng
{
str = pop(s1); //che liang chu zhan
free(str);
while(EmptyStack(s2) != SUCCESS) //rang lu zhan bu wei kong
{
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("che liang bu cun zai! \n");
sleep(2);
while (EmptyStack(s2) != SUCCESS)
{
str = pop(s2);
push(s1,str);
free (str);
}
break;
}
}
}
3.main.c
#include "park.h"
int main()
{
char choice[16]={0};
stack park_stack, leaving_stack;
queue wait_queue;
welcom();
init(&park_stack,&leaving_stack,&wait_queue);//chu shi hua zhan he dui lie
while (1)
{
menu();
printf("xuan ze gong neng: \n");
memset(choice,0,8); //qing kong
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;
}
4.stack.c
#include "park.h"
int InitStack(stack *s)
{
if (NULL == s)
{
return FAILURE;
}
s->top = -1; //chu shi hua cheng kong zhan
return SUCCESS;
}
int push(stack *s, char *id)
{
if (NULL == s || NULL ==id)
{
return FAILURE;
} //ru can pan duan
if (s->top == MAXSIZE -1) //man le
{
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) //ru can ban duan
{
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;
}
5.queue.c
#include "park.h"
int InitQueue(queue *q)
{
if (NULL == q)
{
return FAILURE;
}
node *p = (node *)malloc(sizeof(node));//fen pei jie dian
if (NULL == p)
{
return FAILURE;
}
p->next = NULL;
//dui tou zhi zhen he dui wei zhi zhen tong shi zhi xiang tou jie dian
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); //bao cun che pai hao
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(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;
}
三.今日问题汇总
粗心,代码小错误多,打字太慢
四.今日未解决问题
没有。