#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
FILE*fp;
char infile[]="stop.txt";
struct station{
	char carname[90];
	time_t stoptime;
	int sky;
}sta[50];
int num=0;
void spare ();//查空车位
void stop ();//停车
void live ();//离开
void fprintf ();
void face ();
void spare ()
{
	fp=fopen ("stop.txt","r");
	int i;
	if (num>=50)
	{
		printf ("对不起,停车场已满!");
	}
	else
	{
		printf ("以下位置是空的:\n\n");
		for(i=0;i<50;i++)
		{
			if (sta[i].sky ==0)
			{
				printf ("%d号\t",i+1);
			}
		}
	}
}
void stop ()
{
	for (int i=0;i<50;i++)
	{
		if (sta[i].sky==0)
		{
			fp=fopen ("stop.txt","a+");
			printf ("请输入您的车牌号:");
			scanf ("%s",sta[num].carname);
			fputs (sta[num].carname,fp);
			sta[num].stoptime=time(NULL);
			
			printf ("您的停车时间是:");
			printf ("%s",ctime(&sta[num].stoptime));
			printf ("您好!已经完成操作!");
			sta[num].sky = num+1;
			num++;
			fclose (fp);
			break;
		}
	}
}
void live ()
{
	fp=fopen ("stop.txt","r");
	int i;
	time_t ts;
	char carname[90];
	long money;
	printf ("请输入您的车牌号:");
	scanf ("%s",carname);
	for (i=0;i<=50;i++)
	{
		if (strcmp(sta[i].carname,carname)==0)
		{
			ts=time(NULL);
			sta[i].sky=0;
			money=ts-sta[i].stoptime;
			printf ("您的开车时间是:");
			printf ("%s\n",ctime(&ts));
			printf ("%ld\n",money);
			if (money/60>=30&&money<60)
			{
				printf ("您应交3元!祝您一路平安!");
			}
			else if (money/60>=60&&money/60<120)
			{
				printf ("您应交5元!祝您一路平安!");
			}
	        else if (money/60>=120&&money<180)
			{
		        printf ("您应交10元!祝您一路平安!");
			}
	        else if (money/60<30)
			{
		        printf ("您应交0元!祝您一路平安!");
			}
	        else 
			{
		        printf ("您应交20元!祝您一路平安!");
			}
		}
	}
}
void fprintf ()
{
	fp=fopen ("stop.txt","r");
	printf ("车牌号\t\t\t\t停车时间\n");
	for (int i=0;i<=num;i++)
	{
		if (sta[i].sky!=0)
		{
			printf ("%s\t\t\t",sta[i].carname);
			printf ("%s",ctime(&sta[i].stoptime));
		}
	}
}
void face ()
{
	int choose;
	printf ("\n\n\n\n\n\n\n\t\t\t\t++++++++++++++++++\n");
	printf ("\t\t\t\t||0.        退出||\n");
	printf ("\t\t\t\t||1.        停车||\n");
	printf ("\t\t\t\t||2.        取车||\n ");
	printf ("\t\t\t\t||3.显示所有数据||\n");
	printf ("\t\t\t\t++++++++++++++++++\n");
	scanf ("%d",&choose);
	switch (choose)
	{
	case 3:
		{
			system ("cls");
			fprintf ();
			system("pause");
			system ("cls");
			face ();
			break;
		}
	case 1:
		{
			system ("cls");
			spare ();
			stop ();
			system("pause");
			system ("cls");
			face ();
			break;
		}
	case 2:
		{
			system ("cls");
			live ();
			system("pause");
			system ("cls");
			face ();
			break;
		}
	case 0:
		break;
	}
}
void main ()
{
	printf ("\n\n\n\n\t\t\t     欢迎进入停车场管理系统\n\n");
	printf ("\n********************************************************************************\n\n\n\n\t\t\t        ");
	system("pause");
	system ("cls");
	for (int i=0;i<50;i++)
		sta[i].sky = 0;
	face ();
}