Linux学习之路(番外--停车场1.0)

停车场代码分为3个部分:park.c  ,  main.c  ,  park.h

分别为头文件,主函数和函数

park.h:

#ifndef _PARK_H_
#define _PARK_H_

#define SIZE 10

typedef enum {FALSE, TRUE} BOOL;

typedef struct _Data
{
    int num;
}Data;


typedef struct node
{
    Data data;
    struct node *next;
}Node;

typedef struct queue                    //定义队列类型                    
{
    Node *front;                        //队头指针
    Node *rear;                         //队尾指针
}Queue;

//初始化队列
void Init_Queue(Queue *q);

//判断空队列
BOOL Empty_Queue(Queue *q);

//入队列
void Push_Queue(Queue *q, Data data);

//出队列
void Pop_Queue(Queue *q);

//获取队头元素
Data GetTop_Queue(Queue *q);

typedef struct stack
{
    Data data[SIZE];                    //栈存储空间
    int top;                            //栈顶元素的下标
}Stack;


//初始化栈
void Init_Stack(Stack *s);

//判断空栈
BOOL Empty_Stack(Stack *s);

//判断满栈
BOOL Full_Stack(Stack *s);

//入栈
void Push_Stack(Stack *s, Data data);

//出栈
void Pop_Stack(Stack *s);

//获取栈顶元素
Data GetTop_Stack(Stack *s);

//创建顺序栈
Stack *CreatStack();

//创建链式队列
Queue *CreatQueue();

//停车
void Park(Stack *s, Queue *q);
    


#endif //_PARK_H_
 

 

main.c:

#include <stdio.h>
#include "Park.h"
#include <stdlib.h>
//

int main()
{
    Stack *park = CreatStack();                    //创建顺序栈park存放停车场信息
    Stack *turn = CreatStack();                    //创建顺序栈turn存放从停车场出来再进去的车辆信息
    Queue *hold = CreatQueue();                    //创建链式列表存放等待车辆
    
    
    
    while (1)
    {
        
        system ("clear");
        printf("           停车场管理系统            \n");
        printf("*************************************************\n");        
        printf("*        请选择你要进行的操作        *\n");
        printf("*        1、停车                *\n");
        printf("*        2、离开                *\n");
        printf("*        3、停车信息            *\n");
        printf("*        0、退出程序            *\n");
        printf("*************************************************\n");
        
        
        
        
        
        int tmp;
        scanf("%d", &tmp);
        switch(tmp)
        {
            case 1:
            {
                Park(park, hold);                    //停车
                break;
            }
            case 2:
            {
                Leave(park, turn, hold);            //离开
                break;
            }
            case 3:
            {
                Display(park,turn);                        //显示停车场信息
                break;
            }
            case 0:
            {
                return;
                break;
            }
            default:
            {
                printf("请输入正确指令\n");
                break;
            }
            
        }
        printf("按回车键返回菜单\n");
        getchar();
        getchar();
    
    }
    
    

    
    return 0;
}
    
 

 

park.c:

#include <stdlib.h>
#include "Park.h"
#include <stdio.h>

int wait = 0;


void Init_Queue(Queue *q)                        //初始化队列
{
    if (NULL == q)
        return;
    
    q->front = NULL;
    q->rear  = NULL;
}

BOOL Empty_Queue(Queue *q)                        //判断空队列
{
    if (NULL == q)
        return FALSE;
    
    if (NULL == q->front)
        return TRUE;
    
    return FALSE;
}

void Push_Queue(Queue *q, Data data)            //入队列
{
    if (NULL == q)
        return;
    
    Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
    if (NULL == node)
        return;
    
    node->data = data;
    node->next = NULL;
    if (q->rear != NULL)
    {
        q->rear->next = node;
        q->rear = node;
    }
    else
    {
        q->rear  = node;
        q->front = node;
    }
}

void Pop_Queue(Queue *q)                        //出队列
{
    if (NULL == q)
        return;
    
    if (Empty_Queue(q) == TRUE)
        return;
    
    Node *tmp = q->front;
    q->front = tmp->next;
    free(tmp);
    
    if (q->front == NULL)
        q->rear == NULL;
}

