歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++實現停車場管理系統

C++實現停車場管理系統

日期:2017/3/1 9:07:46   编辑:Linux編程

有一個可以停放n 輛汽車的狹長停車場,它只有一個大門可以供車輛進出。車輛按到達停車場時間的早晚依次從停車場最裡面向大門口處停放(最先到達的第一輛車放在停車場的最裡面)。如果停車場已放滿n 輛車,則後來的車輛只能在停車場大門外的便道上等待,一旦停車場內有車開走,則排在便道上的第一輛車就進入停車場。停車場內如有某輛車要開走,在他之後進入停車場的車都必須先退出停車場為它讓路,待其開出停車場後,這些車輛在依原來的次序進場。每輛車在離開停車場時,都應依據它在停車場內停留的時間長短交費。如果停留在便道上的車未進停車場就要離去,允許其離去,不收停車費,並且仍然保持在便道上等待的車輛的次序。

1. 以棧模擬停車場,以隊列模擬車場外的便道,按照從終端讀入的輸入數據序列進行模擬管理。

2. 每一組輸入數據包括三個數據項:汽車“到達”或“離去”信息、汽車牌照號碼以及到達或離去的時刻。

3. 對每一組輸入數據進行操作後的輸出信息為:若是車輛到達,則輸出汽車在停車場或便道上的停車位置;若是車輛離去,則輸出汽車在停車場內停留的時間和應交納的費用(在便道上停留的時間不收費,功能可自己添加)。

1.頭文件

#define _AFXDLL
#include <afx.h>
#include<iostream>
#include<string>
#define MAX 3
using namespace std;

struct Time //時間結構體,用於車輛進場,出場時間記錄
{
int year;
int month;
int day;
int hour;
int min;
};

struct CarNode //車輛結構體
{
char num[10]; //車牌號
Time reach; //到達時間
Time leave; //離開時間
};
struct StackCar //停車場棧
{
int top;
CarNode *CarStack[MAX + 1];
};
struct QCarNode //鏈隊中的汽車結點結構體
{
CarNode *data; //汽車信息
struct QCarNode *next;
};
struct LinkQueueCar //便道
{
QCarNode *head; //對頭指針
QCarNode *rear; //隊尾指針
};
class CarSystem{
StackCar *CarBase, *QuitTemp; //停車場棧,車輛出停車場時的臨時棧
LinkQueueCar *WaitQueue; //便道
public:
CarSystem(); //構造函數
~CarSystem(); //析構函數
int Arrival(); //車輛進站
void Leave(); //車輛出站
void ShowLeaveInfo(CarNode *p, int item); //離開車輛繳費相關信息
void ShowInfo(); //顯示車位情況
void Carstack(); //停車場中車位狀況
void Carqueue(); //便道中車位狀況
void QueueCarLeave(char a[]); //便道中車輛離開
};
void ShowMenu()
{
cout << "********************************************" << endl;
cout << "****** 停 車 場 管 理 系 統 ******" << endl;
cout << "****** 0.安全退出系統 ******" << endl;
cout << "****** 1.汽車停車登記 ******" << endl;
cout << "****** 2.汽車離開登記 ******" << endl;
cout << "****** 3.便道汽車離開 ******" << endl;
cout << "****** 4.車位信息查看 ******" << endl;
cout << "\n\t\n\t\t請選擇:";
}

CarSystem::CarSystem() //構造函數
{
CarBase = new StackCar; //停車場棧
CarBase->top = 0;
CarBase->CarStack[CarBase->top] = NULL;

QuitTemp = new StackCar; //車輛臨時棧
QuitTemp->top = 0;
QuitTemp->CarStack[QuitTemp->top] = NULL;

WaitQueue = new LinkQueueCar; //便道
WaitQueue->head = new QCarNode;
if (WaitQueue->head != NULL)
{
WaitQueue->head->next = NULL;
WaitQueue->rear = WaitQueue->head;
}
}

