用栈和队列实现简单的停车场管理系统

Parking.h:

#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 *);

Parking.h:

#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 = newnode;
	rear->next = NULL;
    }
}

void outque()
{
    if(front == NULL)
    {
        printf("没有人在等待!\n");
    }
    else
    {

        QUE *tmp = front;
	front->flt = 0;
	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(rear == 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++)
    {
        //char *tmp = (char *)malloc(30);
        tmp = popp();
	pushg(tmp);
    }
    tmp = popp();
    time_t timer = time(NULL);
    printf("进来时间:%s\n",tmp);
    printf("当前时间:%s\n",ctime(&timer));
    mytime(tmp,ctime(&timer));
    for(i = 0;i < temp; i++)
    {
        popg();
    }
    if(front == NULL)
    {
        //printf("居然没人在等车~~~\n");
    }
    else
    {
        printf("有空位了!等待人员进入停车!\n");
	outque();
	pushp();
    }
    end:
    printf("");
}

void myprint()
{
    int i;
    for(i = 0;i <= topp; i++)
    {
        printf("%d:%s",i,parking[i].timer);
    }
}

void mytime(char *timer1,char *timer2)
{
    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');
    printf("您一共停了:%02d:%02d:%02d\n",hour2-hour1,min2-min1,sec2-sec1);
    int money = ((hour2 - hour1) * 60 * 60 + (min2 - min1) * 60 + (sec2 - sec1)) * per;
    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)
    {
        //time_t timer = time(NULL);
        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++)
	{
	    printf("%d:空\t\t",i);
	    l++;
	    if(l % 4 == 0)
	    {
	        printf("\n");
	    }
	}
	printf("\n\n\n\n\n");    //pushp();
		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++)
	{
	    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");
		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");
	    }
	}
	l = 0;
	printf("\n\n\n\n\n");
	        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()
{
    #if 0
    char *test = (char *)malloc(sizeof(char));
    int hour,min,sec;
    time_t timer = time(NULL);
    strcpy(test,ctime(&timer));
    printf("%s\n",test);
    mytime(test,&hour,&min,&sec);
    printf("now time is:%d:%d:%d\n",hour,min,sec);
    #endif
    display();
    return 0;
}

刚刚不知道为什么最后一部分都在一起了,现在该了一下~~~~~~~~~~~~~~~~·

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/zy799894671/article/details/7760975
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