www.pudn.com > pms.rar > main.cpp


/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 
 
项目名:停车场管理系统 
课程:数据结构基础 
编译环境:Visual Stuido .Net 2005 
最后更改:2005/9/25 
程序说明:1.本程序在VS.NET下编写与调试完成 
2.TC下编译亦通过,0 warning ,0 error. 
行数:400(包括大量注释) 
 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/ 
 
#include 
#include 
 
//宏定义 
#define STACKSIZE 3   //停车场容量,小一点,容易调试;) 
#define PRICE 0.05     //费率:$/min,真便宜:) 
#define OK 1 
 
typedef int status; 
 
//结构定义 
typedef struct {     //车辆信息结构 
 
	char licence[12];    //车牌号 
	int reach_h;     //到达时间的小时值 
	int reach_m;      
	int leave_h;     //离开时间的小时值 
	int leave_m; 
} CarInfo;  
 
typedef struct {       //用于模拟停车场以及临时队列的栈 
 
	CarInfo *carstack[STACKSIZE+1];      //顺序存储 
	int top;                          //栈顶   
}ParkStack;  
 
typedef struct Car{     //////// 便道的队列 
	CarInfo *data;      // 
	struct Car *next;   //指向链表的下一个元素 
}QueueNode;             //车辆链表     
//     
typedef struct {        // 
	QueueNode *front;   //队头 
	QueueNode *rear;    //队尾 
}ParkLaterQueue;        ///////// 
 
 
//---------------函数声明--------------------// 
 
//初始化函数块 
status InitStack(ParkStack *);         //初始化栈 
status InitQueue(ParkLaterQueue *);    //初始化便道 
 
//执行方法 
status ParkingPush(ParkStack *,ParkLaterQueue *);             //车辆到达 
status ParkingPop(ParkStack *,ParkStack *,ParkLaterQueue *);  //车辆离开 
status TaskMenu(ParkStack *,ParkStack *,ParkLaterQueue *);    //列出菜单 
status ShowStatus(ParkStack *,ParkLaterQueue *);            //列出停车场状态 
 
status PrintCarInfo(CarInfo *);    //打印离开信息 
status GetTimeReach(CarInfo *);    //获取到达时间 
status GetTimeLeave(CarInfo *);    //获取离开时间 
status ShowTime(int,int);          //检查时间信息,并打印 
 
 
 
//-------------函数声明完毕----------------// 
 
//------------主函数开始---------------// 
 
int main() 
{ 
 
	//定义结构实例 
	ParkStack ComeIn,TempOut; 
	ParkLaterQueue Waiting; 
 
	//调用初始化函数 
	InitStack(&ComeIn);         //初始化车站,传入实例地址. 
	InitStack(&TempOut);        //初始化让路的临时栈 
	InitQueue(&Waiting);        //初始化通道 
 
	//列出任务栏并执行 
	TaskMenu(&ComeIn,&TempOut,&Waiting); 
 
	return OK; 
} 
 
 
//列出任务表 
status TaskMenu(ParkStack  *ComeIn,ParkStack  *TempOut,ParkLaterQueue  *Waiting){ 
 
	int menu; 
 
	printf("\n\n      -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"); 
	printf("      -             Cindy's Parking Management System             -\n"); 
	printf("      -               2005/09/25     version  1.0                 -\n"); 
	printf("      -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"); 
	while(1) 
	{ 
		//打印主菜单 
		printf("\n\4 Please choose:\n 1. Arrivals\n"); 
		printf(" 2. Leaves\n"); 
		printf(" 3. Show the status of Park\n"); 
		printf(" 4. Quit\n\n"); 
 
		while(1) 
		{ 
			flushall(); 
			if(!scanf("%d",&menu)){ 
				printf("You must input number!\n"); 
				break; 
			} 
			if(menu>=1&&menu<=4) 
				break; 
			else printf("\n\aError!\nPlease rechoose: 1 to 4.\n"); 
		} 
		switch(menu) 
		{ 
		case 1: 
			printf("\4 A car is coming...\n"); 
			ParkingPush(ComeIn,Waiting); 
			break;          //车辆到达 
		case 2: 
			printf("\4 A car is leaving...\n"); 
			ParkingPop(ComeIn,TempOut,Waiting); 
			break;         //车辆离开 
		case 3: 
			printf("\4 The status of the park is...\n"); 
			ShowStatus(ComeIn,Waiting);  
			break;         //列出停车场状态 
		case 4:  
			printf("\4 GoodBye!\n\n"); 
			return OK;     //直接退出主程序 
		default:break; 
		} 
	}	 
} 
 
 
//----------------初始化函数-------------------------// 
status InitStack(ParkStack *ps) //初始化栈 
{ 
	int i; 
	ps->top=0; 
	for(i=0;i<=STACKSIZE;i++) 
		ps->carstack[i]=NULL; 
	return OK; 
} 
status InitQueue(ParkLaterQueue *plq) //初始化便道 
{ 
	plq->front=plq->rear=(QueueNode *)malloc(sizeof(QueueNode)); 
	if(!plq->front) //申请失败 
		exit(1); 
	plq->front->next=NULL; 
	return OK; 
} 
 
