停车场管理系统

 【要求C或C++编程】
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若停车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。
【基本要求】
以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。

完整的实现代码如下:

第一种方法:

  1. #include "stdio.h"   
  2. #include "stdlib.h"   
  3. #include "string.h"   
  4.   
  5. #define MAX 2    //车库容量    
  6. #define price 0.05    //每车每分钟费用   
  7. typedef struct time  //时间结点   
  8. {   
  9.     int hour;   
  10.     int min;   
  11. }Time;  
  12. typedef struct node //车辆信息结点    
  13. {   
  14.     char num[10];   
  15.     Time reach;   
  16.     Time leave;   
  17. }CarNode;  
  18. typedef struct NODE   //模拟车站   
  19. {   
  20.     CarNode *stack[MAX+1];   
  21.     int top;   
  22. }SeqStackCar;  
  23. typedef struct car  
  24. {   
  25.     CarNode *data;   
  26.     struct car *next;   
  27. }QueueNode;   
  28. typedef struct Node  //模拟通道   
  29. {   
  30.     QueueNode *head;   
  31.     QueueNode *rear;   
  32. }LinkQueueCar;  
  33.   
  34. void InitStack(SeqStackCar *); //初始化栈   
  35. int InitQueue(LinkQueueCar *); //初始化便道   
  36. int arrival(SeqStackCar *,LinkQueueCar *); //车辆到达   
  37. void leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *); //车辆离开    
  38. void list(SeqStackCar,LinkQueueCar); //显示存车信息   
  39.   
  40. int main(void)   
  41. {   
  42.     SeqStackCar Enter,Temp;   
  43.     LinkQueueCar Wait;   
  44.     int ch;   
  45.     InitStack(&Enter);    //初始化车站    
  46.     InitStack(&Temp);     //初始化让路的临时栈    
  47.     InitQueue(&Wait);     //初始化通道    
  48.     while(1)   
  49.     {   
  50.         printf("\n                1. The car arrive\n");   
  51.         printf("                2. The car leave\n");   
  52.         printf("                3. The schedule\n");   
  53.         printf("                4. Exit\n");   
  54.         while(1)   
  55.         {   
  56.             scanf("%d",&ch);   
  57.             if(ch>=1 && ch<=4)  
  58.                 break;   
  59.             else  
  60.                 printf("\nPlease choose: 1|2|3|4.");   
  61.         }   
  62.         switch(ch)   
  63.         {   
  64.         case 1:  
  65.             arrival(&Enter,&Wait);    //车辆到达   
  66.             break;  
  67.         case 2:  
  68.             leave(&Enter,&Temp,&Wait);    //车辆离开    
  69.             break;  
  70.         case 3:  
  71.             list(Enter,Wait);break;    //列表打印信息   
  72.         case 4:  
  73.             exit(0);    //退出主程序    
  74.         default:  
  75.             break;   
  76.         }   
  77.     }   
  78. }   
  79.   
  80. void InitStack(SeqStackCar *s) //初始化栈   
  81. {   
  82.     int i;   
  83.     s->top=0;   
  84.     for(i=0;i<=MAX;i++)   
  85.         s->stack[s->top]=NULL;   
  86. }   
  87. int InitQueue(LinkQueueCar *Q) //初始化便道   
  88. {   
  89.     Q->head=(QueueNode *)malloc(sizeof(QueueNode));   
  90.     if(Q->head!=NULL)   
  91.     {   
  92.         Q->head->next=NULL;   
  93.         Q->rear=Q->head;   
  94.         return 1;   
  95.     }   
  96.     else return -1;   
  97. }   
  98. void print(CarNode *p,int room) //打印出站车的信息   
  99. {   
  100.     int A1,A2,B1,B2;   
  101.     printf("\nplease input thedepart time:/**:**/");   
  102.     scanf("%d:%d",&(p->leave.hour),&(p->leave.min));   
  103.     printf("\nthe number of the car:");   
  104.     puts(p->num);   
  105.     printf("\nthe time the car arrive: %d:%d",p->reach.hour,p->reach.min);   
  106.     printf("the depart time: %d:%d",p->leave.hour,p->leave.min);   
  107.     A1=p->reach.hour;   
  108.     A2=p->reach.min;   
  109.     B1=p->leave.hour;   
  110.     B2=p->leave.min;   
  111.     printf("\nthe fee: %2.1f元",((B1-A1)*60+(B2-A2))*price);   
  112.     free(p);   
  113. }   
  114. int arrival(SeqStackCar *Enter,LinkQueueCar *W) //车辆到达   
  115. {   
  116.     CarNode *p;   
  117.     QueueNode *t;   
  118.     p=(CarNode *)malloc(sizeof(CarNode));   
  119.     flushall();   
  120.     printf("\ninput the number of the car(例:陕A1234):");   
  121.     gets(p->num);   
  122.     if(Enter->top<MAX)     //车场未满,车进车场   
  123.     {   
  124.         Enter->top++;   
  125.         printf("\nthe place of the car.",Enter->top);   
  126.         printf("\nthe time thecar arrive:/**:**/");   
  127.         scanf("%d:%d",&(p->reach.hour),&(p->reach.min));   
  128.         Enter->stack[Enter->top]=p;   
  129.         return 1;   
  130.     }   
  131.     else     //车场已满,车进便道   
  132.     {   
  133.         printf("\n该车须在便道等待!");   
  134.         t=(QueueNode *)malloc(sizeof(QueueNode));   
  135.         t->data=p;   
  136.         t->next=NULL;   
  137.         W->rear->next=t;   
  138.         W->rear=t;   
  139.         return 1;   
  140.     }   
  141. }   
  142. void leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W)    //车辆离开   
  143. {  
  144.     int i, room;   
  145.     CarNode *p,*t;   
  146.     QueueNode *q;   
  147.     //判断车场内是否有车   
  148.     if(Enter->top>0) //有车   
  149.     {   
  150.         while(1) //输入离开车辆的信息    
  151.         {   
  152.             printf("\n请输入车在车场的位置/1--%d/:",Enter->top);   
  153.             scanf("%d",&room);   
  154.             if(room>=1&&room<=Enter->top)  
  155.                 break;   
  156.         }   
  157.         while(Enter->top>room) //车辆离开    
  158.         {   
  159.             Temp->top++;   
  160.             Temp->stack[Temp->top]=Enter->stack[Enter->top];   
  161.             Enter->stack[Enter->top]=NULL;   
  162.             Enter->top--;   
  163.         }   
  164.         p=Enter->stack[Enter->top];   
  165.         Enter->stack[Enter->top]=NULL;   
  166.         Enter->top--;   
  167.         while(Temp->top>=1)   
  168.         {   
  169.             Enter->top++;   
  170.             Enter->stack[Enter->top]=Temp->stack[Temp->top];   
  171.             Temp->stack[Temp->top]=NULL;   
  172.             Temp->top--;   
  173.         }   
  174.         print(p,room);   
  175.         //判断通道上是否有车及车站是否已满   
  176.         if((W->head!=W->rear)&&Enter->top<MAX) //便道的车辆进入车场   
  177.         {   
  178.             q=W->head->next;   
  179.             t=q->data;   
  180.             Enter->top++;   
  181.             printf("\n便道的%s号车进入车场第%d位置.",t->num,Enter->top);   
  182.             printf("\n请输入现在的时间/**:**/:");   
  183.             scanf("%d:%d",&(t->reach.hour),&(t->reach.min));   
  184.             W->head->next=q->next;   
  185.             if(q==W->rear) W->rear=W->head;   
  186.             Enter->stack[Enter->top]=t;   
  187.             free(q);   
  188.         }   
  189.         else  
  190.             printf("\n便道里没有车.\n");   
  191.     }   
  192.     else  
  193.         printf("\n车场里没有车."); //没车   
  194. }   
  195. void list1(SeqStackCar *S)     //列表显示车场信息   
  196. {   
  197.     int i;   
  198.     if(S->top>0)    //判断车站内是否有车   
  199.     {   
  200.         printf("\n车场:");   
  201.         printf("\n 位置 到达时间 车牌号\n");   
  202.         for(i=1;i<=S->top;i++)   
  203.         {   
  204.             printf(" %d ",i);   
  205.             printf("%d:%d ",S->stack[i]->reach.hour,S->stack[i]->reach.min);   
  206.             puts(S->stack[i]->num);   
  207.         }   
  208.     }   
  209.     else  
  210.         printf("\n车场里没有车");   
  211. }   
  212. void list2(LinkQueueCar *W)   //列表显示便道信息   
  213. {   
  214.     QueueNode *p;   
  215.     p=W->head->next;   
  216.     if(W->head!=W->rear)   //判断通道上是否有车    
  217.     {   
  218.         printf("\n等待车辆的号码为:");   
  219.         while(p!=NULL)   
  220.         {   
  221.             puts(p->data->num);   
  222.             p=p->next;   
  223.         }   
  224.     }   
  225.     else  
  226.         printf("\n便道里没有车.");   
  227. }   
  228. void list(SeqStackCar S,LinkQueueCar W)   
  229. {   
  230.     int flag,tag;   
  231.     flag=1;   
  232.     while(flag)   
  233.     {   
  234.         printf("\n请选择 1|2|3:");   
  235.         printf("\n1.车场\n2.便道\n3.返回\n");   
  236.         while(1)   
  237.         {   
  238.             scanf("%d",&tag);   
  239.             if(tag>=1 || tag<=3)  
  240.                 break;   
  241.             else  
  242.                 printf("\n请选择 1|2|3:");   
  243.         }   
  244.         switch(tag)   
  245.         {  
  246.         case 1:  
  247.             list1(&S);  
  248.             break;    //列表显示车场信息   
  249.         case 2:  
  250.             list2(&W);  
  251.             break;    //列表显示便道信息   
  252.         case 3:  
  253.             flag=0;  
  254.             break;  
  255.         defaultbreak;  
  256.         }   
  257.     }   
  258. }  
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define MAX 2    //车库容量 
#define price 0.05    //每车每分钟费用
typedef struct time  //时间结点
{ 
	int hour; 
	int min; 
}Time;
typedef struct node //车辆信息结点 
{ 
	char num[10]; 
	Time reach; 
	Time leave; 
}CarNode;
typedef struct NODE   //模拟车站
{ 
	CarNode *stack[MAX+1]; 
	int top; 
}SeqStackCar;
typedef struct car
{ 
	CarNode *data; 
	struct car *next; 
}QueueNode; 
typedef struct Node  //模拟通道
{ 
	QueueNode *head; 
	QueueNode *rear; 
}LinkQueueCar;