CarSystem::~CarSystem()
{
QCarNode *ptemp;
while (CarBase->top != 0)
CarBase->CarStack[--CarBase->top] = NULL;
while (WaitQueue->head != WaitQueue->rear)
{
ptemp = WaitQueue->head;
WaitQueue->head = WaitQueue->head->next;
delete ptemp;
}
}

int CarSystem::Arrival() //車輛進站登記
{
CarNode *p; //汽車臨時指針
QCarNode *t; //隊列中汽車臨時指針
CTime start_time = CTime::GetCurrentTime(); //獲取系統當前時間作為車輛進站時間
p = new CarNode;
cout << "登記車牌號:";
cin >> p->num;
if (CarBase->top < MAX) //有空車位
{
CarBase->top++;
cout << "車輛在停車場第 " << CarBase->top << " 號車位" ;
p->reach.year = start_time.GetYear();
p->reach.month = start_time.GetMonth();
p->reach.day = start_time.GetDay();
p->reach.hour = start_time.GetHour();
p->reach.min = start_time.GetMinute();
CarBase->CarStack[CarBase->top] = p;
return 1;
}
else //沒有空車位
{
cout << "停車場已滿,請在便道等待...";
t = new QCarNode;
t->data = p;
t->next = NULL;
WaitQueue->rear->next = t;
WaitQueue->rear = t;
return 1;
}
}

void CarSystem::Leave() //車輛出站登記
{
int item;
CarNode *p, *t;
QCarNode *q;
if (CarBase->top > 0) //車站有車時
{
while (1)
{
cout << "請輸入車在車場的位置:";
cin >> item;
if (item >= 1 && item <= CarBase->top) break; //判斷輸入位置
}
while (CarBase->top > item) //位置不在棧頂的汽車出站
{
QuitTemp->top++;
QuitTemp->CarStack[QuitTemp->top] = CarBase->CarStack[CarBase->top];
CarBase->CarStack[CarBase->top] = NULL;
CarBase->top--;
}

p = CarBase->CarStack[CarBase->top];
CarBase->CarStack[CarBase->top] = NULL;
CarBase->top--;

while (QuitTemp->top >= 1) //當暫時存儲汽車的棧結構中有汽車時汽車重新進站
{
CarBase->top++;
CarBase->CarStack[CarBase->top] = QuitTemp->CarStack[QuitTemp->top];
QuitTemp->CarStack[QuitTemp->top] = NULL;
QuitTemp->top--;
}

ShowLeaveInfo(p, item);

if ((WaitQueue->head != WaitQueue->rear) && CarBase->top<MAX) //停車場有車位且便道有車時,便道中車輛進入停車場
{
CTime start_time = CTime::GetCurrentTime(); //獲取系統當前時間作為車輛進站時間
q = WaitQueue->head->next;
t = q->data;
CarBase->top++;
cout << "\n便道的" << t->num << "號車進入車場第" << CarBase->top << "號車位.";
t->reach.year = start_time.GetYear();
t->reach.month = start_time.GetMonth();
t->reach.day = start_time.GetDay();
t->reach.hour = start_time.GetHour();
t->reach.min = start_time.GetMinute();

WaitQueue->head->next = q->next;
if (q == WaitQueue->rear)
WaitQueue->rear = WaitQueue->head;
CarBase->CarStack[CarBase->top] = t;
free(q);
}
else cout << "便道裡沒有車..." << endl;
}
else cout << "挺車場裡沒有車..." << endl;
}