//----------------其他执行方法-------------------------// 
 
//车辆到达 
status ParkingPush(ParkStack *ComeIn,ParkLaterQueue *Wait_Cars) 
{ 
	CarInfo *Info_A_Car; 
 
	QueueNode *Car_Will_Wait; 
 
	Info_A_Car=(CarInfo *)malloc(sizeof(CarInfo));//申请存储空间 
 
	printf("Input car licence:\n"); 
 
	getchar();                   //由于之前有回车在输入流中,需清除\n 
 
	gets(Info_A_Car->licence); 
 
	if(ComeIn->toptop++; 
 
		GetTimeReach(Info_A_Car); 
 
		printf("The car's position number is No. %d\n",ComeIn->top); 
		ComeIn->carstack[ComeIn->top]=Info_A_Car; 
	} 
	else //车场已满,车进便道 
	{ 
		printf("!There is no position, It must wait at waiting Way!\n"); 
		Car_Will_Wait=(QueueNode *)malloc(sizeof(QueueNode)); 
		Car_Will_Wait->data=Info_A_Car; 
		Car_Will_Wait->next=NULL; 
 
		Wait_Cars->rear->next=Car_Will_Wait; 
		Wait_Cars->rear=Car_Will_Wait; 
	}  
	return OK; 
} 
 
 
//车辆离开 
status ParkingPop(ParkStack *ComeIn,ParkStack *TempOut,ParkLaterQueue *Wait) 
{  
	int num,tmp_h,tmp_m; 
 
 
	//定义两个结构实例 
	QueueNode *Car_Will_Into;     //便道中将要入停车场的汽车结点 
 
	CarInfo *Car_Into;            //便道中将要入停车场的汽车的信息结构实例 
 
	//判断车场内是否有车,若无车则显示无车信息 
 
	if(ComeIn->top>0) //有车 
	{ 
		while(1) //输入离开车辆的信息 
		{ 
			printf("\nInput the position of this car:1 to %d:\n",ComeIn->top); 
			scanf("%d",&num); 
			if(num>=1&&num<=ComeIn->top) 
				break; 
		} 
 
		while(ComeIn->top>num)   //原PARK栈中出栈,进入TEMP栈,直至位置为NUM的车出来 
		{ 
			TempOut->top++; 
			TempOut->carstack[TempOut->top]=ComeIn->carstack[ComeIn->top]; 
			ComeIn->carstack[ComeIn->top]=NULL; 
			ComeIn->top--; 
		} 
 
		GetTimeLeave(ComeIn->carstack[ComeIn->top]); 
 
		//保存离开时间,将赋给下一辆车进站时间 
		tmp_h=ComeIn->carstack[ComeIn->top]->leave_h; 
		tmp_m=ComeIn->carstack[ComeIn->top]->leave_m; 
 
 
		PrintCarInfo(ComeIn->carstack[ComeIn->top]);   //打印离开的车 
 
		ComeIn->carstack[ComeIn->top]=NULL;  //将离开的车所在数组元素赋空值 
		ComeIn->top--; 
 
 
 
		while(TempOut->top>=1)             //让在临时栈中车辆回停车场 
		{ 
			ComeIn->top++; 
			ComeIn->carstack[ComeIn->top]=TempOut->carstack[TempOut->top]; 
			TempOut->carstack[TempOut->top]=NULL; 
			TempOut->top--; 
		} 
 
 
		//便道上是否有车 
		if(Wait->front!=Wait->rear)   //便道的车辆进入车场 
		{ 
			Car_Will_Into=Wait->front->next; 
			Car_Into=Car_Will_Into->data; 
			ComeIn->top++; 
			//显示从便道进入的车辆的信息 
			printf("\nThe car %s enter position %d from waiting way.\n",Car_Into->licence,ComeIn->top); 
 
			//输入进入时间 
 
			Car_Into->reach_h=tmp_h; 
			Car_Into->reach_m=tmp_m; 
 
			//下辆车成为先出车辆 
			Wait->front->next=Car_Will_Into->next; 
 
			if(Car_Will_Into==Wait->rear)  
				Wait->rear=Wait->front; 
			ComeIn->carstack[ComeIn->top]=Car_Into; 
 
			//释放链表所移去元素内存 
			free(Car_Will_Into);  
		} 
	} 
	else 
		printf("\nThere is no car in the park yet.\n"); //没车 
 
	return OK; 
} 
 
 
//打印出站车的信息 
status PrintCarInfo(CarInfo *lc)  
{ 
	int rh,lh,rm,lm,total_min; 
	double total_price; 
 
	printf("\nThe Licence of the leaving car:  "); 
 
	puts(lc->licence); 
 
	rh=lc->reach_h; 
	rm=lc->reach_m; 
	lh=lc->leave_h; 
	lm=lc->leave_m; 
	printf("Arrival time: "); 
	ShowTime(rh,rm); 
	printf("   Left time: "); 
	ShowTime(lh,lm); 
 
	total_min=(lh-rh)*60+lm-rm;     //计算总分钟 
 
	total_price=total_min*PRICE;    //计算总价 
 
	printf("You need to pay: $%.2f!\n",total_price); 
	return OK; 
} 
 
 
//显示停车场信息包括停车场内和便道 
status ShowStatus(ParkStack *Info_In_Car ,ParkLaterQueue *Info_Wait_Car){ 
 
	int i; 
	QueueNode *A_Car; 
 
	//显示停车场内信息 
	printf("\n\4 Park:\n"); 
	if(Info_In_Car->top>0) //判断车站内是否有车 
	{ 
		for(i=1;i<=Info_In_Car->top;i++) 
		{ 
			printf("No.%d",i); 
			printf(" | The licence is: "); 
			printf("%s",Info_In_Car->carstack[i]->licence); 
			printf(" |  Time reach: "); 
			ShowTime(Info_In_Car->carstack[i]->reach_h,Info_In_Car->carstack[i]->reach_m);//打印到达时间 
 
		} 
	} 
	else 
		printf("No car in the park\n"); 
 
	//显示便道信息 
	printf("\n\4 Waiting Way:\n"); 
	A_Car=Info_Wait_Car->front->next; 
	if(Info_Wait_Car->rear!=Info_Wait_Car->front) //判断通道上是否有车 
	{ 
		printf("\nThe Licence of waiting car :\n"); 
		while(A_Car!=NULL) 
		{ 
			puts(A_Car->data->licence); 
			A_Car=A_Car->next; 
		} 
	} 
	else printf("No car at the Waiting Way.\n"); 
	return OK; 
} 
 