void InitStack(SeqStackCar *); //初始化栈
int InitQueue(LinkQueueCar *); //初始化便道
int arrival(SeqStackCar *,LinkQueueCar *); //车辆到达
void leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *); //车辆离开 
void list(SeqStackCar,LinkQueueCar); //显示存车信息

int main(void) 
{ 
	SeqStackCar Enter,Temp; 
	LinkQueueCar Wait; 
	int ch; 
	InitStack(&Enter);    //初始化车站 
	InitStack(&Temp);     //初始化让路的临时栈 
	InitQueue(&Wait);     //初始化通道 
	while(1) 
	{ 
		printf("\n                1. The car arrive\n"); 
		printf("                2. The car leave\n"); 
		printf("                3. The schedule\n"); 
		printf("                4. Exit\n"); 
		while(1) 
		{ 
			scanf("%d",&ch); 
			if(ch>=1 && ch<=4)
				break; 
			else
				printf("\nPlease choose: 1|2|3|4."); 
		} 
		switch(ch) 
		{ 
		case 1:
			arrival(&Enter,&Wait);    //车辆到达
			break;
		case 2:
			leave(&Enter,&Temp,&Wait);    //车辆离开 
			break;
		case 3:
			list(Enter,Wait);break;    //列表打印信息
		case 4:
			exit(0);    //退出主程序 
		default:
			break; 
		} 
	} 
} 

void InitStack(SeqStackCar *s) //初始化栈
{ 
	int i; 
	s->top=0; 
	for(i=0;i<=MAX;i++) 
		s->stack[s->top]=NULL; 
} 
int InitQueue(LinkQueueCar *Q) //初始化便道
{ 
	Q->head=(QueueNode *)malloc(sizeof(QueueNode)); 
	if(Q->head!=NULL) 
	{ 
		Q->head->next=NULL; 
		Q->rear=Q->head; 
		return 1; 
	} 
	else return -1; 
} 
void print(CarNode *p,int room) //打印出站车的信息
{ 
	int A1,A2,B1,B2; 
	printf("\nplease input thedepart time:/**:**/"); 
	scanf("%d:%d",&(p->leave.hour),&(p->leave.min)); 
	printf("\nthe number of the car:"); 
	puts(p->num); 
	printf("\nthe time the car arrive: %d:%d",p->reach.hour,p->reach.min); 
	printf("the depart time: %d:%d",p->leave.hour,p->leave.min); 
	A1=p->reach.hour; 
	A2=p->reach.min; 
	B1=p->leave.hour; 
	B2=p->leave.min; 
	printf("\nthe fee: %2.1f元",((B1-A1)*60+(B2-A2))*price); 
	free(p); 
} 
int arrival(SeqStackCar *Enter,LinkQueueCar *W) //车辆到达
{ 
	CarNode *p; 
	QueueNode *t; 
	p=(CarNode *)malloc(sizeof(CarNode)); 
	flushall(); 
	printf("\ninput the number of the car(例:陕A1234):"); 
	gets(p->num); 
	if(Enter->top<MAX)     //车场未满,车进车场
	{ 
		Enter->top++; 
		printf("\nthe place of the car.",Enter->top); 
		printf("\nthe time thecar arrive:/**:**/"); 
		scanf("%d:%d",&(p->reach.hour),&(p->reach.min)); 
		Enter->stack[Enter->top]=p; 
		return 1; 
	} 
	else     //车场已满,车进便道
	{ 
		printf("\n该车须在便道等待!"); 
		t=(QueueNode *)malloc(sizeof(QueueNode)); 
		t->data=p; 
		t->next=NULL; 
		W->rear->next=t; 
		W->rear=t; 
		return 1; 
	} 
} 
void leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W)    //车辆离开
{
	int i, room; 
	CarNode *p,*t; 
	QueueNode *q; 
	//判断车场内是否有车
	if(Enter->top>0) //有车
	{ 
		while(1) //输入离开车辆的信息 
		{ 
			printf("\n请输入车在车场的位置/1--%d/:",Enter->top); 
			scanf("%d",&room); 
			if(room>=1&&room<=Enter->top)
				break; 
		} 
		while(Enter->top>room) //车辆离开 
		{ 
			Temp->top++; 
			Temp->stack[Temp->top]=Enter->stack[Enter->top]; 
			Enter->stack[Enter->top]=NULL; 
			Enter->top--; 
		} 
		p=Enter->stack[Enter->top]; 
		Enter->stack[Enter->top]=NULL; 
		Enter->top--; 
		while(Temp->top>=1) 
		{ 
			Enter->top++; 
			Enter->stack[Enter->top]=Temp->stack[Temp->top]; 
			Temp->stack[Temp->top]=NULL; 
			Temp->top--; 
		} 
		print(p,room); 
		//判断通道上是否有车及车站是否已满
		if((W->head!=W->rear)&&Enter->top<MAX) //便道的车辆进入车场
		{ 
			q=W->head->next; 
			t=q->data; 
			Enter->top++; 
			printf("\n便道的%s号车进入车场第%d位置.",t->num,Enter->top); 
			printf("\n请输入现在的时间/**:**/:"); 
			scanf("%d:%d",&(t->reach.hour),&(t->reach.min)); 
			W->head->next=q->next; 
			if(q==W->rear) W->rear=W->head; 
			Enter->stack[Enter->top]=t; 
			free(q); 
		} 
		else
			printf("\n便道里没有车.\n"); 
	} 
	else
		printf("\n车场里没有车."); //没车
} 
void list1(SeqStackCar *S)     //列表显示车场信息
{ 
	int i; 
	if(S->top>0)    //判断车站内是否有车
	{ 
		printf("\n车场:"); 
		printf("\n 位置 到达时间 车牌号\n"); 
		for(i=1;i<=S->top;i++) 
		{ 
			printf(" %d ",i); 
			printf("%d:%d ",S->stack[i]->reach.hour,S->stack[i]->reach.min); 
			puts(S->stack[i]->num); 
		} 
	} 
	else
		printf("\n车场里没有车"); 
} 
void list2(LinkQueueCar *W)   //列表显示便道信息
{ 
	QueueNode *p; 
	p=W->head->next; 
	if(W->head!=W->rear)   //判断通道上是否有车 
	{ 
		printf("\n等待车辆的号码为:"); 
		while(p!=NULL) 
		{ 
			puts(p->data->num); 
			p=p->next; 
		} 
	} 
	else
		printf("\n便道里没有车."); 
} 
void list(SeqStackCar S,LinkQueueCar W) 
{ 
	int flag,tag; 
	flag=1; 
	while(flag) 
	{ 
		printf("\n请选择 1|2|3:"); 
		printf("\n1.车场\n2.便道\n3.返回\n"); 
		while(1) 
		{ 
			scanf("%d",&tag); 
			if(tag>=1 || tag<=3)
				break; 
			else
				printf("\n请选择 1|2|3:"); 
		} 
		switch(tag) 
		{
		case 1:
			list1(&S);
			break;    //列表显示车场信息
		case 2:
			list2(&W);
			break;    //列表显示便道信息
		case 3:
			flag=0;
			break;
		default: break;
		} 
	} 
}

