1、停车场管理系统代码/ 1、停车场管理.cpp : Defines the entry point for the console application./#include stdafx.h#include #include #include #include #include / 清空当前屏幕#define ClearScreen() system( cls )/设置背景前景颜色/#define setcolor() system(color 2f)/ 显示字符串 szPrompt 并等待用户按下任意键#define Pause( szPrompt ) printf( %s, szPromp
2、t ),getch()typedef struct carinformation / 车辆信息 char szRegistrationMark64; / 车牌号 char szArrivalTime16; / 到达时间 char szEntranceTime16; / 进入停车场(开始计费)时间 char szDepartureTime16; / 离开时间 TCARINFORMATION, *LPTCARINFORMATION;typedef struct carstack LPTCARINFORMATION lpCarInformation; / 车辆信息 int nTop; / 栈顶元素下
3、标 int nStackSize; / 栈容量 TCARSTACK, *LPTCARSTACK;/ 初始化栈 lpCarStack, 将其容量设置为 nSizevoid InitStack( LPTCARSTACK &lpCarStack, int nSize ) lpCarStack = ( LPTCARSTACK ) malloc( sizeof ( TCARSTACK ) ); lpCarStack-lpCarInformation = ( LPTCARINFORMATION ) malloc( nSize * sizeof ( TCARINFORMATION ) ); lpCarSta
4、ck-nTop = -1; lpCarStack-nStackSize = nSize;/ 车辆信息 carinfo 入栈 lpCarStackvoid Push( LPTCARSTACK &lpCarStack, TCARINFORMATION carinfo ) lpCarStack-nTop+; lpCarStack-lpCarInformationlpCarStack-nTop = carinfo;/ 车辆信息从栈 lpCarStack 中弹出并存入 carinfovoid Pop( LPTCARSTACK &lpCarStack, TCARINFORMATION &carinfo )
5、 carinfo = lpCarStack-lpCarInformationlpCarStack-nTop; lpCarStack-nTop-;/ 若栈 lpCarstack 空,返回 TRUE;否则,返回 FALSEBOOL IsStackEmpty( LPTCARSTACK lpCarStack ) return lpCarStack-nTop = -1;/ 若栈 lpStackFull 满,返回 TRUE;否则,返回 FALSEBOOL IsStackFull( LPTCARSTACK lpCarStack ) return lpCarStack-nTop = ( lpCarStack-
6、nStackSize - 1 );/ 销毁栈 lpCarStack,将指针 lpCarStack 置为 NULLvoid DestroyStack( LPTCARSTACK &lpCarStack ) free( lpCarStack-lpCarInformation ); free( lpCarStack ); lpCarStack = NULL;typedef struct carnode / 链队结点信息 TCARINFORMATION carinfo; / 车辆信息 struct carnode *lpNext; / 指向下一个元素的指针 TCARNODE, *LPTCARNODE;t
7、ypedef struct carqueue / 链队 LPTCARNODE lpHead; / 头结点 LPTCARNODE lpRear; / 指向当前队尾的指针 int nEffectiveSize; / 当前队中元素个数 TCARQUEUE, *LPTCARQUEUE;/ 初始化链队 lpCarQueuevoid InitQueue( LPTCARQUEUE &lpCarQueue ) lpCarQueue = ( LPTCARQUEUE ) malloc( sizeof( TCARQUEUE ) ); lpCarQueue-lpHead = ( LPTCARNODE) malloc(
8、 sizeof( TCARNODE ) ); lpCarQueue-lpHead-lpNext = NULL; lpCarQueue-lpRear = lpCarQueue-lpHead; lpCarQueue-nEffectiveSize = 0;/ 车辆信息 carinfo 入队 lpCarQueuevoid EnQueue( LPTCARQUEUE &lpCarQueue, TCARINFORMATION carinfo ) LPTCARNODE lpCarNode = ( LPTCARNODE ) malloc( sizeof( carnode ) ); lpCarNode-carin
9、fo = carinfo; lpCarNode-lpNext = NULL; lpCarQueue-lpRear-lpNext = lpCarNode; lpCarQueue-lpRear = lpCarQueue-lpRear-lpNext; lpCarQueue-nEffectiveSize+;/ 队头元素从链队 lpCarQueue 中出队并存入 carinfovoid DeQueue( LPTCARQUEUE &lpCarQueue, TCARINFORMATION &carinfo ) LPTCARNODE lpTemp = lpCarQueue-lpHead-lpNext; car
10、info = lpTemp-carinfo; lpCarQueue-lpHead-lpNext = lpTemp-lpNext; free( lpTemp ); lpCarQueue-nEffectiveSize-;/ 若链队 lpCarQueue 为空,返回 TRUE;否则,返回 FALSEBOOL IsQueueEmpty( LPTCARQUEUE lpCarQueue ) return lpCarQueue-nEffectiveSize = 0;/ 销毁链队 lpCarQueuevoid DestroyQueue( LPTCARQUEUE &lpCarQueue ) LPTCARNODE
11、 lpNextCarNode = NULL; for ( LPTCARNODE lpCarNode = lpCarQueue-lpHead; lpCarNode != NULL; lpCarNode = lpNextCarNode ) lpNextCarNode = lpCarNode-lpNext; free( lpCarNode ); free( lpCarQueue ); lpCarQueue = NULL;/ 将字符串时间格式转换为数字(分钟)格式,例如 12:36 将被转换为 756 ( 12 * 60 + 36 )int ConvertTimeFormat( char *lpTim
12、e ) int nHour = 0; int nMinute = 0; sscanf( lpTime, %d:%d, &nHour, &nMinute ); return nHour * 60 + nMinute;/ 根据在停车场内的停留时间 nContinuanceMinutes (分钟)计算费用double CalculateExpense( int nContinuanceMinutes ) return nContinuanceMinutes * ( 5.0 / 60 );int main( void )/ setcolor(); int nParkCapability = 0; /
13、停车场容量 putchar( n ); printf( 请输入停车场容量: ); scanf( %d, &nParkCapability ); LPTCARSTACK lpCarStack = NULL; / 停车场,用栈模拟豫 InitStack( lpCarStack, nParkCapability ); LPTCARQUEUE lpCarQueue = NULL; / 便道,用链队模拟 InitQueue( lpCarQueue ); char cCommandType = NULL; / 命令类型 char szUserInput128 = NULL ; / 用户输入 do Clea
14、rScreen(); /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 ); p
15、uts( - ); char szCarInformation128 = NULL ; sscanf( szUserInput, / 将命令类型与车辆信息分开存放 %c,%s, &cCommandType, / 用户输入的前半部分,即命令类型 szCarInformation / 用户输入的后半部分,即车辆信息 ); char *lpCommaLocation = NULL; / 车辆信息字符串中的逗号位置 for ( lpCommaLocation = szCarInformation; *lpCommaLocation != 0; lpCommaLocation+ ) if ( *lpCo
16、mmaLocation = , ) break; *lpCommaLocation = 0; TCARINFORMATION carinfo = NULL ; / 存储本次用户输入的车辆信息 strcpy( carinfo.szRegistrationMark, szCarInformation ); if ( cCommandType = A ) strcpy( carinfo.szArrivalTime, lpCommaLocation + 1 ); if ( FALSE = IsStackFull( lpCarStack ) ) strcpy( carinfo.szEntranceTim
17、e, carinfo.szArrivalTime ); Push( lpCarStack, carinfo ); printf( 已进入停车场第 %d 个车位n, lpCarStack-nTop + 1 ); printf( 车牌号:tt%sn, carinfo.szRegistrationMark ); printf( 进入时间:t%sn, carinfo.szEntranceTime ); puts( 是否收费:t是 ); else EnQueue( lpCarQueue, carinfo ); printf( 停车场已满,已停放在便道的第 %d 个车位n, lpCarQueue-nEff
18、ectiveSize ); printf( 车牌号:tt%sn, carinfo.szRegistrationMark ); printf( 停放时间:t%sn, carinfo.szArrivalTime ); puts( 是否收费:t否 ); else if ( cCommandType = D ) strcpy( carinfo.szDepartureTime, lpCommaLocation + 1 ); LPTCARSTACK lpTempCarStack = NULL; InitStack( lpTempCarStack, nParkCapability ); TCARINFORM
19、ATION carinfoOut = NULL ; BOOL bIsCarFound = FALSE; while ( FALSE = IsStackEmpty( lpCarStack ) ) Pop( lpCarStack, carinfoOut ); if ( 0 != strcmp( carinfoOut.szRegistrationMark, carinfo.szRegistrationMark ) ) Push( lpTempCarStack, carinfoOut ); else bIsCarFound = TRUE; break; while ( FALSE = IsStackE
20、mpty( lpTempCarStack ) ) TCARINFORMATION tempcarinfo = NULL ; Pop( lpTempCarStack, tempcarinfo ); Push( lpCarStack, tempcarinfo ); if ( FALSE = bIsCarFound ) printf( 车牌号为 %s 的车未进入停车场.n, carinfo.szRegistrationMark ); Pause( -n按任意键输入下一条信息.n ); continue; strcpy( carinfoOut.szDepartureTime, carinfo.szDe
21、partureTime ); int nEntranceTime = ConvertTimeFormat( carinfoOut.szEntranceTime ); int nDepartureTime = ConvertTimeFormat( carinfoOut.szDepartureTime ); int nContinuanceMinutes = nDepartureTime - nEntranceTime; printf( 计费时段:t%s - %s (共 %d 分钟)n, carinfoOut.szEntranceTime, carinfoOut.szDepartureTime,
22、nContinuanceMinutes ); double rExpense = CalculateExpense( nContinuanceMinutes ); printf( 应交纳的费用:t%.1lf 元n, rExpense ); if ( FALSE = IsQueueEmpty( lpCarQueue ) ) TCARINFORMATION tempcarinfo = NULL ; DeQueue( lpCarQueue, tempcarinfo ); strcpy( tempcarinfo.szEntranceTime, carinfoOut.szDepartureTime );
23、 Push( lpCarStack, tempcarinfo ); puts( - ); printf( 停放在便道的第 1 个车位,车牌号为 %s 的车已进入停车场n, tempcarinfo.szRegistrationMark ); else if ( cCommandType = E ) puts( * ); puts( 姓名: 吴远彦 n ); puts( 学号:20101003346 n ); puts( * ); break; else if ( cCommandType = O ) ClearScreen(); /setcolor(); putchar( n ); puts(
24、停车场使用情况n ); puts( 车位t车牌号t到达时间t进入(开始计费)时间n); for ( int i = 0; i nTop; i+ ) printf( %dt%stt%stt%sn, i + 1, lpCarStack-lpCarInformationi.szRegistrationMark, lpCarStack-lpCarInformationi.szArrivalTime, lpCarStack-lpCarInformationi.szEntranceTime ); putchar( n ); putchar( n ); putchar( n ); puts( 便道使用情况n
25、 ); puts( 车位t车牌号t到达时间t进入(开始计费)时间n); int nNum = 0; for ( LPTCARNODE lpCarNode = lpCarQueue-lpHead-lpNext; lpCarNode != NULL; lpCarNode = lpCarNode-lpNext ) nNum+; printf( %dt%stt%stt%sn, 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 ); return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1