停车场管理系统(有诠释,改了几个bug的版本)

停车场管理系统(有注释,改了几个bug的版本)

Parking.h:

/**********************************************************************************
*                       用栈和队列实现简单的停车场管理系统
*
*
*
*
* 文    件: Parking.h
* 作    者: 邹亚鹏
* 部    门: 南京工程大学
* 编写日期: 2012.7.18
* 模块版本:
* 修改记录:
* ================================================================================
* 版 本|  日  期  | 修改人 |
* ================================================================================
*      |          |        |
*      |          |        |
*      |          |        |
* ================================================================================
*
**********************************************************************************/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

#define N 1
/********************定义停车记录结构**********************/
typedef struct car
{
    char * timer;
}CAR;
/********************定义队列记录结构**********************/
typedef struct que
{
    int flt;
    struct que *next;
}QUE;
/***********************申明函数****************************/
void pushp();
char *popp();
void push(char *);
void popg();
void enterque();
void outque();
void mytime(char *,char *,int);
void myprint();


Parking.c:

/**********************************************************************************
*                       用栈和队列实现简单的停车场管理系统
*
*
*
*
* 文    件: Parking.C
* 作    者: 邹亚鹏
* 部    门: 南京工程大学
* 编写日期: 2012.7.18
* 模块版本:
* 修改记录:
* ================================================================================
* 版 本|  日  期  | 修改人 |
* ================================================================================
*      |          |        |
*      |          |        |
*      |          |        |
* ================================================================================
*
**********************************************************************************/
#include "Parking.h"

