苏嵌实训 学习日志4

本文介绍了一个基于堆栈和队列的停车场管理系统的设计与实现,通过使用C语言开发,实现了车辆的出入管理、等待队列和停车场内车辆信息显示等功能。

学习日志 姓名:李金泉 日期:2018.9.6

今日学习任务
学习编写关于停车场车辆出入,停留的知识。

今日任务完成情况
成功完成关于停车场车辆信息记录的任务。

(详细说明本日任务是否按计划完成,开发的代码量)
main.c


#include "park.h"

int main()
{
   char choice[16] = {0};
   stack park_stack, leaving_stack;
   queue wait_queue;

   welcome();
   init(&park_stack, &leaving_stack, &wait_queue);//chishihuazhanduilie

   while (1)
   {
   menu();
   printf("xuanzegongneng: \n");
   memset(choice, 0 , 8);  //qingkong
   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;
}

park.c

#include <stdio.h>
#include "park.h"

void  welcome()
{
     system("clear");
     printf("\n\n\n");
     printf("**********************\n");
     printf("*****huanying********\n");
     printf("***********************\n");
     sleep(2);

}

void menu()
{
     system("clear");
     printf("\n\n\n");
     printf("\t\t 1.tingche\n");
     printf("\t\t 2.chuche\n");
     printf("\t\t 3.changneicheliangxinxi\n");
     printf("\t\t 4.denghoucheliangxinxi\n");
     printf("\t\t 5.tuichuxitong\n");
}

void bye()
{
  system("clear");
  printf("\t\t\t byebye!!\n");
  exit(1);  //tuichuchengcu
}
//chushihuatingcheian,rangluxian,denghouxian
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};
   int ret;

   printf("shuruchepaihao: \n");
   scanf("%s", id);

   ret = push(s, id);
   if (ret == FAILURE)
   {
     printf("push failure!\n");

   }
   else if (ret == FULL)  //zhanman
   {   
      printf("tingchechangman,jingrudenghouqu.\n");
      sleep(2);
      EnterQueue(q, id);
   }
}

void ShowParkInfo(stack *s)
{
   if (NULL == s)
   {
       return;
   }
   if (s->top == -1)
   {
      printf("meiyoucheliang! \n");
      sleep(2);
      return;

   }

   int i;
   for (i = 0; i <= s->top; i++)
   {
     printf("chepaihao: %s\n", s->CarData[i].id);
     printf("tingcheshichang: %d\n", (int)(time(NULL) - s->CarData[i].t));
     printf("*********\n");
     printf("Press Enter Continue..\n");
     getchar();
     getchar();
   }
 }  
void ShowWaitInfo(queue *q) 
{
   if (NULL == q)
   {
   return;

   }
   node *p = q->front->next;
   while (p)  //while (p != NULL)
   {
      printf("chepaihao: %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("shuruchepaihao: \n");
   scanf("%s", id);

   while (1)
   {
      if (!strcmp(s1->CarData[s1->top].id, id))  //chepaihaoxiangtong
      {
        str = pop(s1);  //cheliangchuzhan
        free(str);
      while (EmptyStack(s2) != SUCCESS) //rangluzhanbunengweikong
        {
          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("cheliangbucunzai! \n");
        sleep(2);

        while (EmptyStack(s2) != SUCCESS)  //suoyouchelianghuidaotingchezhan
        {
          str = pop(s2);
        push(s1, str);
        free(str);
        }
        break;
      }
   }
}

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;  //zhizhenyu

};
typedef struct Node node;

struct Queue
{
  node *front;  //duitouzhizhen
  node *rear;  //duiweizhizhen

};
typedef struct Queue queue;
void  welcome();
void menu();
void bye();
int InitQueue(queue *q);
void init(stack *s1, stack *s2, queue *q);
int InitStack(stack *s);
int EnterQueue(queue *q, char *id);
int push(stack *s, char *id);
void ShowParkInfo(stack *s);
void ShowWaitInfo(queue *q);
int EmptyStack(stack *s);
char *pop(stack *s);
int EmptyQueue(queue *q);
char *DelQueue(queue *q);
void OutPark(stack *s1, stack *s2, queue *q);
#endif

queue.c

#include "park.h"

int InitQueue(queue *q)
{
    if (NULL == q)
    {
       return FAILURE;
    }
    node *p = (node *)malloc(sizeof(node));  //fenpeitoujiedian
    if (NULL == p)
    {
       return FAILURE;
    }
    p->next = NULL;
    //duitouzhizhen he duiweizhizhentongshizhixiangtoujiedian
    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);  //baocunchepaihao
    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;
}

stack.c

#include "park.h"

int InitStack(stack *s)
{
   if (NULL == s)
   {
       return FAILURE;
   }
   s->top = -1;  //chushihuachengkongzhan

   return SUCCESS;
}

int push(stack *s, char *id)
{
   if (NULL == s || NULL == id)
   {
      return FAILURE;

   }  //rucanpanduan

   if (s->top == MAXSIZE - 1)  //man
   {
      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;
}

这里写图片描述

今日开发中出现的问题汇总
容易把括号漏掉,打字很慢。

今日未解决问题
已完成。。。

今日开发收获
稍微了解了一点关于堆栈队列的知识。

自我评价
继续努力!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值