C++實(shí)現(xiàn)停車場管理系統(tǒng)的示例代碼
一、案例需求描述
停車場管理系統(tǒng)就是模擬停車場進(jìn)行車輛管理的系統(tǒng),該系統(tǒng)分為汽車信息模塊,用戶使用模塊和管理員用戶模塊,各模塊功能如下所示:
1.1、汽車信息模塊
添加汽車信息:添加汽車屬性。
刪除汽車信息:輸入停車場中存在的車牌號,刪除汽車信息。
查找汽車信息:輸入停車場存在的車牌號,顯示汽車詳細(xì)信息。
修改汽車信息:輸入停車場內(nèi)存在的車牌號,修改汽車屬性信息。
停車時長統(tǒng)計(jì):顯示汽車在停車場中停留的時間和車輛總數(shù)。
停車場信息顯示:顯示停車場所有汽車的屬性信息。
汽車信息保存:將汽車信息保存到本地文件中。
1.2、普通用戶模塊
可以查詢、顯示所有汽車信息及停車費(fèi)信息,另外還包含停車時長統(tǒng)計(jì)與退出普通用戶登錄功能。由于是多次操作,因此需要有循環(huán)判斷功能,這種情況多使用while嵌套switch case語句實(shí)現(xiàn)。
1.3、管理員用戶模塊
此模塊具有普通用戶模塊的所有功能,此外還應(yīng)有增、刪、改的功能。
二、案例分析
通過案例描述我們得到了非常清晰的模塊信息,因此在設(shè)計(jì)類時應(yīng)該包含普通用戶類、管理員用戶類、汽車信息類。
大致思路:
在汽車信息類中實(shí)現(xiàn)基本功能并封裝起來,以便后續(xù)調(diào)用。
在普通用戶類中定義菜單功能,通過鍵入不同數(shù)字實(shí)現(xiàn)不同功能。
在管理員用戶類中定義菜單功能,通過鍵入不同數(shù)字實(shí)現(xiàn)不同功能。
三、案例代碼實(shí)現(xiàn)
這里我采用分文件編寫的方式,建立user.h、admin.h、car.h及對應(yīng)的三個.cpp文件和main.cpp文件,在main里面循環(huán)調(diào)用user和admin的方法就能實(shí)現(xiàn)停車場管理系統(tǒng)。
3.1、汽車信息類及方法實(shí)現(xiàn)
car.h
#pragma once // 防止頭文件被重復(fù)調(diào)用
#include<string>
#include<ctime>
using namespace std;
class Car {
private:
string carNum; // 汽車編號
string carType; // 汽車型號
string color; // 汽車顏色
time_t inTime; // 汽車停車時間點(diǎn)
public:
void addCar(); // 下面四行對應(yīng)增刪改查
void delCar();
void modCar();
void findCar();
void timeAmount(); // 計(jì)算停車時長并統(tǒng)計(jì)汽車總數(shù)
void showInfor(); // 顯示車輛信息(讀文件)
void saveInfor(); // 保存車輛信息(寫文件)
};
car.cpp
#include"car.h"
#include<fstream> // 讀寫操作
#include<iostream>
#include<iomanip> // IO流控制頭文件,類似C里的格式化輸出
using namespace std;
void Car::addCar() {
time_t _time; // 定義時間變量,秒數(shù),調(diào)用time()獲取
while (1) {
AA: cout << "請輸入車牌號:";
cin >> carNum;
// 判斷文件中是否已存在相同車牌號
ifstream ifs("carData.txt", ios::in); // 讀文件
if (ifs) {
char buf[1024];
string strs[20];
int index = 0; // 標(biāo)識數(shù)組索引
while (!ifs.eof()) { // 讀取文件直到末尾
ifs.getline(buf, 100); // 每次讀取一行數(shù)據(jù),放入buf數(shù)組 注:第二個參數(shù)為字符數(shù),緩沖區(qū)盡量大,否則循環(huán)會異常結(jié)束
strs[index++] = buf[0]; // buf[0]為車牌號,存入strs數(shù)組,索引自增
}
// 遍歷strs數(shù)組,auto 自動推導(dǎo)數(shù)據(jù)類型,這里等價于 string
for (auto& num : strs) {
// 判斷輸入車牌號是否與文件里重復(fù)
if (num == carNum) {
cout << "車牌號重復(fù)!" << endl;
goto AA; // 重復(fù)后重新輸入車牌號
}
}
}
// 車牌號不重復(fù)繼續(xù)加下來的輸入
cout << "請輸入車的種類:";
cin >> carType;
cout << "請出入車的顏色:";
cin >> color;
inTime = time(&_time); // 記錄停車時間
// 保存新增車輛信息
saveInfor();
char ch;
cout << "\t是否繼續(xù)?(y/n)"; // 判斷是否繼續(xù)輸入,\t 制表符,通常八個空格
cin >> ch;
if (ch == 'n' || ch == 'N') {
break;
}
}
}
void Car::delCar() {
// 讀文件
ifstream carData("carData.txt", ios::in);
// 創(chuàng)建文件寫入流,緩沖文件
ofstream outData("tempCarData.txt", ios::out);
if (!outData || !carData) {
cout << "文件打開失??!" << endl;
return;
}
string carId, name, str;
bool flag = true;
cout << "請輸入要刪除的車牌號:";
cin >> carId;
// 讀取文件第一個字段(車牌號) >> 遇空格結(jié)束讀取
while (carData >> name) {
getline(carData,str); // 將該行數(shù)據(jù)讀取到 str
// 如果相同,輸出要刪除的車輛信息:顏色,型號,停車時間
if (name == carId) {
cout << "要刪除的車輛信息:" << endl << str << endl;
flag = false;
break;
}
// 如果不相同,將車輛信息寫入到temp,否則舍棄該行
outData << name << " " << str << endl;
}
if (flag) cout << "該車牌號不存在" << endl;
else {
while (getline(carData, str)) { // 繼續(xù)按行讀取,此時第一行
outData << str << endl; // 寫入到temp
}
carData.close();
outData.close();
// 讀取 temp,寫入 carData
ifstream in("tempCarData.txt", ios::in);
ofstream out("carData.txt", ios::out);
if (!in || !out) {
cout << "文件讀取失敗!" << endl;
return;
}
else {
while (getline(in, str)) { // 按行讀取,寫入
out << str << endl;
}
}
in.close();
out.close();
}
}
void Car::modCar() {
string chepai1, chepai2, str;
time_t time1;
int i = 1;
cout << "請輸入你要修改的車輛的車牌號" << endl;
cin >> chepai1;
ifstream indata("carData.txt", ios::in);
ofstream outdata("tempCarData.txt", ios::out);
if (!indata || !outdata)
{
cerr << "文件打開錯誤" << endl;
exit(1);
}
while (indata >> chepai2)
{
indata >> carType >> color >> inTime; // 讀取該行剩余元素
if (chepai1 == chepai2)
{
i = 0;
cout << "已找到所要修改的車輛" << endl;
cout << "修改后的車牌號" << endl;
cin >> carNum;
cout << "修改后的車輛型號" << endl;
cin >> carType;
cout << "修改后的車輛顏色" << endl;
cin >> color;
inTime = time(&time1);
// 寫入carData.txt
outdata << carNum << " " << carType << " " << color << " " << inTime << endl;
break;
}
// 車牌號不同,將車輛信息存到temp
outdata << chepai2 << " " << carType << " " << color << " " << inTime << endl;
}
if (i) {
cout << "停車場中沒有找到要修改的車輛" << endl;
}
outdata.close();
indata.close();
ifstream in("tempCarData.txt", ios::in);
ofstream out("carData.txt", ios::out);
if (!in || !out)
{
cout << "文件打開錯誤" << endl;
exit(1);
}
while (getline(in, str))
{
out << str << endl;
}
in.close();
out.close();
}
void Car::findCar() {
ifstream carData("carData.txt", ios::in);
if (!carData)
{
cout << "文件打開失敗" << endl;
return;
}
else {
string carId;
time_t _time, t1;
bool flag = true;
cout << "請輸入要查找的車牌號" << endl;
cin >> carId;
while (carData >> carNum) // 讀取車牌號
{
carData >> carType >> color >> inTime;
t1 = time(&_time); // 獲取系統(tǒng)當(dāng)前時間
if (carId == carNum)
{
flag = false;
break;
}
}
if (flag) {
cout << "未找到該車輛信息!" << endl;
}
else {
cout << "車牌號" << carNum <<" "<<"車的型號:" << carType <<" " <<
" 車的顏色:" << color << " "<<"停車時長:" << (t1 - inTime) << "秒"
<<" "<< "停車費(fèi) " << (t1 - inTime) * 0.05 << "元" << endl;
}
carData.close();
}
}
void Car::timeAmount() {
time_t it, time1;
int c1 = 0, c2 = 0;
ifstream indata("carData.txt", ios::in);
if (!indata)
{
cerr << "文件打開失敗" << endl;
exit(1);
}
while (indata >> carNum)
{
indata >> carType >> color >> inTime;
it = time(&time1);
if ((it - inTime) / (60 * 60 * 24) >= 24)
{
c1++;
}
else c2++;
}
cout << "車輛總數(shù)是:" << c1 + c2 << endl;
cout << "其中停放超過24小時的有" << c1 << "輛" << endl;
cout << "其中停放不超過24小時的有" << c2 << "輛" << endl;
indata.close();
}
void Car::showInfor() {
int i = 1;
string chepai;
time_t it, time1;
ifstream indata("carData.txt", ios::in);
if (!indata)
{
cerr << "文件打開錯誤" << endl;
exit(1);
}
cout << "停車場中所有汽車信息如下所示:" << endl;
cout << "-----------------------------" << endl;
while (indata >> chepai)
{
indata >> carType >> color >> inTime;
it = time(&time1);
cout << "第" << i << "輛車信息如下" << endl;
cout << "車牌號" << chepai << " 車的型號:" << carType << " 車的顏色:" << color
<< " 停車時間" << (it - inTime) << "秒" <<
" 停車費(fèi) " << (it - inTime) * 0.05 << "元" << endl;
i++;
}
indata.close();
}
void Car::saveInfor() {
ofstream outData("carData.txt", ios::app); // app 追加方式寫文件,即在文件末尾添加
if (!outData) {
cout << "文件打開失??!" << endl;
return;
}
else {
// 將新增車輛信息寫入carData
outData << carNum << " " << carType << " " << color << " " << inTime << endl;
}
outData.close();
}
3.2、普通用戶類及方法實(shí)現(xiàn)
user.h
#pragma once
#include<string>
using namespace std;
// 普通用戶類,只能查看、統(tǒng)計(jì)、顯示車輛,無法實(shí)現(xiàn)增刪改
class User {
public:
void checkCar(); // 普通用戶登錄菜單
};user.cpp
#include<iostream>
#include<Windows.h>
#include"user.h"
#include"car.h"
using namespace std;
void User::checkCar() {
Car car;
while (1) {
system("cls"); // 清空屏幕
cout << "1.顯示車輛狀況" << endl;
cout << "2.查詢車輛信息" << endl;
cout << "3.統(tǒng)計(jì)車輛" << endl;
cout << "4.退出普通用戶" << endl;
int c;
cout << "輸入要執(zhí)行的操作:";
cin >> c;
switch (c) {
case 1: car.showInfor(); break;
case 2: car.findCar(); break;
case 3: car.timeAmount(); break;
case 4: return;
default: cout << "請輸入正確的操作" << endl;
}
system("pause");
}
}
3.3、管理員用戶類及方法實(shí)現(xiàn)
admin.h
#pragma once // 避免同一個頭文件被包含多次
#include<string>
#include"user.h"
using namespace std;
// 管理員類,公有繼承普通用戶類,可以添加,修改,刪除
class Admin:public User {
public:
void Manager(); // 顯示管理員登錄的菜單
};
admin.cpp
#include"admin.h"
#include"car.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void Admin::Manager() {
Car car;
while (1) {
system("cls"); // 清空屏幕
cout << "1.增加車輛" << endl;
cout << "2.顯示所有車輛信息" << endl;
cout << "3.查詢" << endl;
cout << "4.修改" << endl;
cout << "5.刪除" << endl;
cout << "6.統(tǒng)計(jì)" << endl;
cout << "7.退出管理用戶" << endl;
int choice;
cout << "請輸入要執(zhí)行的操作:";
cin >> choice;
switch (choice) {
case 1: car.addCar(); break;
case 2: car.showInfor(); break;
case 3: car.findCar(); break;
case 4: car.modCar(); break;
case 5: car.delCar(); break;
case 6: car.timeAmount(); break;
case 7: return;
default: cout << "輸入錯誤!" << endl;
}
system("pause");
}
}3.4、主函數(shù)調(diào)用情況
#include"user.h"
#include"admin.h"
#include<iostream>
using namespace std;
int main() {
User user; // 普通用戶對象
Admin admin; // 管理員對象
int choice;
while (1) {
system("cls");
cout << "1.普通用戶登錄" << endl;
cout << "2.管理員登錄" << endl;
cout << "3.退出系統(tǒng)" << endl;
cout << "請輸入要執(zhí)行的操作:" << endl;
cin >> choice;
switch (choice) {
case 1: user.checkCar(); break;
case 2: admin.Manager(); break;
case 3: exit(0); // 退出系統(tǒng)
default: cout << "請輸入正確的操作" << endl;
}
system("pause");
}
return 0;
}
四、運(yùn)行界面截圖




以上就是C++實(shí)現(xiàn)停車場管理系統(tǒng)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++停車場管理系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Qt中PaintEvent繪制實(shí)時波形圖的實(shí)現(xiàn)示例
本文主要介紹了Qt中PaintEvent繪制實(shí)時波形圖的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
C++ 中重載和運(yùn)算符重載加號實(shí)現(xiàn)矩陣相加實(shí)例代碼
這篇文章主要介紹了C++ 中重載和運(yùn)算符重載加號實(shí)現(xiàn)矩陣相加實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
c++ vector(向量)使用方法詳解(順序訪問vector的多種方式)
vector是向量類型,它可以容納許多類型的數(shù)據(jù),如若干個整數(shù),所以稱其為容器,本文介紹一下使用方法2013-12-12
C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼
大家好,本篇文章主要講的是C語言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
C++第11版本中的一些強(qiáng)大的新特性小結(jié)
這篇文章主要介紹了C++第11版本中的一些強(qiáng)大的新特性小結(jié),需要的朋友可以參考下2015-12-12

