学習ログ4日目

22460 ワード

蘇嵌プロジェクト実戦
学習日誌名前:李昕日付: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);//chushihua zhan he duilie

    while (1)
    {
        menu();
        printf("xuanzegongneng: 
"
); 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 
#include "park.h"

void welcome()
{
    system("clear");  //qingchu pingmu
    printf("


"
); printf("********************
"
); printf("******huanying******
"
); printf("********************
"
); sleep(2); } void menu() { system("clear"); printf("


"
); printf("\t\t 1.tingche
"
); printf("\t\t 2.chuche
"
); printf("\t\t 3.changneicheliangxinxi
"
); printf("\t\t 4.denghoucheliangxinxi
"
); printf("\t\t 5.tuichuxitong
"
); } void bye() { system("clear"); printf("\t\t\t byebye!!
"
); exit(1); //tuichuchengxu } //chushihua tingchezhan rangluzhan denghouduilie void init(stack *s1, stack *s2, queue *q) { int ret; ret = InitStack(s1); if (FAILURE == ret) { printf("Init Stack Failure!
"
); } ret = InitStack(s2); if (FAILURE == ret) { printf("Init Stack Failure!
"
); } ret = InitQueue(q); if (FAILURE == ret) { printf("Init Queue Failure!
"
); } } void EnterPark(stack *s, queue *q) { char id[32] = {0}; //baocun chepaihao int ret; printf("shuruchepaihao:
"
); scanf("%s",id); ret = push(s,id); if (ret == FAILURE) { printf("push failure!
"
); } else if (ret == FULL) //zhan man { printf("tingchechangman,jinrudenghouqu.
"
); sleep(2); EnterQueue(q,id); } } void ShowParkInfo(stack *s) { if (NULL == s) { return; } if (s->top == -1) { printf("meiyoucheliang!
"
); sleep(2); return; } int i; for (i=0;i<=s->top;i++) { printf("chepaihao: %s
"
,s->CarData[i].id); printf("tingcheshichang: %d
"
,(int)(time(NULL) - s->CarData[i].t)); printf("*********
"
); } printf("Press Enter Continue..
"
); getchar(); getchar(); } void ShowWaitInfo(queue *q) { if (NULL == q) { return; } node *p = q->front->next; while (p) //while (p!= NULL) { printf("chepaihao: %s
"
,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:
"
); scanf("%s",id); while (1) { if (!strcmp(s1->CarData[s1->top].id,id)) //chepaihao xiangtong { str = pop(s1); //cheliang chuzhan free(str); while (EmptyStack(s2) !=SUCCESS) //rangluzhan buweikong { 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) //suoyou cheliang huidao tingchezhan { printf("cheliangbucunzai!
"
); sleep(2); while (EmptyStack(s2) != SUCCESS) { str = pop(s2); push(s1, str); free(str); } break; } } }

park.h
#ifndef PARK_H
#define PARK_H

#include 
#include 
#include 
#include 
#include 

#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  //biao shi denghou duile jiedian xinxi
{
    char id[32];
    struct Node *next;  //zhi zhen yu
};
typedef struct Node node;

struct Queue      //denghou duilie
{
    node *front;  //dui tou zhi zhen
    node *rear;   //dui wei zhi zhen
};
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 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);
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)); //fenpei toujiedian
    if (NULL == p)
    {
        return FAILURE;
    }
    p->next = NULL;

    //duitouzhizhen he duiweizhizhen tongshi zhixiang toujiedian
    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); //baocun chepaihao
    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;  //chushihua cheng kongzhan

    return SUCCESS;
}

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

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

今日開発中に出てきた問題をまとめて先生にコードを打つのはやはり多くの間違いがあって、自分は本当に細心の注意を払っていません.
今日は問題を解決していないので、あまり問題はありません.
今日の開発の収穫はコードにもっと熟練して、コードを打つのはもっと熟練しています.
自己評価はまだあまりよくありません.