1. 程式人生 > >蘇嵌日誌第四天

蘇嵌日誌第四天

學習日誌4 姓名:潘曦 日期:2018.9.13 今日學習任務: 完成用c語言的棧和佇列的知識編寫出停車場的程式。 今日任務完成情況: 任務按時完成,完成約500行程式碼量。 今日問題: 輸入亂碼時未能出現提示,執行出現錯誤。 出車時時間計時會重置,未能解決。

今日編寫的程式: ①park.h

#ifndef PARK_H
#define PARK_H

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

#define MAXSIZE 5
#define SUCCESS 100
#define FAILURE 101
#define FULL        102
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 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); int EmptyQueue(queue
*q); void ShowWaitInfo(queue *q); void LeavePark(stack *s1,stack *s2,queue *q); car pop(stack *s); int EmptyStack(stack *s); char *DelQueue(queue *q); #endif

②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***************************\n");
    printf("\t\t\t***************************\n");
    printf("\t\t\t**********WELCOME**********\n");
    printf("\t\t\t***************************\n");
    printf("\t\t\t***************************\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)
{
    if(NULL == s || NULL == q)
    {
        return ;
    }
    char id[10]={0};
    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;                    
        }
        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);  
        }
    }


}

③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;
}

④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;
}

⑤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;
}