数据结构停车场管理系统 5
推荐于2019-04-01
展开全部
#include<iostream>
#include<string>
using namespace std;
#define MAX 2 //停车场车位数
#define price 0.05 //每分钟收取的费用
typedef struct time
{
int hour;
int min;
}Time;//定义时间结点
typedef struct node
{
string num;
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); /*查看停车场内车辆情况及
便道上车辆情况的函数声明*/
void main( )
{
SeqStackCar Enter,Temp;//初始栈,中转栈
LinkQueueCar Wait;//便道队列
int a;
InitStack(&Enter);
InitStack(&Temp);
InitQueue(&Wait);//初始化
while(1)
{
cout<<"********************************************************************"<<endl;
cout<<" 欢迎光临停车场!";
cout<<"(*^__^*) !";
cout<<endl<<"车辆到达登记->请按 1";
cout<<" 车辆离开登记->请按 2"<<endl;
cout<<"车辆停靠查询->请按 3";
cout<<" 退出系统->请按 4"<<endl;//系统选项设置
while(1)
{
cin>>a;
if(a>=1&&a<=4) break;
else cout<<endl<<"请选择: 1~4.";
}
switch(a)
{
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) //堆栈初始化
{
s->top=0;
s->stack[s->top]=NULL;
}
int InitQueue(LinkQueueCar *Q)//队列初始化
{
Q->head=new 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 A,B,C,D;
cout<<"\n车辆离开的时间:";
cin>>p->leave.hour>>p->leave.min;
cout<<"离开车辆的车牌号为:";
cout<<p->num;
cout<<endl<<"其到达时间为: "<<p->reach.hour<<":"<<p->reach.min;
cout<<"离开时间为: "<<p->leave.hour<<":"<<p->leave.min;
A=p->reach.hour;
B=p->reach.min;
C=p->leave.hour;
D=p->leave.min;
cout<<endl<<"应交费用为: "<<((C-A)*60+(D-B))*price<<"元"<<endl;
cout<<"车辆离开登记完毕!"<<endl;
cout<<"*********************************************************************"<<endl;
delete p;
}
int Arrival(SeqStackCar *Enter,LinkQueueCar *W)
{
CarNode *p;
QueueNode *t;
p=new CarNode;
cout<<"********************************************************************"<<endl;
cout<<"车辆到达登记开始:"<<endl;
cout<<endl<<"请输入车牌号:";
cin>>p->num;
if(Enter->top<MAX)//如果车位未满则进停车场内
{
Enter->top++;
cout<<endl<<"车辆在车场第"<<Enter->top<<"位置.";
cout<<endl<<"车辆到达时间:";
cin>>p->reach.hour>>p->reach.min;
cout<<endl<<"车辆到达登记完毕!"<<endl;
cout<<"*********************************************************************"<<endl;
Enter->stack[Enter->top]=p;
return 1;
}
else //如果车位已满,则停靠在便道上
{
cout<<"*********************************************************************"<<endl;
cout<<endl<<"该车须在便道等待!有车位时进入车场"<<endl;
t=new 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 room;
CarNode *p,*t;
QueueNode *q;
if(Enter->top>0)
{
while(1)
{
cout<<"*********************************************************************"<<endl;
cout<<"车辆离开登记开始:"<<endl;
cout<<endl<<"请输入车在车场的位置/1--"<<Enter->top<<"/:";
cin>>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--;
}
cout<<"*********************************************************************"<<endl;
cout<<"车辆离开登记结算:"<<endl;
PRINT(p,room);
if((W->head!=W->rear)&&Enter->top<MAX)
{
q=W->head->next;
t=q->data;
Enter->top++;
cout<<endl<<"便道的"<<t->num<<"号车进入车场第";
cout<<Enter->top<<"位置."<<endl;
cout<<"请输入"<<t->num<<"号车进入车场的时间:";
cin>>t->reach.hour>>t->reach.min;
W->head->next=q->next;
if(q==W->rear) W->rear=W->head;
Enter->stack[Enter->top]=t;
delete q;
}
else cout<<endl<<"便道里没有车"<<endl;
}
else cout<<endl<<"车场里没有车."<<endl;
}
void List1(SeqStackCar *S) //查看停车场内车辆情况的函数定义
{
int i;
if(S->top>0)
{
cout<<"********************************************************************"<<endl;
cout<<endl<<"车场内部车辆停靠情况:"<<endl;
cout<<endl<<"位置 到达时间 车牌号"<<endl;
for(i=1;i<=S->top;i++)
{
cout<<i;
cout<<" "<<S->stack[i]->reach.hour<<":"<<S->stack[i]->reach.min;
cout<<" "<<S->stack[i]->num<<endl;
}
}
else cout<<endl<<"车场里没有车"<<endl;
cout<<"********************************************************************"<<endl;
}
void List2(LinkQueueCar *W) //查看便道上停靠车辆情况的函数定义
{
QueueNode *p;
p=W->head->next;
if(W->head!=W->rear)
{
cout<<"********************************************************************"<<endl;
cout<<endl<<"便道停靠车辆情况:"<<endl;
while(p!=NULL)
{
cout<<endl<<"车辆牌号:";
cout<<p->data->num<<endl;
p=p->next;
}
}
else cout<<endl<<"便道里没有车."<<endl;
cout<<"********************************************************************"<<endl;
}
void List(SeqStackCar S,LinkQueueCar W) //车辆列表显示函数
{
int flag,tag;
flag=1;
while(flag)
{
cout<<"********************************************************************"<<endl;
cout<<"车辆停靠查询开始:"<<endl;
cout<<endl<<"请选择 1|2|3:"<<endl;
cout<<"1.车场列表"<<endl<<"2.便道列表"<<endl<<"3.返回主菜单"<<endl;
while(1)
{
cin>>tag;
if(tag>=1||tag<=3) break;
else cout<<endl;
cout<<"请选择 1~3:";
}
switch(tag)
{
case 1:List1(&S);
cout<<"车辆停靠查询结束!"<<endl;break;
case 2:List2(&W);
cout<<"车辆停靠查询结束!"<<endl;break;
case 3:flag=0;break;
default: break;
}
}
cout<<"********************************************************************"<<endl;
}
#include<string>
using namespace std;
#define MAX 2 //停车场车位数
#define price 0.05 //每分钟收取的费用
typedef struct time
{
int hour;
int min;
}Time;//定义时间结点
typedef struct node
{
string num;
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); /*查看停车场内车辆情况及
便道上车辆情况的函数声明*/
void main( )
{
SeqStackCar Enter,Temp;//初始栈,中转栈
LinkQueueCar Wait;//便道队列
int a;
InitStack(&Enter);
InitStack(&Temp);
InitQueue(&Wait);//初始化
while(1)
{
cout<<"********************************************************************"<<endl;
cout<<" 欢迎光临停车场!";
cout<<"(*^__^*) !";
cout<<endl<<"车辆到达登记->请按 1";
cout<<" 车辆离开登记->请按 2"<<endl;
cout<<"车辆停靠查询->请按 3";
cout<<" 退出系统->请按 4"<<endl;//系统选项设置
while(1)
{
cin>>a;
if(a>=1&&a<=4) break;
else cout<<endl<<"请选择: 1~4.";
}
switch(a)
{
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) //堆栈初始化
{
s->top=0;
s->stack[s->top]=NULL;
}
int InitQueue(LinkQueueCar *Q)//队列初始化
{
Q->head=new 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 A,B,C,D;
cout<<"\n车辆离开的时间:";
cin>>p->leave.hour>>p->leave.min;
cout<<"离开车辆的车牌号为:";
cout<<p->num;
cout<<endl<<"其到达时间为: "<<p->reach.hour<<":"<<p->reach.min;
cout<<"离开时间为: "<<p->leave.hour<<":"<<p->leave.min;
A=p->reach.hour;
B=p->reach.min;
C=p->leave.hour;
D=p->leave.min;
cout<<endl<<"应交费用为: "<<((C-A)*60+(D-B))*price<<"元"<<endl;
cout<<"车辆离开登记完毕!"<<endl;
cout<<"*********************************************************************"<<endl;
delete p;
}
int Arrival(SeqStackCar *Enter,LinkQueueCar *W)
{
CarNode *p;
QueueNode *t;
p=new CarNode;
cout<<"********************************************************************"<<endl;
cout<<"车辆到达登记开始:"<<endl;
cout<<endl<<"请输入车牌号:";
cin>>p->num;
if(Enter->top<MAX)//如果车位未满则进停车场内
{
Enter->top++;
cout<<endl<<"车辆在车场第"<<Enter->top<<"位置.";
cout<<endl<<"车辆到达时间:";
cin>>p->reach.hour>>p->reach.min;
cout<<endl<<"车辆到达登记完毕!"<<endl;
cout<<"*********************************************************************"<<endl;
Enter->stack[Enter->top]=p;
return 1;
}
else //如果车位已满,则停靠在便道上
{
cout<<"*********************************************************************"<<endl;
cout<<endl<<"该车须在便道等待!有车位时进入车场"<<endl;
t=new 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 room;
CarNode *p,*t;
QueueNode *q;
if(Enter->top>0)
{
while(1)
{
cout<<"*********************************************************************"<<endl;
cout<<"车辆离开登记开始:"<<endl;
cout<<endl<<"请输入车在车场的位置/1--"<<Enter->top<<"/:";
cin>>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--;
}
cout<<"*********************************************************************"<<endl;
cout<<"车辆离开登记结算:"<<endl;
PRINT(p,room);
if((W->head!=W->rear)&&Enter->top<MAX)
{
q=W->head->next;
t=q->data;
Enter->top++;
cout<<endl<<"便道的"<<t->num<<"号车进入车场第";
cout<<Enter->top<<"位置."<<endl;
cout<<"请输入"<<t->num<<"号车进入车场的时间:";
cin>>t->reach.hour>>t->reach.min;
W->head->next=q->next;
if(q==W->rear) W->rear=W->head;
Enter->stack[Enter->top]=t;
delete q;
}
else cout<<endl<<"便道里没有车"<<endl;
}
else cout<<endl<<"车场里没有车."<<endl;
}
void List1(SeqStackCar *S) //查看停车场内车辆情况的函数定义
{
int i;
if(S->top>0)
{
cout<<"********************************************************************"<<endl;
cout<<endl<<"车场内部车辆停靠情况:"<<endl;
cout<<endl<<"位置 到达时间 车牌号"<<endl;
for(i=1;i<=S->top;i++)
{
cout<<i;
cout<<" "<<S->stack[i]->reach.hour<<":"<<S->stack[i]->reach.min;
cout<<" "<<S->stack[i]->num<<endl;
}
}
else cout<<endl<<"车场里没有车"<<endl;
cout<<"********************************************************************"<<endl;
}
void List2(LinkQueueCar *W) //查看便道上停靠车辆情况的函数定义
{
QueueNode *p;
p=W->head->next;
if(W->head!=W->rear)
{
cout<<"********************************************************************"<<endl;
cout<<endl<<"便道停靠车辆情况:"<<endl;
while(p!=NULL)
{
cout<<endl<<"车辆牌号:";
cout<<p->data->num<<endl;
p=p->next;
}
}
else cout<<endl<<"便道里没有车."<<endl;
cout<<"********************************************************************"<<endl;
}
void List(SeqStackCar S,LinkQueueCar W) //车辆列表显示函数
{
int flag,tag;
flag=1;
while(flag)
{
cout<<"********************************************************************"<<endl;
cout<<"车辆停靠查询开始:"<<endl;
cout<<endl<<"请选择 1|2|3:"<<endl;
cout<<"1.车场列表"<<endl<<"2.便道列表"<<endl<<"3.返回主菜单"<<endl;
while(1)
{
cin>>tag;
if(tag>=1||tag<=3) break;
else cout<<endl;
cout<<"请选择 1~3:";
}
switch(tag)
{
case 1:List1(&S);
cout<<"车辆停靠查询结束!"<<endl;break;
case 2:List2(&W);
cout<<"车辆停靠查询结束!"<<endl;break;
case 3:flag=0;break;
default: break;
}
}
cout<<"********************************************************************"<<endl;
}
展开全部
把扩展名改成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 中弹出并存入 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->lpHead; lpCarNode != NULL; lpCarNode = 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] = ; // 用户输入
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] = ;
sscanf( szUserInput, // 将命令类型与车辆信息分开存放
"%c,%s",
&cCommandType, // 用户输入的前半部分,即命令类型
szCarInformation // 用户输入的后半部分,即车辆信息
);
char *lpCommaLocation = NULL; // 车辆信息字符串中的逗号位置
for ( lpCommaLocation = szCarInformation; *lpCommaLocation != '\0'; lpCommaLocation++ )
{
if ( *lpCommaLocation == ',' )
{
break;
}
}
*lpCommaLocation = '\0';
TCARINFORMATION carinfo = ; // 存储本次用户输入的车辆信息
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 = ;
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 = ;
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 = ;
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 = 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");
int nNum = 0;
for ( LPTCARNODE lpCarNode = 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" );
return 0;
}
///////////////////////////////
下面这个程序是我大二上数据结构课时交的作业,现在正好电脑里面放着,所以就给你,应该满足你老师的要求了.编译环境是 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 中弹出并存入 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->lpHead; lpCarNode != NULL; lpCarNode = 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] = ; // 用户输入
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] = ;
sscanf( szUserInput, // 将命令类型与车辆信息分开存放
"%c,%s",
&cCommandType, // 用户输入的前半部分,即命令类型
szCarInformation // 用户输入的后半部分,即车辆信息
);
char *lpCommaLocation = NULL; // 车辆信息字符串中的逗号位置
for ( lpCommaLocation = szCarInformation; *lpCommaLocation != '\0'; lpCommaLocation++ )
{
if ( *lpCommaLocation == ',' )
{
break;
}
}
*lpCommaLocation = '\0';
TCARINFORMATION carinfo = ; // 存储本次用户输入的车辆信息
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 = ;
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 = ;
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 = ;
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 = 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");
int nNum = 0;
for ( LPTCARNODE lpCarNode = 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" );
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询