Data GetTop_Queue(Queue *q)                        //获取队头数据
{
    if (NULL == q)
        return;
    
    if (Empty_Queue(q) == TRUE)
        exit(-1);                                //程序退出
    
    return q->front->data;
}

void Init_Stack(Stack *s)                        //初始化栈
{
    if (NULL == s)
        return;
    
    s->top = -1;
}

BOOL Empty_Stack(Stack *s)                    //判断空栈
{
    if (NULL == s)
        return FALSE;
    
    if (-1 == s->top)
        return TRUE;
    
    return FALSE;
}

BOOL Full_Stack(Stack *s)                        //判断满栈
{
    if (NULL == s)
        return FALSE;
    
    if (SIZE - 1 == s->top)
        return TRUE;
    
    return FALSE;
}

void Push_Stack(Stack *s, Data data)            //压栈
{
    if(NULL == s)
        return;
    
    if(Full_Stack(s) == TRUE)
        return;
    
    s->data[++s->top] = data;
}

void Pop_Stack(Stack *s)                        //出栈
{
    if(NULL == s)
        return;
    
    if (Empty_Stack(s) == TRUE)
        return;
    
    --s->top;
}

Data GetTop_Stack(Stack *s)                        //获取栈顶元素
{
    if (NULL == s)
        return;
    
    if (Empty_Stack(s) == TRUE)
        exit(-1);                        //程序退出
    
    return s->data[s->top];
}
    
Stack *CreatStack()
{
    Stack *s = (Stack *)malloc(sizeof(Stack)/sizeof(char));
    if (NULL == s)
        return NULL;
    
    Init_Stack(s);
    
    return s;
}

Queue *CreatQueue()
{
    Queue *q = (Queue *)malloc(sizeof(Queue)/sizeof(char));
    if (NULL == q)
        return NULL;
    
    Init_Queue(q);
    
    return q;
}

void Park(Stack *s, Queue *q)
{
    if (NULL == s)
        return;
    
    int static numb = 1000;
    numb++;
    Data a;
    a.num = numb;
    
    if (Full_Stack(s) == FALSE)
    {
        Push_Stack(s, a);
        printf("欢迎光临,您的停车号为%d号\n",a.num);
    }
    else
    {
        Push_Queue(q, a);
        printf("欢迎光临,您的停车号为%d号\n",a.num);
        printf("不好意思,由于停车场已满,请您在等待区等待\n");
        printf("您前方还有%d车在等待区排队中\n", wait);
        wait += 1;
    }
        
}

void Leave(Stack *s1, Stack *s2, Queue *q)
{
    if (NULL == s1)
        return;
    if (NULL == s2)
        return;
    if (NULL == q)
        return;
    
    int i = 1;
    int j;
    printf("您要取出的车是第几号车位的车?(1~10)");
    scanf ("%d",&j);
    if (j >= 1 && j <= 10)
    {
        for (i = 1; i < j; i++)                                        //将第j号车位之前的车依次存放在调转区
        {
            Push_Stack(s2, GetTop_Stack(s1));                    
            Pop_Stack(s1);
        }
    
        Pop_Stack(s1);                                                //将第j号车取出
    
        for (i = 1; i < j; i++)                                        //将调转区的车依次放回停车场
        {
            Push_Stack(s1, GetTop_Stack(s2));
            Pop_Stack(s2);
        }
        
        if(Empty_Queue(q) == FALSE)                                    
        {
            Push_Stack(s1, GetTop_Queue(q));                        //将等待区的车依次放入停车场
            Pop_Queue(q);
            wait -= 1;
        }
    }
}

void Print_Date(Data data)
{
    printf("%d\n", data.num);
}

void Display(Stack *s1, Stack *s2)                                    //显示停车情况
{
    if (NULL == s1 || NULL == s2)
        return;
    

    
    printf("停车信息如下:\n");
    while(Empty_Stack(s1) == FALSE)
    {
        Data data = GetTop_Stack(s1);
        Print_Date(data);
        Push_Stack(s2, GetTop_Stack(s1));
        Pop_Stack(s1);
    }
    while(Empty_Stack(s2) == FALSE)
    {
        Push_Stack(s1, GetTop_Stack(s2));
        Pop_Stack(s2);
    }
    printf("等待中的车辆有%d辆\n", wait);
}
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值