停车场

来源:互联网 发布:怎样学好数控车床编程 编辑:程序博客网 时间:2024/04/19 16:21

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <malloc.h>

#include "api.h"

#include<iostream>

#define max 10

using namespace std;

 

ParkingAutoInfo g_ParkingAutoInfo[max];//包含车辆信息的数组

ParkSysTime Time;//系统时间

UL parkingNumber=1;//空闲停车位的数量

 

//停车卡号的状态

typedef struct 

  int cardNumber;

  CardStatus state; 

}cardInfo;

 

//历史记录链表结构体

 typedef struct historyInfo

{

  ParkingAutoHistoryInfo *HistoryInfo;

  historyInfo *next;

}history,*List;

history *HisInfo;

 

cardInfo g_cardInfo[CARD_COUNT];//存储车位号码和状态的数组

 

void main(int argc, char* argv[])

{

    /* 

启动Socket服务侦听5555端口(sapi_server_start函数在lib库已实现)

    lib库已实现从Socket接收到字符串后的命令分发处理;

    */

//OpReset();//系统初始化

apiServerStart(argc,argv);

// 此处不会执行到,注意不要在此处添加代码

}

 

/*****************************************************************************

 函 数 名  : OpReset

 功能描述  需要考生实现的接口,系统初始化

 输入参数  

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpReset(void)

{

    

 /*存储历史记录信息的链表的初始化*/

     HisInfo=(List)malloc(sizeof(historyInfo));

     HisInfo->next=NULL;

 Time.hh=0;

 Time.mm=0;

 parkingNumber=5;

     //cout<<"parkingNumber:"<<parkingNumber<<endl;

 for(int i=0;i<=10;i++)

 {

 g_ParkingAutoInfo[i].AutoNo=0;//将每辆车的车牌号写入结构体数组

         g_ParkingAutoInfo[i].ParkingCount=0;//停车次数设为0

 }

 

 for(int j=0;j<5;j++)//初始化各个停车卡号所对应停车位为空闲状态

 {   

 g_cardInfo[j].cardNumber=j;

 g_cardInfo[j].state=CARD_IDLE;

 }

     api_parksys_ret(OP_RESET_SUCC);

}

 

