停车场管理系统代码.docx
《停车场管理系统代码.docx》由会员分享,可在线阅读,更多相关《停车场管理系统代码.docx(14页珍藏版)》请在冰豆网上搜索。
![停车场管理系统代码.docx](https://file1.bdocx.com/fileroot1/2023-2/24/35f89b83-4fa1-418e-a78d-549e165b14e1/35f89b83-4fa1-418e-a78d-549e165b14e11.gif)
停车场管理系统代码
//1、停车场管理.cpp:
Definestheentrypointfortheconsoleapplication.
//
#include"stdafx.h"
#include
#include
#include
#include
#include
//清空当前屏幕
#defineClearScreen()system("cls")
//设置背景前景颜色
//#definesetcolor()system("color2f")
//显示字符串szPrompt并等待用户按下任意键
#definePause(szPrompt)printf("%s",szPrompt),getch()
typedefstructcarinformation//车辆信息
{
charszRegistrationMark[64];//车牌号
charszArrivalTime[16];//到达时间
charszEntranceTime[16];//进入停车场(开始计费)时间
charszDepartureTime[16];//离开时间
}TCARINFORMATION,*LPTCARINFORMATION;
typedefstructcarstack
{
LPTCARINFORMATIONlpCarInformation;//车辆信息
intnTop;//栈顶元素下标
intnStackSize;//栈容量
}TCARSTACK,*LPTCARSTACK;
//初始化栈lpCarStack,将其容量设置为nSize
voidInitStack(LPTCARSTACK&lpCarStack,intnSize)
{
lpCarStack=(LPTCARSTACK)malloc(sizeof(TCARSTACK));
lpCarStack->lpCarInformation=(LPTCARINFORMATION)malloc(
nSize*sizeof(TCARINFORMATION)
);
lpCarStack->nTop=-1;
lpCarStack->nStackSize=nSize;
}
//车辆信息carinfo入栈lpCarStack
voidPush(LPTCARSTACK&lpCarStack,TCARINFORMATIONcarinfo)
{
lpCarStack->nTop++;
lpCarStack->lpCarInformation[lpCarStack->nTop]=carinfo;
}
//车辆信息从栈lpCarStack中弹出并存入carinfo
voidPop(LPTCARSTACK&lpCarStack,TCARINFORMATION&carinfo)
{
carinfo=lpCarStack->lpCarInformation[lpCarStack->nTop];
lpCarStack->nTop--;
}
//若栈lpCarstack空,返回TRUE;否则,返回FALSE
BOOLIsStackEmpty(LPTCARSTACKlpCarStack)
{
returnlpCarStack->nTop==-1;
}
//若栈lpStackFull满,返回TRUE;否则,返回FALSE
BOOLIsStackFull(LPTCARSTACKlpCarStack)
{
returnlpCarStack->nTop==(lpCarStack->nStackSize-1);
}
//销毁栈lpCarStack,将指针lpCarStack置为NULL
voidDestroyStack(LPTCARSTACK&lpCarStack)
{
free(lpCarStack->lpCarInformation);
free(lpCarStack);
lpCarStack=NULL;
}
typedefstructcarnode//链队结点信息
{
TCARINFORMATIONcarinfo;//车辆信息
structcarnode*lpNext;//指向下一个元素的指针
}TCARNODE,*LPTCARNODE;
typedefstructcarqueue//链队
{
LPTCARNODElpHead;//头结点
LPTCARNODElpRear;//指向当前队尾的指针
intnEffectiveSize;//当前队中元素个数
}TCARQUEUE,*LPTCARQUEUE;
//初始化链队lpCarQueue
voidInitQueue(LPTCARQUEUE&lpCarQueue)
{
lpCarQueue=(LPTCARQUEUE)malloc(sizeof(TCARQUEUE));
lpCarQueue->lpHead=(LPTCARNODE)malloc(sizeof(TCARNODE));
lpCarQueue->lpHead->lpNext=NULL;
lpCarQueue->lpRear=lpCarQueue->lpHead;
lpCarQueue->nEffectiveSize=0;
}
//车辆信息carinfo入队lpCarQueue
voidEnQueue(LPTCARQUEUE&lpCarQueue,TCARINFORMATIONcarinfo)
{
LPTCARNODElpCarNode=(LPTCARNODE)malloc(sizeof(carnode));
lpCarNode->carinfo=carinfo;
lpCarNode->lpNext=NULL;
lpCarQueue->lpRear->lpNext=lpCarNode;
lpCarQueue->lpRear=lpCarQueue->lpRear->lpNext;
lpCarQueue->nEffectiveSize++;
}
//队头元素从链队lpCarQueue中出队并存入carinfo
voidDeQueue(LPTCARQUEUE&lpCarQueue,TCARINFORMATION&carinfo)
{
LPTCARNODElpTemp=lpCarQueue->lpHead->lpNext;
carinfo=lpTemp->carinfo;
lpCarQueue->lpHead->lpNext=lpTemp->lpNext;
free(lpTemp);
lpCarQueue->nEffectiveSize--;
}
//若链队lpCarQueue为空,返回TRUE;否则,返回FALSE
BOOLIsQueueEmpty(LPTCARQUEUElpCarQueue)
{
returnlpCarQueue->nEffectiveSize==0;
}
//销毁链队lpCarQueue
voidDestroyQueue(LPTCARQUEUE&lpCarQueue)
{
LPTCARNODElpNextCarNode=NULL;
for(LPTCARNODElpCarNode=lpCarQueue->lpHead;lpCarNode!
=NULL;lpCarNode=lpNextCarNode)
{
lpNextCarNode=lpCarNode->lpNext;
free(lpCarNode);
}
free(lpCarQueue);
lpCarQueue=NULL;
}
//将字符串时间格式转换为数字(分钟)格式,例如12:
36将被转换为756(12*60+36)
intConvertTimeFormat(char*lpTime)
{
intnHour=0;
intnMinute=0;
sscanf(lpTime,"%d:
%d",&nHour,&nMinute);
returnnHour*60+nMinute;
}
//根据在停车场内的停留时间nContinuanceMinutes(分钟)计算费用
doubleCalculateExpense(intnContinuanceMinutes)
{
returnnContinuanceMinutes*(5.0/60);
}
intmain(void)
{
//setcolor();
intnParkCapability=0;//停车场容量
putchar('\n');
printf("请输入停车场容量:
");
scanf("%d",&nParkCapability);
LPTCARSTACKlpCarStack=NULL;//停车场,用栈模拟豫
InitStack(lpCarStack,nParkCapability);
LPTCARQUEUElpCarQueue=NULL;//便道,用链队模拟
InitQueue(lpCarQueue);
charcCommandType=NULL;//命令类型
charszUserInput[128]={NULL};//用户输入
do
{
ClearScreen();
//setcolor();
putchar('\n');
puts("--------------------");
puts("[命令类型]");
puts("A-车辆到达");
puts("D-车辆离开");
puts("E-停止输入");
puts("O-显示当前停车场和便道使用情况");
putchar('\n');
puts("例:
");
puts("A,鄂A3926,13:
33");
puts("D,鄂A3926,14:
44");
puts("E");
puts("O");
putchar('\n');
printf("请输入命令:
");
scanf("%s",szUserInput);
puts("--------------------");
charszCarInformation[128]={NULL};
sscanf(szUserInput,//将命令类型与车辆信息分开存放
"%c,%s",
&cCommandType,//用户输入的前半部分,即命令类型
szCarInformation//用户输入的后半部分,即车辆信息
);
char*lpCommaLocation=NULL;//车辆信息字符串中的逗号位置
for(lpCommaLocation=szCarInformation;*lpCommaLocation!
='\0';lpCommaLocation++)
{
if(*lpCommaLocation==',')
{
break;
}
}
*lpCommaLocation='\0';
TCARINFORMATIONcarinfo={NULL};//存储本次用户输入的车辆信息
strcpy(carinfo.szRegistrationMark,szCarInformation);
if(cCommandType=='A')
{
strcpy(carinfo.szArrivalTime,lpCommaLocation+1);
if(FALSE==IsStackFull(lpCarStack))
{
strcpy(carinfo.szEntranceTime,carinfo.szArrivalTime);
Push(lpCarStack,carinfo);
printf("已进入停车场第%d个车位\n",
lpCarStack->nTop+1
);
printf("车牌号:
\t\t%s\n",carinfo.szRegistrationMark);
printf("进入时间:
\t%s\n",carinfo.szEntranceTime);
puts("是否收费:
\t是");
}
else
{
EnQueue(lpCarQueue,carinfo);
printf("停车场已满,已停放在便道的第%d个车位\n",
lpCarQueue->nEffectiveSize
);
printf("车牌号:
\t\t%s\n",carinfo.szRegistrationMark);
printf("停放时间:
\t%s\n",carinfo.szArrivalTime);
puts("是否收费:
\t否");
}
}
elseif(cCommandType=='D')
{
strcpy(carinfo.szDepartureTime,lpCommaLocation+1);
LPTCARSTACKlpTempCarStack=NULL;
InitStack(lpTempCarStack,nParkCapability);
TCARINFORMATIONcarinfoOut={NULL};
BOOLbIsCarFound=FALSE;
while(FALSE==IsStackEmpty(lpCarStack))
{
Pop(lpCarStack,carinfoOut);
if(0!
=strcmp(carinfoOut.szRegistrationMark,carinfo.szRegistrationMark))
{
Push(lpTempCarStack,carinfoOut);
}
else
{
bIsCarFound=TRUE;
break;
}
}
while(FALSE==IsStackEmpty(lpTempCarStack))
{
TCARINFORMATIONtempcarinfo={NULL};
Pop(lpTempCarStack,tempcarinfo);
Push(lpCarStack,tempcarinfo);
}
if(FALSE==bIsCarFound)
{
printf("车牌号为%s的车未进入停车场.\n",carinfo.szRegistrationMark);
Pause("--------------------\n按任意键输入下一条信息...\n");
continue;
}
strcpy(carinfoOut.szDepartureTime,carinfo.szDepartureTime);
intnEntranceTime=ConvertTimeFormat(carinfoOut.szEntranceTime);
intnDepartureTime=ConvertTimeFormat(carinfoOut.szDepartureTime);
intnContinuanceMinutes=nDepartureTime-nEntranceTime;
printf("计费时段:
\t%s-%s(共%d分钟)\n",
carinfoOut.szEntranceTime,
carinfoOut.szDepartureTime,
nContinuanceMinutes
);
doublerExpense=CalculateExpense(nContinuanceMinutes);
printf("应交纳的费用:
\t%.1lf元\n",rExpense);
if(FALSE==IsQueueEmpty(lpCarQueue))
{
TCARINFORMATIONtempcarinfo={NULL};
DeQueue(lpCarQueue,tempcarinfo);
strcpy(tempcarinfo.szEntranceTime,carinfoOut.szDepartureTime);
Push(lpCarStack,tempcarinfo);
puts("--------------------");
printf("停放在便道的第1个车位,车牌号为%s的车已进入停车场\n",
tempcarinfo.szRegistrationMark
);
}
}
elseif(cCommandType=='E')
{
puts("********************");
puts("姓名:
吴远彦\n");
puts("学号:
20101003346\n");
puts("********************");
break;
}
elseif(cCommandType=='O')
{
ClearScreen();
//setcolor();
putchar('\n');
puts("[停车场使用情况]\n");
puts("[车位]\t[车牌号]\t[到达时间]\t[进入(开始计费)时间]\n");
for(inti=0;i<=lpCarStack->nTop;i++)
{
printf("%d\t%s\t\t%s\t\t%s\n",
i+1,
lpCarStack->lpCarInformation[i].szRegistrationMark,
lpCarStack->lpCarInformation[i].szArrivalTime,
lpCarStack->lpCarInformation[i].szEntranceTime
);
}
putchar('\n');
putchar('\n');
putchar('\n');
puts("[便道使用情况]\n");
puts("[车位]\t[车牌号]\t[到达时间]\t[进入(开始计费)时间]\n");
intnNum=0;
for(LPTCARNODElpCarNode=lpCarQueue->lpHead->lpNext;
lpCarNode!
=NULL;lpCarNode=lpCarNode->lpNext)
{
nNum++;
printf("%d\t%s\t\t%s\t\t%s\n",
nNum,
lpCarNode->carinfo.szRegistrationMark,
lpCarNode->carinfo.szArrivalTime,
lpCarNode->carinfo.szEntranceTime
);
}
putchar('\n');
}
else
{
puts("输入信息有误.第一个字符只能为'A'或'D'或'E'或'O'(区分大小写).");
}
Pause("--------------------\n按任意键输入下一条信息.\n");
}while(TRUE);
DestroyStack(lpCarStack);
DestroyQueue(lpCarQueue);
Pause("\n按任意键退出程序...\n");
return0;
}