⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stopcar1.c

📁 《数据结构-使用C语言》第三版
💻 C
字号:
//停车场管理模型
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<malloc.h>
#include<stdlib.h>

typedef struct Car  //进站时应保存车的数据信息 
{
	char name[10];
	int number;
	int h;
	int m;
	int s;
}DataType;
  
#define  MaxStackSize 4 //宏定义停车场的车位数量 

#include"SeqStack.h"
#include"LQNode.h"

SeqStack stackin, stackout; 
LQueue queue;

int main()
{
	int i;
	int hour, min, sec;
	char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; 
	time_t timep; 
	struct tm *p;
	
	DataType x, y;
    
	int Judgeio(char str[]);
	//int 
	
	printf("***********欢迎使用停车场管理系统***********\n");
	time(&timep);
	p=localtime(&timep); //取得当地时间
	printf("现在时间是: %d年 %d月 %d日\n",(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);  
	printf("\t    %s %d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
	printf("***********若要退出程序请输入exit***********\n\n"); 
	
	StackInitiate(&stackin);
	StackInitiate(&stackout);
	QueueInitiate(&queue);
	
	while(1)
	{
		int t,io,i;
		char str[10];
		scanf("%s",str);
		if(strcmp("exit",str)==0)  //退出程序 
		{
			printf("\n*********谢谢使用!*********\n"); 
			break;
		}
		else if(strcmp("in",str)==0)io=1; //进站还是出战 
		else if(strcmp("out",str)==0)io=-1;
		
		scanf("%s",str);
		t=Judgeio(str);  //判断车在停车场的位置 
		if(t==-1 && io==-1) 
		{
			printf("error:停车场中无此车\n\n"); 
			continue;
		}
		if(t>=0 && io==1)
		{
			printf("error:此车已经在停车场中\n\n");
			continue;
		}
		
		time(&timep);
		p=localtime(&timep);  //获取此时的时间信息 
		strcpy(x.name, str);
		x.h=p->tm_hour;
		x.m=p->tm_min;
		x.s=p->tm_sec;
		
		if(io==1)//进站 
		{
			if(stackin.top<MaxStackSize)
			{
				printf("\t可停放在停车场内第 %d 号车位\n",stackin.top); 
				x.number=stackin.top;
				StackPush(&stackin,x);
			}
			else
			{
				printf("\t可停放在便道上\n"); 
				x.number=-1;
				QueueAppend(&queue, x);
			}
		}
		else   //出站 
		{
			if(t<MaxStackSize)
			{
				printf("\t此车停放在停车场内,正在出栈...\n");
				for(i=stackin.top-1; i>t; i--)
				{
					StackPop(&stackin,&y);
					printf("\t%s 正在出栈...\n",y.name);
					StackPush(&stackout, y);
				}
				StackPop(&stackin,&y);
				
				hour=x.h - y.h;
				min=x.m - y.m;
				sec=x.s - y.s;
				if(sec<0)
				{
					min--;
					sec+=60;
				}
				if(min<0)
				{
					hour--;
					min+=60;
				}
				printf("此车在停车场内停留的时间为: %d:%d:%d\n\n",hour,min,sec);
				for(i=stackout.top-1; i>=0;i--)
				{
					StackPop(&stackout,&y);
					StackPush(&stackin, y);
				}
				
				if(queue.front!=NULL)
				{
					QueueDelete(&queue, &y);
					y.h=x.h;
					y.m=x.m;
					y.s=x.s;
					StackPush(&stackin, y);
				}
			}
			else if(t>=MaxStackSize)
			{
				printf("\t此车停放在便道上\n");
				i=0;
				while(1)
				{
					if(strcmp(str,queue.front->data.name)==0)break;
					i++;
					QueueDelete(&queue, &y);
					StackPush(&stackout, y);
				}
				QueueDelete(&queue, &y);
				
			    hour=x.h - y.h;
				min=x.m - y.m;
				sec=x.s - y.s;
				if(sec<0)
				{
					min--;
					sec+=60;
				}
				if(min<0)
				{
					hour--;
					min+=60;
				}
				printf("此车在停车场内停留的时间为: %d:%d:%d\n\n",hour,min,sec);
				while(i--)
				{
					StackPop(&stackout, &y);
					QueueAppend(&queue, y);
				}
			}
		}
	}
	return 0;
}

int Judgeio(char str[])
{
	int i;
	LQNode *p;
	for(i=0;i<stackin.top;i++)
		if(strcmp(str,stackin.stack[i].name)==0)return i;//车在堆栈中,返回在堆栈中的位置 
	
	p=queue.front;
	while(p!=NULL)
	{
		if(strcmp(str,p->data.name)==0)return MaxStackSize; //车在队列中 
		p=p->next;
	}
	
	return -1; //停车场中无此车 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -