1. 程式人生 > >今天教大家寫一個停車場的程式

今天教大家寫一個停車場的程式

學習日誌4 姓名:繆志超 日期:2018.9.13

今日學習任務 編寫具有定時功能停車場的程式

今日任務完成情況: 課上程式已經基本全部完成,大部分功能都已經實現

今日問題 在停車出車的程式部分還有一些問題,需要明天來修改下!

5個子程式: 第一個檔名: main.c

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

int main()
{
    int choice; 
    stack park_stack,leave_stack; //停車、讓路棧
    queue wait_queue;            //等候佇列
welcome(); Init(&park_stack,&leave_stack,&wait_queue); //初始化 while(1) { menu(); scanf("%d",&choice); switch(choice) { case 1: EnterPark(&park_stack,&wait_queue); break; case
2: LeavePark(&park_stack,&leave_stack,&wait_queue); break; case 3: ShowParkInfo(&park_stack); break; case 4: ShowWaitInfo(&wait_queue); break; case
5: bye(); break; default: printf("輸入有誤\n"); break; } } return 0; }

第二個檔名:park.c

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

void welcome()
{
    system("clear");      //清屏

    printf("\n\n\n\n\n\n");
    printf("\t\t\t*******************************\n");
    printf("\t\t\t************WELCOME************\n");
    printf("\t\t\t*******************************\n");
    sleep(2);
}

void menu()
{
    system("clear");
    printf("\n\n\n");
    printf("\t\t1、停車\n"); 
    printf("\t\t2、出車\n");
    printf("\t\t3、檢視場內車輛資訊\n");
    printf("\t\t4、檢視等候車輛資訊\n");
    printf("\t\t5、退出系統\n");
    printf("請輸入你的選擇:");
}

void bye()
{
    system("clear");

    printf("\n\n\n\n\n\t\t\tbyebye...\n");
    sleep(1);
    system("clear");
    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)
{
    char id[10] = {0};
    if(NULL == s||NULL == q)
    {
        return ;
    }

    printf("請輸入車牌號:");
    scanf("%s", id);

    int ret = push(s,id);
    if(ret == FAILURE)
    {
        printf("進場失敗!\n");
    }
    else if(ret == FULL)
    {
        printf("停車場已滿,進入等待佇列\n");
        sleep(1);
        EnterQueue(q,id);    //把車放在等候佇列
    }
    else
    {
        printf("停車成功!\n");
        sleep(1);
    }
}

void ShowParkInfo(stack *s)
{
    int i;
    if(NULL == s)
    {
        return;
    }
    for(i=0;i<= s->top;i++)
    {
        printf("車牌號:%s\n",s->data[i].number);
        printf("停車時長: %d\n",(int)(time(NULL) - s->data[i].park_time));

        printf("****\n");
    }
    printf("Press Enter Continue...\n");
    getchar();
    getchar();
}

void ShowWaitInfo(queue *q)
{
    if(NULL == q)
    {
        return;
    }

    if(EmptyQueue(q) == SUCCESS)
    {
        printf("等候佇列沒有車輛\n");
        sleep(1);
    }
    else
    {
        car *p = q->front->next;
        while(p)
        {
            printf("車牌號:%s\n",p->number);
            p = p->next;                //指標p指向下一輛車
        }
        printf("Press Enter Continue...\n");
        getchar();
        getchar();
    }
}

void LeavePark(stack *s1,stack *s2,queue *q)
{
    char id[10] = {0};
    int i;
    if(NULL == s1 || NULL == s2 || NULL == q)
    {
        return;
    }

    printf("請輸入車牌號:");
    scanf("%s", id);

    int length =s1->top;
    for(i = 0;i<= length;i++)
    {
        if(!strcmp(id, s1->data[s1->top].number))    //匹配到車輛
        {
            pop(s1);                        //匹配到的車輛出棧
            while(EmptyStack(s2) != SUCCESS)
            {
                car c = pop(s2);           //從讓路棧出來
                push(s1,c.number);         //進入停車棧
            }
            /*如果等候佇列不為空,則第一輛車進棧*/
            if(EmptyQueue(q) != SUCCESS)
            {
                char *id = DelQueue(q);
                push(s1,id);
                free(id);
            }
            break;
        }
        else
        {
            car c = pop(s1);     //不匹配則出棧並且進入讓路棧
            push(s2, c.number);
        }
    }

}

第三個檔名:park.h

#ifndef PARK_H
#define PARK_H

#include <time.h>
#include <string.h>

#define MAXSIZE     5
#define SUCCESS    1000
#define FAILURE    1001
#define FULL       1002

struct carinfo
{
    char number[10];       //車牌號
    time_t park_time;     //進場時間
    struct carinfo *next;  //指向下一輛車
};
typedef struct carinfo car;

struct stackinfo
{
    car data[MAXSIZE];      //結構他陣列
    int top;               //棧頂指標
};
typedef struct stackinfo stack;

struct queueinfo
{
    car *front;         //隊頭指標
    car *rear;          //隊尾指標
};
typedef struct queueinfo queue;

void welcome();
void menu();
void bye();
void Init(stack *s1,stack *s2,queue *q);
int InitQueue(queue *q);
int InitStack(stack *s);
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 EmptyQueue(queue *q);
void LeavePark(stack *s1,stack *s2,queue *q);
car pop(stack *s);
int EmptyStack(stack *s);
char *DelQueue(queue *q);

#endif




第四個檔名:queue.c

#include "park.h"
#include <stdlib.h>
#include <string.h>

/*初始化佇列*/
int InitQueue(queue *q)
{
    if(NULL == q)
    {
        return FAILURE;
    }
    car *p = (car *)malloc(sizeof(car));
    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;
    }
    car *p = (car *)malloc(sizeof(car));
    if(NULL == p)
    {
        return FAILURE;
    }

    strcpy(p->number,id);
    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(sizeof(char) * 10);
    car *c = q->front->next;

    if(c != NULL)
    {
        strcpy(id,c->number);    //儲存車牌號
        q->front->next = c->next;
        free(c);
    }
    if(c == q->rear)          //如果佇列只剩一輛車
    {
        q->rear = q->front;
    }
    return id;
}

第五個檔名:stack.c

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

/*棧的初始化*/
int InitStack(stack *s)
{
    if(NULL == s)
    {
        return FAILURE;
    }
    s->top = -1;

    return SUCCESS;
}

/*進棧的操作*/
int push(stack *s,char *id)
{
    if(NULL == s)
    {
        return FAILURE;
    }

    if(s->top == MAXSIZE - 1)
    {
        return FULL;
    }
    strcpy(s->data[s->top + 1].number,id);   //拷貝車牌號
    s->data[s->top + 1].park_time = time(NULL);  //記錄進場時間

    s->top++;
    return SUCCESS;
}

/*出棧操作*/
car pop(stack *s)
{
    car c;
    if(NULL == s)
    {
        return c;
    }
    if(s->top == -1)
    {
        return c;
    }


    strcpy(c.number, s->data[s->top].number);
    c.park_time = s->data[s->top].park_time;

    s->top--;
    return c;
}

int EmptyStack(stack *s)
{
    if(NULL == s)
    {
        return FAILURE;
    }
    return (s->top == -1) ? SUCCESS : FAILURE;
}