- 相关推荐
停车场管理 c语言
/*----------------------------------------------------------------
// Copyright (C) 2014 沈阳工程学院信息安全工作室
// 版权所有。
//
// 文件名:模拟停车场问题.cpp
// 文件功能描述:模拟停车场问题
//
//
// 创建标识:20141214
//
// 修改标识:20141218
// 修改描述:完成编码
//----------------------------------------------------------------*/
//头文件
#include <iostream>
#include <http://www.ahsrst.cn
#include <string>
#include <http://www.ahsrst.cn
//常量定义
#define MAX_STOP 4 //定义停车场最大停车数
#define MAX_PLATE 10 //定义车牌号最大长度
#define TIME_COUNT "秒" //定义时间单位
#define TIME_MS_TO_CONUT 1000 //定义时间进制,意为由TIME_COUNT到毫秒的进制
#define UNIT_PRICE 10 //定义单位时间收费标准
using namespace std; //使用std命名空间
//数据结构定义
//定义存储汽车信息的结构体
typedef struct
{
char license_plate[MAX_PLATE]; //汽车牌照号码,定义为一个字符指针类型
char state; //汽车当前状态,字符p表示停放在停车位上,字符s表示停放在便道上,每辆车的初始状态用字符i来进行表示
int time; //汽车停入停车场时的时间,用来计时收费
}CAR;
//定义模拟停车场的栈结构
typedef struct
{
CAR STOP[MAX_STOP]; //汽车信息的存储空间
int top; //用来指示栈顶位置的静态指针
}SeqStack;
//定义模拟便道的队列结构
typedef struct node
{
CAR WAIT; //汽车信息的存储空间
struct node *next; //用来指示队列位置的动态指针
}QNode; //链队列节点的类型
//定义链队列的收尾指针
typedef struct
{
QNode *front,*rear;
}LQueue; //将头尾指针封装在一起的链队
//函数声明
int Empty_LQueue(LQueue *q); //判队空
int LeaveCheck(SeqStack parking , char *license_plate); //检查离开的车是否在停车场中
int QueueLength(LQueue *q); //判队长度
int Out_LQueue(LQueue *&sidewalk , char *license_plate); //出队操作
int StackEmpty(SeqStack parking); //判断栈是否为空
int StackFull(SeqStack parking); //判断栈是否为满
int StackPop(SeqStack &parking); //出栈操作
int StackTop(SeqStack parking , char *license_plate , int &time);//取栈顶元素
void Car_come(SeqStack &parking , LQueue *&sidewalk); //有车到来时的操作
void Car_leave(SeqStack &parking , LQueue *&sidewalk); //有车离开的操作
void Display(SeqStack parking); //显示停车场内的所有信息 调试时用
void InitStack(SeqStack &parking); //初始化栈
void InitList(LQueue *&sidewalk); //初始化队列
void In_LQueue(LQueue *&sidewalk , char *license_plate); //进队操作
void Input_Check(char *license_plate); ////检验输入的车牌是否合法
void StackPush(SeqStack &parking , char *license_plate , int stop_time);//进栈操作
void main()
{
//定义变量
SeqStack parking;
LQueue *sidewalk = NULL;
char *choice = new char;
int flag = 1; //定义一个变量 判断是否退出
//初始化一个为空的停车场
InitStack(parking);
//初始化一个为空的便道
InitList(sidewalk);
//运行界面及功能选择
while(flag)
{
cout<<"\n\t 停车场模拟管理系统 \n\n";
cout<<"\t|--------------------------------------------------|\n\n";
cout<<"\t|本程序为停车场的模拟管理系统,有车到来时请按C键。|\n\n";
cout<<"\t|然后根据屏幕提示进行相关操作,有车要走时请按l键。|\n\n";
cout<<"\t|然后根据屏幕提示进行相关操作,查看停车场请按D键。|\n\n";
cout<<"\t|然后根据屏幕提示进行相关操作,要退出系统请按Q键。|\n\n";
cout<<"\t|--------------------------------------------------|\n\n";
cout<<"请选择操作:";
gets(choice);
if(1 != strlen(choice))
{
cout<<"请正确输入选项!";
continue;
}
else
{
switch(*choice)
{
case 'c':
case 'C':
{
Car_come(parking,sidewalk);break;
}
case 'l':
case 'L':
{
Car_leave(parking,sidewalk);break;
}
case 'q':
case 'Q':
{
flag=0;break;
}
case 'd':
case 'D':
{
Display(parking);break;
}
default:
cout<<"选择不正确!请重新选择!\n";
}
}
}
}
//有车到来时的操作
void Car_come(SeqStack &parking , LQueue *&sidewalk)
{
//定义变量
char license_plate[MAX_PLATE];
cout<<"请输入车辆的车牌号码:";
Input_Check(license_plate);
//判断停车场是否已满,满则进入便道,不满进入停车场
if(StackFull(parking))
{
In_LQueue(sidewalk , license_plate); //进入便道
cout<<"停车场已满请在便道等候,您的位置为"<<QueueLength(sidewalk)
<<endl;
}
else
{
StackPush(parking , license_plate , GetTickCount()); //进入停车场
cout<<"请进入停车场中的"<<http://www.ahsrst.cn"号停车位\n";
}
// Display(parking);
}
//有车离开时的操作
void Car_leave(SeqStack &parking , LQueue *&sidewalk)
{
//定义变量
SeqStack tmpparking; //定义临时停车场
char leave_license_plate[MAX_PLATE]; //要离开的车牌号
char license_plate[MAX_PLATE]; //存放从停车场中读出来的车牌信息
int time;
InitStack(tmpparking); //初始化临时停车场
//判断停车场中是否有车
if(StackEmpty(parking))
{
cout<<"当前停车场中没有车\n";
return; //退出子函数
}
cout<<"请输入要离开的车牌照:";
Input_Check(leave_license_plate);
cout<<"当前停车场中有"<<http://www.ahsrst.cn"辆车\n";
if(LeaveCheck(parking , leave_license_plate)) //判断车是否在停车场中
{
//车在停车场中
cout<<"您的车在"<<LeaveCheck(parking , leave_license_plate)<<"号车位上\n";
while(StackTop(parking , license_plate , time)
&& (strcmp(http://www.ahsrst.cn[http://www.ahsrst.cn].license_plate , leave_license_plate) != 0))
{
strcpy(http://www.ahsrst.cn[http://www.ahsrst.cn].license_plate , license_plate);
cout<<"牌照为"<<license_plate<<"的车暂时退出停车场"<<http://www.ahsrst.cn"号位\n";
StackPush(tmpparking , license_plate , time); //停车场中的车暂时退出 进入临时停车场
StackPop(parking); //出栈
}
cout<<"牌照为"<<license_plate<<"的车离开停车场"<<http://www.ahsrst.cn"号位\n";
cout<<"您在停车场中停了"<<(GetTickCount()-time)/TIME_MS_TO_CONUT<<TIME_COUNT<<endl; //输出所停时间信息
cout<<"应缴费用为"<<(GetTickCount()-time)/TIME_MS_TO_CONUT*UNIT_PRICE<<"元\n";; //输出费用信息
StackPop(parking); //出栈
//将临时停车场中的车停回停车场
while(StackEmpty(tmpparking) != 1)
{
StackTop(tmpparking , license_plate , time);
StackPush(parking , license_plate , time);
cout<<"牌照为"<<license_plate<<"的车进入停车场"<<http://www.ahsrst.cn"号位\n";
license_plate[0] = '\0';
StackPop(tmpparking);
}
if(http://www.ahsrst.cn) //判断车离开前停车场是否停满
if(QueueLength(sidewalk)) //如果停满则判断便道上是否有车
{
//便道中有车 则从便道中停入停车场
Out_LQueue(sidewalk , license_plate); //出队
StackPush(parking , license_plate , GetTickCount()); //入栈
cout<<"在便道中牌照为"<<license_plate<<"的车进入停车场"<<http://www.ahsrst.cn"号位\n";
}
}
else
//车不在停车场中
cout<<"您的车不在停车场中!\n";
}
//初始化顺序栈
void InitStack(SeqStack &parking)
{
http://www.ahsrst.cn
}
//判栈空
int StackEmpty(SeqStack parking)
{
if(http://www.ahsrst.cn)
return 1;
else
return 0;
}
//判栈满
int StackFull(SeqStack parking)
{
if(http://www.ahsrst.cn)
return 1;
else
return 0;
}
//入栈
void StackPush(SeqStack &parking , char *license_plate , int stop_time)
{
http://www.ahsrst.cn
strcpy(http://www.ahsrst.cn[http://www.ahsrst.cn].license_plate , license_plate);
http://www.ahsrst.cn[http://www.ahsrst.cn].state = 'p';
http://www.ahsrst.cn[http://www.ahsrst.cn].time = stop_time;
}
//出栈 返回栈顶指针
int StackPop(SeqStack &parking)
{
if(StackEmpty(parking))
return 0;
else
return http://www.ahsrst.cn
}
//取栈顶元素
int StackTop(SeqStack parking , char *license_plate , int &time)
{
if(StackEmpty(parking))
return 0;
else
{
strcpy(license_plate , http://www.ahsrst.cn[http://www.ahsrst.cn].license_plate);
time = http://www.ahsrst.cn[http://www.ahsrst.cn].time;
return 1;
}
}
//显示所有
void Display(SeqStack parking)
{
if(http://www.ahsrst.cn)
printf("停车场为空\n");
else
{
while(http://www.ahsrst.cn!= -1)
{
cout<<"车牌号为:"<<http://www.ahsrst.cn[http://www.ahsrst.cn].license_plate;
cout<<",停在"<<http://www.ahsrst.cn"号车位上";
cout<<",已停"<<(GetTickCount()http://www.ahsrst.cn[http://www.ahsrst.cn].time)/TIME_MS_TO_CONUT<<TIME_COUNT<<endl;
http://www.ahsrst.cn
}
}
}
//初始化队列
void InitList(LQueue *&sidewalk)
{
sidewalk = (LQueue *)malloc(sizeof(LQueue));
sidewalk->front=sidewalk->rear = NULL;
}
//入队
void In_LQueue(LQueue *&sidewalk,char *license_plate)
{
QNode *car_on_sidewalk;
car_on_sidewalk = (QNode *)malloc(sizeof(QNode)); //为新节点开辟新空间
strcpy(car_on_sidewalk->http://www.ahsrst.cn); //将数据写入节点
car_on_sidewalk->http://www.ahsrst.cn's'; //写入停车信息
car_on_sidewalk->http://www.ahsrst.cn(); //写入停车时间
car_on_sidewalk->next = NULL;
if(Empty_LQueue(sidewalk)) //队空则创建第一个节点
sidewalk->front = sidewalk->rear = car_on_sidewalk;
else
{
//队非空插入队尾
sidewalk->rear->next = car_on_sidewalk;
sidewalk->rear = car_on_sidewalk;
}
}
//判队空
int Empty_LQueue(LQueue *q)
{
if(q->front == NULL)
return 1;
else
return 0;
}
//判队长度 返回队长
int QueueLength(LQueue *q)
{
QNode *p=q->front;
int i=0;
while(p != NULL)
{
i++;
p=p->next;
}
return i;
}
//出队 成功返回1 队空返回0
int Out_LQueue(LQueue *&sidewalk,char *license_plate)
{
QNode *car_on_sidewalk;
if(Empty_LQueue(sidewalk)) //如果队空返回0
return 0;
car_on_sidewalk = sidewalk->front;
strcpy(license_plate , car_on_sidewalk->http://www.ahsrst.cn);//取出队头元素
if(sidewalk->front == sidewalk->rear) //队中只有一个元素
sidewalk->front = sidewalk->rear=NULL; //删除元素
else
sidewalk->front = sidewalk->front->next; //队头指针后移
free(car_on_sidewalk); //释放指针
return 1;
}
//检查离开的车是否在停车场中 返回车在停车场中位置 不在则返回0
int LeaveCheck(SeqStack parking,char *license_plate)
{
int flag = http://www.ahsrst.cn定义变量记录当前车在停车场中位置
if(StackEmpty(parking))
return 0;
else
{
//查找离开车所在位置
while(http://www.ahsrst.cn!= -1 && strcmp(http://www.ahsrst.cn[http://www.ahsrst.cn].license_plate , license_plate) != 0)
{
flag--;
http://www.ahsrst.cn
}
return flag;
}
}
//检验输入的车牌是否合法
void Input_Check(char *license_plate)
{
int flag = 1;
int i;
string tmpstr;
while(flag)
{
cin>>tmpstr;
getchar();
if(http://www.ahsrst.cn()<MAX_PLATE)
{
for(i=0;i<10;i++)
license_plate[i] = http://www.ahsrst.cn()[i];
flag = 0;
}
else
cout<<"输入有误,请重新输入:";
}
}
【停车场管理 c语言】相关文章:
停车场管理程序c语言04-17
停车场管理c++04-17
停车场管理04-17
内部停车场管理04-17
济南停车场管理04-17
南京停车场 管理04-17
rfid停车场管理04-17
停车场管理情况04-17
停车场的管理思路04-17
停车场管理方案04-17