/*****************************************************************************

 函 数 名  : OpGetin

 功能描述  需要考生实现的接口,车辆进入停车场

 输入参数  : UL AutoNo 车牌号

             ParkSysAutoType AutoType 车辆类型

             ParkSysTime InTime 车辆进入停车场时间

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpGetin(UL AutoNo, ParkSysAutoType AutoType, ParkSysTime InTime)

{

    //cout<<"parkingNumber:"<<parkingNumber<<endl;

//判断参数是否合法

   if(!(AutoNo<=20&&AutoNo>=10)

   ||(AutoType!=CAR&&AutoType!=BUS)

   ||!((InTime.hh<=23&&InTime.hh>=0)&&(InTime.mm<=59&&InTime.mm>=0)))

   {

     api_parksys_ret(OP_E_PARAMETER);

 return;

   }

  // cout<<"parkingNumber:"<<parkingNumber<<endl;

//如果停车位数量为0,提示车位已满

if(0==parkingNumber)

  {

  api_parksys_ret(OP_E_PARKING_FULL);

  return;

  }

 

 //如果该车已经在停车场进行提示

  //UL cardNumber=g_ParkingAutoInfo[AutoNo].CardNo;

  for(int i=0;i<10;i++)

  {

if(AutoNo==g_ParkingAutoInfo[i].AutoNo)

  {   

 // cout<<"g_cardInfo[cardNumber].state:"<<endl;

      cout<<"error"<<endl;

  api_parksys_ret(OP_E_EXISTED_ATUO);

  return;

  }

  }

  

  //判断是否进入时间逻辑错误

  int InTimeInfo=(int)InTime.hh*60+(int)InTime.mm;

  int AutoNoGetInTimeInfo=(int)g_ParkingAutoInfo[AutoNo].CurGetInTime.hh*60+(int)g_ParkingAutoInfo[AutoNo].CurGetInTime.mm;

  if(AutoNoGetInTimeInfo>InTimeInfo)

  {

    api_parksys_ret(OP_E_TIME_LOGIC);

return;

  }

 

  //判断是否为套牌车

 if(AutoType!=g_ParkingAutoInfo[AutoNo].AutoType)

 {

     api_parksys_ret(OP_E_MISMATCH_AUTO_TYPE);

     history *p,*q;

 p=HisInfo;

 q=HisInfo->next;

 while(q!=NULL)

 {

   if(AutoType==q->HistoryInfo->AutoNo)//找到车牌号相同的记录

   {   

   history *r;//用来标识所符合条件的结点

   r=q;

   q=q->next;

   p->next=p->next->next;

       free(r);//删除该车牌号所对应的结点

   }

    p=p->next;

    q=q->next;

 }

   return;

 }

 

  

 

  for(int j=0;j<5;j++)

  {

    if(CARD_IDLE==g_cardInfo[j].state)

  for(int k=0;k<=10;k++)

  {   

  if(AutoNo==g_ParkingAutoInfo[k].AutoNo)

  {

  g_ParkingAutoInfo[k].CardNo=j;

              g_ParkingAutoInfo[k].ParkingCount++;

  g_ParkingAutoInfo[k].AutoType=AutoType;

  g_ParkingAutoInfo[k].CurGetInTime=InTime;

              g_cardInfo[j].state=CARD_INUSE;

  }

  } 

 

}

  }

    parkingNumber--;

   api_parksys_ret(OP_PARKING_SUCC);//提示停车操作成功

   return;

 

}

 

/*****************************************************************************

 函 数 名  : OpGetout

 功能描述  需要考生实现的接口,车辆离开停车场

 输入参数  : UL AutoNo 车牌号

             UL CardNo 卡号

             ParkSysTime OutTime 车辆离开停车场时间

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpGetout(UL AutoNo, UL CardNo, ParkSysTime OutTime)

   //判断是否离开时间逻辑错误

  /*int OutTimeInfo=(int)InTime.hh*60+(int)InTime.mm;

  int AutoNoOutTimeInfo=(int)g_ParkingAutoInfo[AutoNo].CurGetInTime.hh*60+(int)g_ParkingAutoInfo[AutoNo].CurGetInTime.mm;

  if(OutTimeInfo<AutoNoOutTimeInfo)

  {

    api_parksys_ret(OP_E_TIME_LOGIC);

return;

  }*/

 

    //判断参数是否合法

   if(!(AutoNo<=20&&AutoNo>=10)

   ||(CardNo<=4&&CardNo>=0)

   ||!((OutTime.hh<=23&&OutTime.hh>=0)&&(OutTime.mm<=59&&OutTime.mm>=0)))

   {

     api_parksys_ret(OP_E_PARAMETER);

 return;

   }

for(int i=0;i<=10;i++)

{

//如果该车不在停车场内提示信息

UL cardNumber=g_ParkingAutoInfo[AutoNo].CardNo;

        if(CARD_IDLE==g_cardInfo[cardNumber].state)

{

api_parksys_ret(OP_E_NOT_IN_PARKING);

return;

}

 

if(AutoNo==g_ParkingAutoInfo[i].AutoNo)//如果卡号和车对应的车牌号不一致提示信息

{

  if(CardNo!=g_ParkingAutoInfo[i].CardNo)

          api_parksys_ret(OP_E_AUTONO_MISMATCH_CARDNO);

  return;

}

else

{

  UL parkingFee=0;

  UL parkingTime=OutTime.hh*60+OutTime.mm-g_ParkingAutoInfo[i].CurGetInTime.hh*60-g_ParkingAutoInfo[i].CurGetInTime.mm;

  if(parkingTime<=30)

            parkingFee=0;

  else 

  {

    if(CAR==g_ParkingAutoInfo[i].AutoType)//小轿车的收费情况

{

if(parkingTime/60<=2)

     parkingFee=10;

else

  int time=0;

  UL parkingTime1=0;

                  parkingTime1=ceil(parkingTime/60);

  time=(int)parkingTime1;

  if(time<=20)

                  parkingFee=10+(time-2)*1;

  else

                    parkingFee=20;

}

}

 

   if(BUS==g_ParkingAutoInfo[i].AutoType)//卡车的收费情况

{

if(parkingTime/60<=2)

     parkingFee=20;

else

  int time=0;

  UL parkingTime1=0;

                  parkingTime1=ceil(parkingTime/60);

  time=(int)parkingTime1;

  if(time<=20)

                  parkingFee=20+(time-2)*1;

  else

                    parkingFee=30;

}

 

}

  

   

   api_parksys_fee(parkingFee);//输出停车费用

               parkingNumber++;//空闲车位增加一个

               

 

  /*将车辆的所有停车信息写入历史查询记录结构体*/

             history *p,*q;

 p=HisInfo;

 q=(List)malloc(sizeof(historyInfo));

 q->HistoryInfo->AutoNo=AutoNo;

 q->HistoryInfo->CardNo=CardNo;

 q->HistoryInfo->AutoType=g_ParkingAutoInfo[i].AutoType;

 q->HistoryInfo->Fee=parkingFee;

 q->HistoryInfo->GetInTime=g_ParkingAutoInfo[i].CurGetInTime;

 q->HistoryInfo->GetOutTime=OutTime;

 q->next=NULL;

 p->next=q;

 p=q;

 

             g_cardInfo[CardNo].state=CARD_IDLE;//使该车所持卡号的状态设为空闲

 

              /*将车辆在数组的位置的所持卡号、进入时间再一次初始化*/

 

  }

 

}

}

 

 

}

 