CAR parking[10];                        /*定义停车栈*/
CAR giveway[10];                        /*定义让路栈*/
int per = N;                            /*定义没秒收费*/
int topp = -1;
int topg = -1;
QUE *front = NULL;
QUE *rear = NULL;
/**********************************************************************************
*                               入队函数
*描述:停车栈满时,进入等待队列。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void enterque()
{
    QUE *newnode = (QUE *)malloc(sizeof(QUE));
    if(rear == NULL)
    {
	newnode->flt = 1;
	front = newnode;
	rear = newnode;
	rear->next = NULL;
    }
    else
    {
        newnode->flt = 1;
	rear->next = newnode;
	rear = newnode;
	rear->next = NULL;
    }
}
/**********************************************************************************
*                               出队函数
*描述:停车栈有空位,出队进栈。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void outque()
{
    if(front == NULL)
    {
        printf("没有人在等待!\n");
    }
    else
    {

        QUE *tmp = front;
	front->flt = 0;
	if(rear == front)
	{
	    free(tmp);
	    rear = NULL;
	    front = NULL;
	    tmp = NULL;
	}
	else
	{
	    front = front->next;
	    free(tmp);
	    tmp = NULL;
	}
    }
}
/**********************************************************************************
*                               停车栈压栈函数
*描述:开始停车。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void pushp()
{
    if(topp == 9)
    {
        printf("停车位已满,请等待!\n");
    }
    else
    {
        topp++;
        time_t timer = time(NULL);               /*获取当前机器时间*/
	parking[topp].timer = (char *)malloc(30);
	strcpy(parking[topp].timer,ctime(&timer));   /*将时间信息保存*/
    }
}
/**********************************************************************************
*                               停车栈出栈函数
*描述:开始离开停车栈。
*
*参数:  无
*
*返回值:车进入时间
*
*注意事项:
**********************************************************************************/
char * popp()
{
    char *str = (char *)malloc(30);
    if(topp == -1)
    {
        printf("停车位为空!\n");
    }
    else
    {
        strcpy(str,parking[topp].timer);
	free(parking[topp].timer);             /*出栈释放空间*/
	parking[topp].timer = NULL;
	topp--;
    }
    return str;                            /*开始停车时间保存*/
}
/**********************************************************************************
*                               让路栈入栈函数
*描述:停车栈进让路栈。
*
*参数:  车进入时间
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void pushg(char *tm)
{
    if(topg == 9)
    {
        printf("让路间已经不能在进车了!\n");
    }
    else
    {
        topg++;
	giveway[topg].timer = (char *)malloc(30);
        strcpy(giveway[topg].timer,tm);            /*开始停车时间保存进让路栈*/
    }
}
/**********************************************************************************
*                               让路栈出栈函数
*描述:返回停车栈。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void popg()
{
    if(topg == -1)
    {
        printf("让路间还是空的哦!\n");
    }
    else
    {
        topp++;
	parking[topp].timer = (char *)malloc(30);
        strcpy(parking[topp].timer,giveway[topg].timer);   /*开始停车时间保存返回停车栈*/
	topg--;
    }
}
/**********************************************************************************
*                               进车函数
*描述:有车来操作。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void incar()
{
    if(topp == 9)                                 /*无停车位*/
    {
        printf("停车位已经满!请耐心等待!\n");
	enterque();
    }
    else                                         /*有停车位*/
    {
        if(front == NULL)                        /*无人等车位*/
	{
	    printf("当前有空位!请停车!\n");
	    pushp();
	}
	else                                          /*有人等车位,进等待队列*/
	{
	    printf("停车位已满!请等待!\n");
	    enterque();
	}
    }
}
/**********************************************************************************
*                               出车函数
*描述:出去操作。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void outcar()
{
    int no,i,temp;
    printf("请输出要出车的停车位号:");
    char *tmp = (char *)malloc(30);
    scanf("%d",&no);
    if(no > topp)                              /*该停车位无车*/
    {
        printf("该车位没有车!\n");
	goto end;                                  /*结束*/
    }
    temp = topp - no;
    for(i = 0; i < temp; i++)                   /*进让路栈保存*/
    {
        tmp = popp();
	pushg(tmp);
    }
    tmp = popp();                              /*离开*/
    time_t timer = time(NULL);
    printf("进来时间:%s",tmp);
    printf("当前时间:%s\n",ctime(&timer));
    mytime(tmp,ctime(&timer),1);               /*记时收费*/
    for(i = 0;i < temp; i++)                   /*让路栈返回停车栈*/
    {
        popg();
    }
    if(front == NULL)                           /*无人在等车位*/
    {
        ;
    }
    else                                        /*有人在等车位*/
    {
        printf("有空位了!等待人员进入停车!\n");
	outque();                                   /*出队*/
	pushp();                                    /*进停车栈*/
    }
    end:
    printf("");
}
/**********************************************************************************
*                               显示当前停车位函数
*描述:显示停车位信息。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void myprint()
{
    int i;
    time_t timer = time(NULL);
    char *tmp = ctime(&timer);                          /*获取当前机器时间*/
    printf("当前系统时间为:%s\n",tmp);
    for(i = 0;i <= topp; i++)
    {
        printf("%d号车位:开始停车时间%s",i,parking[i].timer);
	mytime(parking[i].timer,tmp,0);                     /*显示义停时间*/
    }
}
/**********************************************************************************
*                               求停车时间函数
*描述:求停车时间函数。
*
*参数:  timer1:当前时间    timer2:开始停车时间   flt:是一个标志,控制money的输出与否
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void mytime(char *timer1,char *timer2,int flt)
{
    int hour1 = (timer1[11]- '0') * 10 + (timer1[12] - '0');
    int min1 = (timer1[14]- '0') * 10 + (timer1[15] - '0');
    int sec1 = (timer1[17]- '0') * 10 + (timer1[18] - '0');
    int hour2 = (timer2[11]- '0') * 10 + (timer2[12] - '0');
    int min2 = (timer2[14]- '0') * 10 + (timer2[15] - '0');
    int sec2 = (timer2[17]- '0') * 10 + (timer2[18] - '0');
    int money = ((hour2 - hour1) * 60 * 60 + (min2 - min1) * 60 + (sec2 - sec1)) * per;
    int hour,min,sec;
    hour = hour2-hour1;
    min = min2-min1;
    sec = sec2 - sec1;
    while(1)                                  /*转换成合法时间*/
    {
        if(min < 0)
        {
            hour = hour - 1;
            min = min + 60;
        }
        if(sec < 0)
        {
            min = min - 1;
	    sec = sec + 60;
        }
	if((hour >= 0) && (min >= 0) && (sec >= 0))
	{
	    break;
	}
    }
    printf("截至当前共停了:%02d:%02d:%02d\n\n",hour,min,sec);
    if(flt == 1)
    {
        printf("消费了%d元钱!\n",money);
    }
}
/**********************************************************************************
*                               菜单函数
*描述:该函数用于对屏幕进行初始化。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
void display()
{
    int choose;
    system("clear");
    printf("\n\n\n\n\n\n\n\n\n\n\n");
    printf("\t\t\t   欢迎使用停车场管理系统!\n");
    printf("\n\n\n\n\n\n\n\n\n\n\n");
    sleep(2);
    while(1)
    {
        system("clear");                               /*清屏*/
	int i;
	int l = 0;
	for(i = 0; i <= topp ; i++)                         /*动态显示车位信息*/
	{
	    printf("%d:有车\t\t",i);
	    l++;
	    if(l % 4 == 0)
	    {
	        printf("\n");
	    }
	}
	for(i = topp+1; i < 10; i++)
	{
	    printf("%d:空\t\t",i);
	    l++;
	    if(l % 4 == 0)
	    {
	        printf("\n");
	    }
	}
	printf("\n\n\n\n\n");
        printf("/*******************************************/\n");
        printf("/*************** 1.停车 ********************/\n");
        printf("/*************** 2.离开 ********************/\n");
        printf("/*************** 3.查看当前以停时间*********/\n");
        printf("/*************** 4.退出*********************/\n");
        printf("/*******************************************/\n");
	printf("请输入您的选择:");
	scanf("%d",&choose);
	switch(choose)
	{
	    case 1:
	    {
                system("clear");
		l = 0;                                  /*动态显示车位信息*/
	    	for(i = 0; i <= topp ; i++)
	        {
	            printf("%d:有车\t\t",i);
	            l++;
	        if(l % 4 == 0)
	        {
	            printf("\n");
	        }
	        }
	        for(i = topp+1; i < 10; i++)       /*一行4个*/
	        {
	            printf("%d:空\t\t",i);
	            l++;
	            if(l % 4 == 0)
	            {
	                printf("\n");
	            }
	        }
	        printf("\n\n\n\n\n");
		incar();
		sleep(1);
		break;
	    }
	    case 2:
	    {
                system("clear");
		l = 0;                                   /*动态显示车位信息*/
                for(i = 0; i <= topp ; i++)
	        {
	            printf("%d:有车\t\t",i);
	            l++;
	            if(l % 4 == 0)
	            {
	                printf("\n");
	            }
	        }
	        for(i = topp+1; i < 10; i++)        /*一行4个*/
	        {
	            printf("%d:空\t\t",i);
	            l++;
	            if(l % 4 == 0)
	            {
	                printf("\n");
	            }
	        }
	        l = 0;
	        printf("\n\n\n\n\n");
	        outcar();
		printf("请按回车键继续...\n");
		getchar();
		getchar();
		break;
	    }
	    case 3:
	    {
                system("clear");
	        myprint();
		printf("请按回车键继续...\n");
		getchar();
		getchar();
	        break;
	    }
	    case 4:
	    {
                system("clear");
                printf("\n\n\n\n\n\n\n\n\n\n\n");
		printf("\t\t\t\t欢迎再次使用!\n");
                printf("\n\n\n\n\n\n\n\n\n\n\n");
		sleep(2);
		system("clear");
	        exit(0);
	        break;
	    }
	    default:
	    {
	        break;
	    }
	}
    }
}
/**********************************************************************************
*                               菜单函数
*描述:该函数用于对屏幕进行初始化。
*
*参数:  无
*
*返回值:无
*
*注意事项:
**********************************************************************************/
int main()
{
    display();
    return 0;
}