void CarSystem::ShowLeaveInfo(CarNode *p, int room) //汽車離站時繳費顯示
{
CTime end_time = CTime::GetCurrentTime();
p->leave.year = end_time.GetYear();
p->leave.month = end_time.GetMonth();
p->leave.day = end_time.GetDay();
p->leave.hour = end_time.GetHour();
p->leave.min = end_time.GetMinute();
printf("\n離開車輛的車牌號為:");
cout << p->num;
cout << "\n到達時間為: " << p->reach.year << " 年 " << p->reach.month << " 月 " << p->reach.day << " 日 " << p->reach.hour << " 時 " << p->reach.min << " 分";
cout << "\n離開時間為: " << p->leave.year << " 年 " << p->leave.month << " 月 " << p->leave.day << " 日 " << p->leave.hour << " 時 " << p->leave.min << " 分";
cout << "\n應交費用為: " << ((p->leave.hour - p->reach.hour) * 60 + (p->leave.min - p->reach.min)) * 100 << " 元";
free(p);
}

void CarSystem::QueueCarLeave(char a[]) //便道中的車直接離開
{
QCarNode *p,*q;
p = WaitQueue->head->next;
q = WaitQueue->head;
if (WaitQueue->head != WaitQueue->rear)
{
while (strcmp(p->data->num, a) && p != NULL)
{
q = p;
p = p->next;
}
q->next = p->next;
free(p);
}
else
{
cout << "便道中無車!" << endl;
}
}

void CarSystem::ShowInfo() //查詢車位狀態
{
int choice = 0;
cout << "請輸入查看列表:" << endl;
cout << "--- 1.停車場車位狀況 ---" << endl;
cout << "--- 2.便道停車位狀況 ---" << endl;
cout << "--- 3.返回主菜單 ---" << endl;
while (choice!=3)
{
while (1)
{
cin >> choice;
if (choice >= 1 || choice <= 3) break;
else cout << "請檢查輸入!" << endl;
}
switch (choice)
{
case 1:
Carstack();
break;
case 2:
Carqueue();
break;
case 3:
return;
break;
default:
break;
}
}
}

void CarSystem::Carstack() //車場車位顯示
{
int i;
if (CarBase->top>0)
{
cout << "停車場車位狀態:";
cout << "\n車位號\t" << " 到達時間 \t" << "車牌號\n";
for (i = 1; i <= CarBase->top; i++)
{
cout << " " << i << "\t ";
cout << CarBase->CarStack[i]->reach.year << "年" << CarBase->CarStack[i]->reach.month << "月" << CarBase->CarStack[i]->reach.day
<< "日" << CarBase->CarStack[i]->reach.hour << "時" << CarBase->CarStack[i]->reach.min << "分";
cout << "\t " << CarBase->CarStack[i]->num << endl;
}
}
else cout << "\n車場裡沒有車";
}

void CarSystem::Carqueue() //便道車位顯示
{
QCarNode *p;
p = WaitQueue->head->next;
cout << "便道車位狀況:" << endl;
if (WaitQueue->head != WaitQueue->rear)
{
cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
while (p != NULL)
{
cout << p->data->num << "\t";
p = p->next;
}
cout << endl;
cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
}
else
{
cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
cout << "便道中無車!" << endl;
cout << "-->-->-->-->-->-->-->-->-->-->-->-->-->-->" << endl;
}
}

2.源文件

#include "CarSystem.h"
#include<iostream>

using namespace std;
int main()
{
CarSystem CAR; //停車場類
int x = 100;
char a[10];
while (x != 0)
{
system("pause");
system("cls"); //清屏
ShowMenu();
cin >> x;
switch (x)
{
case 0:
exit(0);
break;
case 1:
CAR.Arrival();
cout << endl << "-----------------------------------------" << endl;
cout << "停車場更新..." << endl;
CAR.Carstack();
cout << "-----------------------------------------" << endl;
break;
case 2:

CAR.Leave();
cout << endl << "-----------------------------------------" << endl;
cout << "停車場更新..." << endl;
CAR.Carstack();
cout << "-----------------------------------------" << endl;
break;
case 3:

cout << "請輸入要離開的車牌號:" ;
cin >> a;
CAR.QueueCarLeave(a);
cout << "便道更新..." << endl;
CAR.Carqueue();
break;
case 4:
CAR.ShowInfo();
break;
}
}
system("pause");
return 0;
}

Copyright © Linux教程網 All Rights Reserved