#include "stdlib.h"
#define n 100
#define NULL 0
typedef struct
{
int data[n]
int top
}sqstack
void initstack(sqstack *&s)
{
s=new sqstack
s->top=-1
}
int push(sqstack *s,int e)
{
if(s->top==n-1)
return 0
s->top++
s->data[s->top]=e
return 1
}
int pop(sqstack *s,int x)
{
int a[n],e,i=0,j
if(s->top==-1)
{
printf("停车场是空的!\n")
return 0
}
while(s->data[s->top]!=x)
{
e=s->data[s->top]
s->top--
if(s->top==-1)
{
printf("你要找的车辆不在停车场内\n")
return 0
}
a[i]=e
i++
}
s->top--
for(j=i-1j>=0j--)
{
s->top++
s->data[s->top]=a[j]
}
return (s->top)
}
void dispstack(sqstack *s)
{
int i
for(i=s->拿扮郑topi>=0i--)
printf("%4d",s->data[i])
printf("\n")
}
void main()
{
int x,y,i=0
sqstack *s=NULL
initstack(s)
do
{
printf("请输入命令(1、到消颂达;2、离开;3、浏览停车场;0、退出)及车牌号\n")
scanf("%d",&y)
switch(y)
{
case 1:
{
scanf("%d",&x) //输入车牌号
push(s,x)
i++
printf("%d号车在停车场的位置缺丛是:%d\n",x,i)
break
}
case 2:
{
scanf("%d",&x) //输入车牌号
i=pop(s,x)
if(i==0)
break
printf("该车离开后停车场剩余的车为:\n")
dispstack(s)
break
}
case 3:
{
printf("停车场中的车辆为:\n")
dispstack(s)
break
}
case 0:
{
exit(0)
break
}
}
}while(y<=3)
}
把扩展名改成CPP,然后编译连接///////////////////////////////
下面这个程序是我大二上数据结构课时交的作业,现在正好电脑里面放着,所以就给你,应该满足你老师的要求了.编译环境是 VC++ 6.0
如果不会用的话就在问题补充里说,那个"例"是我临时给你打上去的,随便写了点,你要看不懂例子不会输命令就问我.
/*****************************************************************/
#include <conio.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
// 清空当前屏幕
#define ClearScreen() system( "cls" )
// 显示字符串 szPrompt 并等待用户按下任意键
#define Pause( szPrompt ) printf( "%s", szPrompt ),getch()
typedef struct carinformation// 车辆信息
{
char szRegistrationMark[64] // 车牌号
char szArrivalTime[16] // 到达时间
char szEntranceTime[16] // 进入停车场(开始计费)时间
char szDepartureTime[16] // 离开羡昌时间
} TCARINFORMATION, *LPTCARINFORMATION
typedef struct carstack
{
LPTCARINFORMATION lpCarInformation // 车辆信息
int nTop// 栈顶元素下标
int nStackSize// 栈容量
} TCARSTACK, *LPTCARSTACK
// 初始化栈 lpCarStack, 将其容量设置为 nSize
void InitStack( LPTCARSTACK &lpCarStack, int nSize )
{
lpCarStack = ( LPTCARSTACK ) malloc( sizeof ( TCARSTACK ) )
lpCarStack->银让lpCarInformation = ( LPTCARINFORMATION ) malloc(
nSize * sizeof ( TCARINFORMATION )
)
lpCarStack->nTop = -1
lpCarStack->nStackSize = nSize
}
// 车辆信息 carinfo 入栈 lpCarStack
void Push( LPTCARSTACK &lpCarStack, TCARINFORMATION carinfo )
{
lpCarStack->nTop++
lpCarStack->lpCarInformation[lpCarStack->nTop] = carinfo
}
// 车辆信息从栈 lpCarStack 中d出并存入 carinfo
void Pop( LPTCARSTACK &lpCarStack, TCARINFORMATION &carinfo )
{
carinfo = lpCarStack->lpCarInformation[lpCarStack->nTop]
lpCarStack->nTop--
}
// 若栈 lpCarstack 空,返回 TRUE否则,返回 FALSE
BOOL IsStackEmpty( LPTCARSTACK lpCarStack )
{
return lpCarStack->nTop == -1
}
// 若栈 lpStackFull 满,返回 TRUE否则,返回 FALSE
BOOL IsStackFull( LPTCARSTACK lpCarStack )
{
return lpCarStack->nTop == ( lpCarStack->nStackSize - 1 )
}
//锋派局 销毁栈 lpCarStack,将指针 lpCarStack 置为 NULL
void DestroyStack( LPTCARSTACK &lpCarStack )
{
free( lpCarStack->lpCarInformation )
free( lpCarStack )
lpCarStack = NULL
}
typedef struct carnode // 链队结点信息
{
TCARINFORMATION carinfo // 车辆信息
struct carnode *lpNext // 指向下一个元素的指针
} TCARNODE, *LPTCARNODE
typedef struct carqueue // 链队
{
LPTCARNODE lpHead // 头结点
LPTCARNODE lpRear // 指向当前队尾的指针
int nEffectiveSize // 当前队中元素个数
} TCARQUEUE, *LPTCARQUEUE
// 初始化链队 lpCarQueue
void InitQueue( 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
void EnQueue( LPTCARQUEUE &lpCarQueue, TCARINFORMATION carinfo )
{
LPTCARNODE lpCarNode = ( LPTCARNODE ) malloc( sizeof( carnode ) )
lpCarNode->carinfo = carinfo
lpCarNode->lpNext = NULL
lpCarQueue->lpRear->lpNext = lpCarNode
lpCarQueue->lpRear = lpCarQueue->lpRear->lpNext
lpCarQueue->nEffectiveSize++
}
// 队头元素从链队 lpCarQueue 中出队并存入 carinfo
void DeQueue( LPTCARQUEUE &lpCarQueue, TCARINFORMATION &carinfo )
{
LPTCARNODE lpTemp = lpCarQueue->lpHead->lpNext
carinfo = lpTemp->carinfo
lpCarQueue->lpHead->lpNext = lpTemp->lpNext
free( lpTemp )
lpCarQueue->nEffectiveSize--
}
// 若链队 lpCarQueue 为空,返回 TRUE否则,返回 FALSE
BOOL IsQueueEmpty( LPTCARQUEUE lpCarQueue )
{
return lpCarQueue->nEffectiveSize == 0
}
// 销毁链队 lpCarQueue
void DestroyQueue( LPTCARQUEUE &lpCarQueue )
{
LPTCARNODE lpNextCarNode = NULL
for ( LPTCARNODE lpCarNode = lpCarQueue->lpHeadlpCarNode != NULLlpCarNode = lpNextCarNode )
{
lpNextCarNode = lpCarNode->lpNext
free( lpCarNode )
}
free( lpCarQueue )
lpCarQueue = NULL
}
// 将字符串时间格式转换为数字(分钟)格式,例如 12:36 将被转换为 756 ( 12 * 60 + 36 )
int ConvertTimeFormat( char *lpTime )
{
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 )
{
int nParkCapability = 0 // 停车场容量
putchar( '\n' )
printf( "请输入停车场容量:" )
scanf( "%d", &nParkCapability )
LPTCARSTACK lpCarStack = NULL // 停车场,用栈模拟
InitStack( lpCarStack, nParkCapability )
LPTCARQUEUE lpCarQueue = NULL // 便道,用链队模拟
InitQueue( lpCarQueue )
char cCommandType = NULL // 命令类型
char szUserInput[128] = { NULL } // 用户输入
do
{
ClearScreen()
putchar( '\n' )
puts( "--------------------" )
puts( "[命令类型]" )
puts( "A - 车辆到达" )
puts( "D - 车辆离开" )
puts( "E - 停止输入" )
puts( "O - 显示当前停车场和便道使用情况" )
putchar( '\n' )
puts( "例:" )
puts( "A,冀A1234,14:26" )
puts( "D,冀A1234,16:51" )
puts( "E" )
puts( "O" )
putchar( '\n' )
printf( "请输入命令:" )
scanf( "%s", szUserInput )
puts( "--------------------" )
char szCarInformation[128] = { NULL }
sscanf( szUserInput, // 将命令类型与车辆信息分开存放
"%c,%s",
&cCommandType, // 用户输入的前半部分,即命令类型
szCarInformation // 用户输入的后半部分,即车辆信息
)
char *lpCommaLocation = NULL // 车辆信息字符串中的逗号位置
for ( lpCommaLocation = szCarInformation*lpCommaLocation != '\0'lpCommaLocation++ )
{
if ( *lpCommaLocation == ',' )
{
break
}
}
*lpCommaLocation = '\0'
TCARINFORMATION carinfo = { 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否" )
}
}
else if ( cCommandType == 'D' )
{
strcpy( carinfo.szDepartureTime, lpCommaLocation + 1 )
LPTCARSTACK lpTempCarStack = NULL
InitStack( lpTempCarStack, nParkCapability )
TCARINFORMATION 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 == IsStackEmpty( 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.szDepartureTime )
int nEntranceTime = ConvertTimeFormat( carinfoOut.szEntranceTime )
int nDepartureTime = ConvertTimeFormat( carinfoOut.szDepartureTime )
int nContinuanceMinutes = nDepartureTime - nEntranceTime
printf( "计费时段:\t%s - %s (共 %d 分钟)\n",
carinfoOut.szEntranceTime,
carinfoOut.szDepartureTime,
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 )
Push( lpCarStack, tempcarinfo )
puts( "--------------------" )
printf( "停放在便道的第 1 个车位,车牌号为 %s 的车已进入停车场\n",
tempcarinfo.szRegistrationMark
)
}
}
else if ( cCommandType == 'E' )
{
puts( "********************" )
puts( "陈赛 - Build20090507\n" )
puts( "********************" )
break
}
else if ( cCommandType == 'O' )
{
ClearScreen()
putchar( '\n' )
puts( "[停车场使用情况]\n" )
puts( "[车位]\t[车牌号]\t[到达时间]\t[进入(开始计费)时间]\n")
for ( int i = 0i <= lpCarStack->nTopi++ )
{
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")
int nNum = 0
for ( LPTCARNODE lpCarNode = lpCarQueue->lpHead->lpNext
lpCarNode != NULLlpCarNode = 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" )
return 0
}
// 车辆进入用 A 命令,格式如下:
// A,牌照,时间 例如: A,12345,13:52
// 离开用 D 命令, 例如: D,12345,14:53
// 查看当前停车场情况,用英文字母O命令 直接输入O即可,
// 结束,直接输入E
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)