#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include<io.h>
void putCar();
void getCar();
void look();
void minit();
void save();
void menu();
int i;
int temp;
int flag=1;
double cost;
int park[2][6];
struct carInfo
{
	int carNo;		//车牌号 
	int layerNo;	//层号 
	int parkNo;		//车位号 
	int time;		//停车时间 
}Cinfo[12];
void minit()                                          /*初始化*/
{
	char filename[] = "car.dat";
	FILE *fp;
	int i;
	if((fp = fopen(filename,"r")) == NULL){
		printf("无法打开文件 \" car.dat\"\n");
		return ;
		}
	for(i=0;i<4;i++){                               /*读数据文件内容到数组*/
		fscanf(fp,"%d ", &Cinfo[i].carNo);
		fscanf(fp,"%d ", &Cinfo[i].layerNo);
		fscanf(fp,"%d ", &Cinfo[i].parkNo);
		fscanf(fp,"%d ", &Cinfo[i].time);
		}
	fclose(fp);
}

void save()
{                                                     /*保存数据到car.dat*/
	char file[] = "car.dat";
	FILE *fp;
	int i=0;
	if((fp = fopen(file,"w")) == NULL){
		printf("无法打开文件 \" car.dat\"\n");
		return ;
		}
	for(i=0;i<12;i++){                                   /*写数据到文件,这里可以用一下feof函数*/
		fprintf(fp,"%d ", Cinfo[i].carNo);
		fprintf(fp,"%d ", Cinfo[i].layerNo);
		fprintf(fp,"%d ", Cinfo[i].parkNo);
		fprintf(fp,"%d ", Cinfo[i].time);
		fprintf(fp,"\n" );
		}
	fclose(fp);
}


int main()                                              /*定义主函数*/
{
	FILE *fp;
		if((fp=fopen("car.dat","r"))==NULL)
		{
			printf("cannot open this file\n");
			exit(0);
		}
    fclose(fp);	
	menu();		
}


void menu()
{                                                
	int a;                                                   /*菜单选择*/
	int init();
	 do{ 
		printf("\n\n\t*******************************************************************\n");
		printf("\t*                         主菜单                                  *\n");
		printf("\t*                        1.停车                                   *\n");
		printf("\t*                        2.取车                                   *\n");
		printf("\t*                        3.当前车位信息                           *\n");
		printf("\t*                        4.退出                                   *\n");           
		printf("\t*******************************************************************\n");
		printf("\t\t\t\t请输入选项:");
		scanf("%d",&a);  
		switch(a){                                             
			      case 1:putCar(); break;
			      case 2:getCar();break;
			      case 3:look();break;
		          case 4:exit(0); break; 
			      default:printf("您输入的信息有误!\n"); break;
	              };
	     	getchar();
	 }while(1);	/*return 0;*/
}
  void putCar()                                               /*停车操作*/
  {                                                         
	printf("\n\n\t\t\t*********欢迎来到停车场*********\n");
	printf("\t\t\t请输入你的车牌号:");
	do{
		scanf("%d",&temp);
		if((temp>0)&&(temp<10000))break;
		flag=0;
		printf("Your car number is a digit between 0 and 10000:");
	}while(!flag);
	flag=0;
	for(i=0;i<12;i++)
	    {
		if(Cinfo[i].carNo!=0)
			continue;
		Cinfo[i].carNo=temp;
		flag=1;
		break;
		}
	if(flag==0){                            /*没有找到位置,停车场已满*/
		printf("\t\t很抱歉!车位已满:\n");
		return ;
		}
	for(i=0;i<12;i++){                      /*所有车的停车时间一起加5*/
		if(Cinfo[i].carNo!=0)
		   Cinfo[i].time+=5;
		}
	save();
	getchar();
	printf("\t\t\t存车成功!\n\t\t\t按任意键回到主菜单...");
  }

  void getCar()
  {                                                        /*取车*/
	int i,temp,flag=0;
	printf("\n\n\t\t*********从停车场取车*********\n ");
	printf(" \t\t请输入你的车牌号码:");
	do{
		scanf("%d",&temp);
		if((temp>0)&&(temp<10000)){
			for(i=0;i<12;i++)
				if(Cinfo[i].carNo==temp){
					Cinfo[i].carNo=0;                
					cost=0.2*Cinfo[i].time/5;
					Cinfo[i].time=0;                            /*删除该车位车辆信息*/
					flag=1;
					break;
					}
			}
		if(flag==0)
			printf("\t\t您输入的车牌号码有误,请重新输入。\n");
	}while(!flag);
	printf("\n \t\tPrintf out the cost?Yes(1)/No(2)");
	scanf("%d",&i);
	switch(i){
		case 1:	printf("\n\t\t车牌号 : %d     费用 %8.2f元\n",temp,cost);
		case 2:
		default: break;
		}
	save();                                               /*保存数据*/
	getchar();
	printf("\t\t取车成功!\n按任意键回到主菜单...");
  }

  void look()
  {                                                        /*显示当前停车场信息*/
	int i;
	printf("\n\n\t\t*********当前停车场车位信息*********\n");
	printf("车牌号\t\t楼层\t       车位 \t\t时间\n");
	for(i=0;i<12;i++)
		if(Cinfo[i].carNo!=0)
			printf("%d\t\t %d \t\t%d  \t\t %d\n",Cinfo[i].carNo,Cinfo[i].layerNo,Cinfo[i].parkNo,Cinfo[i].time);
	getchar();
	printf("\t\t\t输入任意键返回主菜单...");
  }