/*****************************************************************************

 函 数 名  : OpListIdleParkingSpaceNumber

 功能描述  需要考生实现的接口,停车场空闲车位查询

 输入参数  

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpListIdleParkingSpaceNumber(void)

{

   api_parksys_idle_parkingspace_number(parkingNumber);

}

 

/*****************************************************************************

 函 数 名  : OpListCardStatus

 功能描述  需要考生实现的接口,指定卡状态查询

 输入参数  : UL CardNo 卡号

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpListCardStatus(UL CardNo)

{

     if(!(CardNo<=4&&CardNo>=0))

   {

     api_parksys_ret(OP_E_PARAMETER);

 return;

   }

api_parksys_card_status(g_cardInfo[CardNo].state);

}

 

/*****************************************************************************

 函 数 名  : OpListParkingAuto

 功能描述  需要考生实现的接口,停车场中指定车辆信息查询

 输入参数  : UL AutoNo 车牌号

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpListParkingAuto(UL AutoNo)

{

    UL cardNumber=g_ParkingAutoInfo[AutoNo].CardNo;

if(CARD_IDLE==g_cardInfo[cardNumber].state)

{

 api_parksys_ret(OP_E_NOT_IN_PARKING);

 return;

}

else

 api_parksys_parking_auto(&g_ParkingAutoInfo[AutoNo]);

}

 

/*****************************************************************************

 函 数 名  : OpListParkingAutoHistoryInfo

 功能描述  需要考生实现的接口,停车场中历史所有车辆停车信息查询

 输入参数  : UL AutoNo 车牌号

 输出参数  

 返 回 值  

 调用函数  

 被调函数  

 

 修改历史      :

  1.日    期   : 200999

    作    者   

    修改内容   新生成函数

 

*****************************************************************************/

void OpListParkingAutoHistoryInfo(UL AutoNo)

{

    //如果该车还没开出,先输出当前的状态

UL cardNumber=g_ParkingAutoInfo[AutoNo].CardNo;

if(CARD_INUSE==g_cardInfo[cardNumber].state)

{

     //申请历史记录空间

     history *p;

 p=(List)malloc(sizeof(historyInfo));

 p->next=NULL;

             //设定输出值

 p->HistoryInfo->AutoNo=AutoNo;

 p->HistoryInfo->AutoType=g_ParkingAutoInfo[AutoNo].AutoType;

 p->HistoryInfo->CardNo=g_ParkingAutoInfo[AutoNo].CardNo;

             p->HistoryInfo->Fee=0;

             p->HistoryInfo->GetInTime=g_ParkingAutoInfo[AutoNo].CurGetInTime;

             p->HistoryInfo->GetOutTime.hh=99;

 p->HistoryInfo->GetOutTime.mm=99;

             api_parksys_parking_auto_history_info(p->HistoryInfo);

             //释放空间

     free(p);

}

history *q;

    q=HisInfo->next;

while(q!=NULL)

{

 api_parksys_parking_auto_history_info(q->HistoryInfo);

 q=q->next;

}

 

}

 

0 0
原创粉丝点击