//获得进站时间 
status GetTimeReach(CarInfo * Car){ 
	printf("Input arrival time,eg:4:30,16:40 etc.\n"); 
	while(1){ 
		scanf("%d:%d",&(Car->reach_h),&(Car->reach_m)); 
		if(Car->reach_h>=0&&Car->reach_h<24&&Car->reach_m>=0&&Car->reach_m<60) 
			break; 
		else 
			printf("Your input is wrong,please try again.\n"); 
	} 
	return OK; 
} 
//获得出站时间 
status GetTimeLeave(CarInfo * Car){ 
	printf("Input now time,eg:4:30,16:40 etc.\n"); 
	while(1){ 
		scanf("%d:%d",&(Car->leave_h),&(Car->leave_m)); 
		if(Car->leave_h>=0&&Car->leave_h<24&&Car->leave_m>=0&&Car->leave_m<60) 
			break; 
		else 
			printf("Your input is wrong,please try again.\n"); 
	} 
	return OK; 
} 
 
//显示时间 
status ShowTime(int h,int m){ 
	if(h<10)             //防止出现输入2:01,而显示却是2:1的情况 
		putchar('0'); 
	printf("%d:",h); 
	if(m<10) 
		putchar('0'); 
	printf("%d\n",m); 
	return OK; 
}