第二种方法:

  1. #include "stdio.h"   
  2. #include "stdlib.h"   
  3.   
  4. #define SIZE 10   
  5. typedef struct   
  6. {  
  7.     int hour;  
  8.     int min;   
  9. }time;      //车的时间结构体   
  10.   
  11. typedef struct   
  12. {  
  13.     int num;  
  14.     int position;  
  15.     time t;  
  16.     float money;   
  17. }Car;     //车的信息   
  18. typedef struct   
  19. {  
  20.     Car elem[SIZE+1];   
  21.     int top;       //指向便道中的第一个空位   
  22. } Stack;            //创建堆栈   
  23.   
  24. typedef struct Node   
  25. {  
  26.     Car data;   
  27.     struct Node *next;   
  28. }CQueueNode;  
  29.   
  30. //建立过道的程序:   
  31. typedef struct   
  32. {  
  33.     CQueueNode *front;  
  34.     CQueueNode *rear;   
  35. }LinkQueue;    //设置的便道   
  36.   
  37. //便道初始化程序   
  38. void InitQueue(LinkQueue *Q)   
  39. {  
  40.     Q->front=(CQueueNode*)malloc(sizeof(CQueueNode));  //使mallo返回的指针转换为指向CQueueNode类型数据的指针   
  41.     if(Q->front!=NULL)   
  42.     {  
  43.         Q->rear=Q->front;   
  44.         Q->front->next=NULL;  
  45.     }   
  46. }  
  47.   
  48. int EnterQueue(LinkQueue *Q,Car *t)   
  49. {  
  50.     CQueueNode *NewNode;   
  51.     NewNode=(CQueueNode*)malloc(sizeof(CQueueNode));    //给便道申请空间   
  52.     if(NewNode!=NULL)   
  53.     {  
  54.         NewNode->data.num=t->num;   
  55.         NewNode->data.t.hour=t->t.hour;   
  56.         NewNode->data.t.min=t->t.min;   
  57.         NewNode->next=NULL;   
  58.         Q->rear->next=NewNode;   
  59.         Q->rear=NewNode;   
  60.         return 1;   
  61.     }   
  62.     else return 0;   
  63. }   
  64. void InitStack(Stack *S)   
  65. {  
  66.     S->top=0;  
  67. }    //确保堆栈为空   
  68.   
  69. void Push(Stack *S,Car *r)       //便道中的车入库   
  70. {  
  71.     S->top++;   
  72.     S->elem[S->top].num=r->num;   
  73.     r->position=S->elem[S->top].position=S->top;   
  74.     S->elem[S->top].t.hour=r->t.hour;   
  75.     S->elem[S->top].t.min=r->t.min;   
  76. }  
  77. int IsEmpty(Stack* S)   //判断车库是否为空   
  78. {  
  79.     return(S->top==0?1:0);  
  80. }  
  81.   
  82. int IsFull(Stack *S)   //判断车库是否为满   
  83. {  
  84.     return(S->top==SIZE?1:0);  
  85. }  
  86.   
  87. int GetTop(Stack *S,Car *n)    //车离开车库   
  88. {  
  89.     n->num=S->elem[S->top].num;   
  90.     n->position=S->elem[S->top].position;   
  91.     n->t.hour=S->elem[S->top].t.hour;   
  92.     n->t.min=S->elem[S->top].t.min;   
  93.     return 1;  
  94. }  
  95. int DeleteQueue(LinkQueue *Q,Car *x)   
  96. {  
  97.     CQueueNode *p;   
  98.     if(Q->front==Q->rear)  
  99.         return 0;      //判断便道为空   
  100.     p=Q->front->next;      //将便道中的车放入车库   
  101.     Q->front->next=p->next;   
  102.     if(Q->rear==p)   
  103.         Q->rear=Q->front;   
  104.     x->num=p->data.num;   
  105.     x->t.hour=p->data.t.hour;   
  106.     x->t.min=p->data.t.min;   
  107.     free(p);      //释放临时指针   
  108.     return 1;   
  109. }   
  110. void In(Stack *S,LinkQueue *Q,Car*r)   
  111. {  
  112.     if(IsFull(S))   
  113.     {  
  114.         printf("车库已满,请等待!");   
  115.         EnterQueue(Q,r);     //车进入便道            
  116.     }  
  117.     else   
  118.     {  
  119.         Push(S,r);   
  120.         printf("\n您现在所在位置 %d",r->position);        //打印车的位置   
  121.     }   
  122. }  
  123. void TaM(Car *r,int h,int m)   
  124. {  
  125.     if(m>r->t.min)  
  126.     {  
  127.         r->t.min+=60;  
  128.         r->t.hour-=1;  
  129.     }  
  130.     h=r->t.hour-h;  
  131.     m=r->t.min-m;  
  132.     printf("\n停车 %d小时 %d 分钟\n",h,m);  
  133.     printf("每小时收费30元\n");  
  134.     h=h*60;m=h+m;   
  135.     r->money=0.5*m;   
  136.     printf("请支付金额%.2f元\n",r->money);      //输出车主应付金额   
  137. }  
  138. void Out(Stack *S,Stack *S0,Car *r,LinkQueue *Q)   
  139. {  
  140.     int tag=S->top;   
  141.     Car x;   
  142.     if(IsEmpty(S))  
  143.         printf("没有此车!");   
  144.     else   
  145.     {  
  146.         for(;r->num!=S->elem[tag].num && tag>0;tag--)   
  147.         {  
  148.             Push(S0,&S->elem[tag]);   
  149.             S->top--;   
  150.         }   
  151.         if(r->num==S->elem[tag].num)   
  152.         {  
  153.             TaM(r,S->elem[tag].t.hour,S->elem[tag].t.min);   
  154.             S->top--;   
  155.             for(;S0->top>0;S0->top--)   
  156.                 Push(S,&S0->elem[S0->top]);   
  157.             if(S->top<SIZE && Q->front!=Q->rear)    //判断车库是否有此车,有就找到此车,然后退出   
  158.             {  
  159.                 DeleteQueue(Q,&x);  
  160.                 Push(S,&x);   
  161.             }   
  162.         }  
  163.         else if(tag==0)   //过道中的车无需收车费   
  164.         {  
  165.             printf("未进入停车场应支付金额 0元!");   
  166.             for(;S0->top>0;S0->top--)  
  167.                 Push(S,&S0->elem[S0->top]);     
  168.         }   
  169.     }   
  170. }  
  171.   
  172. void print1(Stack *S)   
  173. {  
  174.     int tag;   
  175.     Car x;   
  176.     printf("停车场停车情况:\n");   
  177.     if(IsEmpty(S))  
  178.         printf("无车!");   
  179.     for(tag=S->top;S->top>0;S->top--)   
  180.         if(GetTop(S,&x))          //显示车库中个车的信息及到达时间   
  181.             printf("车牌号 %d,所在位置 %d,到达/离开时间 %d:%d\n",x.num,x.position,x.t.hour,x.t.min);   
  182.     S->top=tag;   
  183. }  
  184.   
  185. void print2(LinkQueue *Q)   
  186. {  
  187.     CQueueNode *p;   
  188.     p=Q->front->next;   
  189.     for(;p!=NULL;p=p->next)    //显示过道上车的信息及到达时间   
  190.         printf("等待车牌号 %d, 到达/离开时间 %d:%d",p->data.num,p->data.t.hour,p->data.t.min);   
  191. }  
  192.   
  193. void print()   
  194. {  
  195.     printf("\n***********************************欢迎光临*************************************\n");   
  196.     printf("\n 请选择:\n");   
  197.     printf("\n 1 :到达");   
  198.     printf("\n 2 :离开");   
  199.     printf("\n 3 :搜索");   
  200.     printf("\n 4 :退出\n");   
  201.     printf("\n");  
  202. }  
  203.   
  204. int main(void)   
  205. {  
  206.     int n,m,i=1,j,flag=0;  
  207.     Car c[10];  
  208.     Stack S,S0;       //设定堆栈S,SO   
  209.     LinkQueue Q;      //便道   
  210.     InitStack(&S);    //堆栈S   
  211.     InitStack(&S0);   //临时堆栈S0   
  212.     InitQueue(&Q);  
  213.     while(1)   
  214.     {  
  215.         print();   
  216.         scanf("%d",&m);   
  217.         switch(m)  
  218.         {  
  219.         case 1:  
  220.             printf("\n请输入车牌号:");   
  221.             scanf("%d",&c[i].num);   
  222.             printf("\n请输入到达/离开时间:");   
  223.             scanf("%d:%d",&c[i].t.hour,&c[i].t.min);   
  224.             In(&S,&Q,&c[i]);i++;     //车辆的情况   
  225.             break;  
  226.         case 2:  
  227.             printf("\n请输入车牌号:");   
  228.             scanf("%d",&n);   
  229.             for(j=0;j<10;j++)   
  230.                 if(n==c[j].num)  
  231.                     break;   
  232.             printf("\n请输入到达/离开时间:");   
  233.             scanf("%d:%d",&c[j].t.hour,&c[j].t.min);   
  234.             Out(&S,&S0,&c[j],&Q);    //车辆的情况   
  235.             break;              
  236.         case 3:  
  237.             print1(&S);      //输出车库中车的信息   
  238.             print2(&Q);      //输出过道上车的信息   
  239.             break;           //终止   
  240.         case 4:  
  241.             flag=1;  
  242.             break;   
  243.         default:  
  244.             printf("\n输入错误,请输入 1,2,3 或4");   
  245.         }   
  246.         if(flag)  
  247.             break;     //结束程序   
  248.     }  return 0;   
  249. }  
  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值