??作者主頁(yè):微涼秋意
?作者簡(jiǎn)介:后端領(lǐng)域優(yōu)質(zhì)創(chuàng)作者??,CSDN內(nèi)容合伙人??,阿里云專家博主??
之前寫的停車場(chǎng)管理系統(tǒng)或者是通訊錄管理系統(tǒng)都沒(méi)有使用文件錄入、保存數(shù)據(jù),今天帶來(lái)一個(gè)文件錄入信息的C++版停車場(chǎng)管理系統(tǒng)。代碼部分都會(huì)有詳細(xì)注釋,稍加思考都能夠理解該程序的邏輯。
一、案例需求描述
? 停車場(chǎng)管理系統(tǒng)就是模擬停車場(chǎng)進(jìn)行車輛管理的系統(tǒng),該系統(tǒng)分為汽車信息模塊,用戶使用模塊和管理員用戶模塊,各模塊功能如下所示:
1.1、汽車信息模塊
- 添加汽車信息:添加汽車屬性。
- 刪除汽車信息:輸入停車場(chǎng)中存在的車牌號(hào),刪除汽車信息。
- 查找汽車信息:輸入停車場(chǎng)存在的車牌號(hào),顯示汽車詳細(xì)信息。
- 修改汽車信息:輸入停車場(chǎng)內(nèi)存在的車牌號(hào),修改汽車屬性信息。
- 停車時(shí)長(zhǎng)統(tǒng)計(jì):顯示汽車在停車場(chǎng)中停留的時(shí)間和車輛總數(shù)。
- 停車場(chǎng)信息顯示:顯示停車場(chǎng)所有汽車的屬性信息。
- 汽車信息保存:將汽車信息保存到本地文件中。
1.2、普通用戶模塊
? 可以查詢、顯示所有汽車信息及停車費(fèi)信息,另外還包含停車時(shí)長(zhǎng)統(tǒng)計(jì)與退出普通用戶登錄功能。由于是多次操作,因此需要有循環(huán)判斷功能,這種情況多使用while
嵌套switch case
語(yǔ)句實(shí)現(xiàn)。
1.3、管理員用戶模塊
? 此模塊具有普通用戶模塊的所有功能,此外還應(yīng)有增、刪、改的功能。
二、案例分析
? 通過(guò)案例描述我們得到了非常清晰的模塊信息,因此在設(shè)計(jì)類時(shí)應(yīng)該包含普通用戶類、管理員用戶類、汽車信息類。
大致思路:
- 在汽車信息類中實(shí)現(xiàn)基本功能并封裝起來(lái),以便后續(xù)調(diào)用。
- 在普通用戶類中定義菜單功能,通過(guò)鍵入不同數(shù)字實(shí)現(xiàn)不同功能。
- 在管理員用戶類中定義菜單功能,通過(guò)鍵入不同數(shù)字實(shí)現(xiàn)不同功能。
三、案例代碼實(shí)現(xiàn)
? 這里我采用分文件編寫的方式,建立user.h
、admin.h
、car.h
及對(duì)應(yīng)的三個(gè).cpp
文件和main.cpp
文件,在main里面循環(huán)調(diào)用user
和admin
的方法就能實(shí)現(xiàn)停車場(chǎng)管理系統(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; // 汽車編號(hào)
string carType; // 汽車型號(hào)
string color; // 汽車顏色
time_t inTime; // 汽車停車時(shí)間點(diǎn)
public:
void addCar(); // 下面四行對(duì)應(yīng)增刪改查
void delCar();
void modCar();
void findCar();
void timeAmount(); // 計(jì)算停車時(shí)長(zhǎng)并統(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í)間變量,秒數(shù),調(diào)用time()獲取
while (1) {
AA: cout << "請(qǐng)輸入車牌號(hào):";
cin >> carNum;
// 判斷文件中是否已存在相同車牌號(hào)
ifstream ifs("carData.txt", ios::in); // 讀文件
if (ifs) {
char buf[1024];
string strs[20];
int index = 0; // 標(biāo)識(shí)數(shù)組索引
while (!ifs.eof()) { // 讀取文件直到末尾
ifs.getline(buf, 100); // 每次讀取一行數(shù)據(jù),放入buf數(shù)組 注:第二個(gè)參數(shù)為字符數(shù),緩沖區(qū)盡量大,否則循環(huán)會(huì)異常結(jié)束
strs[index++] = buf[0]; // buf[0]為車牌號(hào),存入strs數(shù)組,索引自增
}
// 遍歷strs數(shù)組,auto 自動(dòng)推導(dǎo)數(shù)據(jù)類型,這里等價(jià)于 string
for (auto& num : strs) {
// 判斷輸入車牌號(hào)是否與文件里重復(fù)
if (num == carNum) {
cout << "車牌號(hào)重復(fù)!" << endl;
goto AA; // 重復(fù)后重新輸入車牌號(hào)
}
}
}
// 車牌號(hào)不重復(fù)繼續(xù)加下來(lái)的輸入
cout << "請(qǐng)輸入車的種類:";
cin >> carType;
cout << "請(qǐng)出入車的顏色:";
cin >> color;
inTime = time(&_time); // 記錄停車時(shí)間
// 保存新增車輛信息
saveInfor();
char ch;
cout << "\t是否繼續(xù)?(y/n)"; // 判斷是否繼續(xù)輸入,\t 制表符,通常八個(gè)空格
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 << "文件打開(kāi)失?。? << endl;
return;
}
string carId, name, str;
bool flag = true;
cout << "請(qǐng)輸入要?jiǎng)h除的車牌號(hào):";
cin >> carId;
// 讀取文件第一個(gè)字段(車牌號(hào)) >> 遇空格結(jié)束讀取
while (carData >> name) {
getline(carData,str); // 將該行數(shù)據(jù)讀取到 str
// 如果相同,輸出要?jiǎng)h除的車輛信息:顏色,型號(hào),停車時(shí)間
if (name == carId) {
cout << "要?jiǎng)h除的車輛信息:" << endl << str << endl;
flag = false;
break;
}
// 如果不相同,將車輛信息寫入到temp,否則舍棄該行
outData << name << " " << str << endl;
}
if (flag) cout << "該車牌號(hào)不存在" << endl;
else {
while (getline(carData, str)) { // 繼續(xù)按行讀取,此時(shí)第一行
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 << "請(qǐng)輸入你要修改的車輛的車牌號(hào)" << endl;
cin >> chepai1;
ifstream indata("carData.txt", ios::in);
ofstream outdata("tempCarData.txt", ios::out);
if (!indata || !outdata)
{
cerr << "文件打開(kāi)錯(cuò)誤" << endl;
exit(1);
}
while (indata >> chepai2)
{
indata >> carType >> color >> inTime; // 讀取該行剩余元素
if (chepai1 == chepai2)
{
i = 0;
cout << "已找到所要修改的車輛" << endl;
cout << "修改后的車牌號(hào)" << endl;
cin >> carNum;
cout << "修改后的車輛型號(hào)" << endl;
cin >> carType;
cout << "修改后的車輛顏色" << endl;
cin >> color;
inTime = time(&time1);
// 寫入carData.txt
outdata << carNum << " " << carType << " " << color << " " << inTime << endl;
break;
}
// 車牌號(hào)不同,將車輛信息存到temp
outdata << chepai2 << " " << carType << " " << color << " " << inTime << endl;
}
if (i) {
cout << "停車場(chǎng)中沒(méi)有找到要修改的車輛" << endl;
}
outdata.close();
indata.close();
ifstream in("tempCarData.txt", ios::in);
ofstream out("carData.txt", ios::out);
if (!in || !out)
{
cout << "文件打開(kāi)錯(cuò)誤" << 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 << "文件打開(kāi)失敗" << endl;
return;
}
else {
string carId;
time_t _time, t1;
bool flag = true;
cout << "請(qǐng)輸入要查找的車牌號(hào)" << endl;
cin >> carId;
while (carData >> carNum) // 讀取車牌號(hào)
{
carData >> carType >> color >> inTime;
t1 = time(&_time); // 獲取系統(tǒng)當(dāng)前時(shí)間
if (carId == carNum)
{
flag = false;
break;
}
}
if (flag) {
cout << "未找到該車輛信息!" << endl;
}
else {
cout << "車牌號(hào)" << carNum <<" "<<"車的型號(hào):" << carType <<" " <<
" 車的顏色:" << color << " "<<"停車時(shí)長(zhǎng):" << (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 << "文件打開(kāi)失敗" << 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 << "其中停放超過(guò)24小時(shí)的有" << c1 << "輛" << endl;
cout << "其中停放不超過(guò)24小時(shí)的有" << 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 << "文件打開(kāi)錯(cuò)誤" << endl;
exit(1);
}
cout << "停車場(chǎng)中所有汽車信息如下所示:" << endl;
cout << "-----------------------------" << endl;
while (indata >> chepai)
{
indata >> carType >> color >> inTime;
it = time(&time1);
cout << "第" << i << "輛車信息如下" << endl;
cout << "車牌號(hào)" << chepai << " 車的型號(hào):" << carType << " 車的顏色:" << color
<< " 停車時(shí)間" << (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 << "文件打開(kāi)失??!" << 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ì)、顯示車輛,無(wú)法實(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 << "請(qǐng)輸入正確的操作" << endl;
}
system("pause");
}
}
3.3、管理員用戶類及方法實(shí)現(xiàn)
admin.h
#pragma once // 避免同一個(gè)頭文件被包含多次
#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 << "請(qǐng)輸入要執(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 << "輸入錯(cuò)誤!" << endl;
}
system("pause");
}
}
3.4、主函數(shù)調(diào)用情況
#include"user.h"
#include"admin.h"
#include<iostream>
using namespace std;
int main() {
User user; // 普通用戶對(duì)象
Admin admin; // 管理員對(duì)象
int choice;
while (1) {
system("cls");
cout << "1.普通用戶登錄" << endl;
cout << "2.管理員登錄" << endl;
cout << "3.退出系統(tǒng)" << endl;
cout << "請(qǐng)輸入要執(zhí)行的操作:" << endl;
cin >> choice;
switch (choice) {
case 1: user.checkCar(); break;
case 2: admin.Manager(); break;
case 3: exit(0); // 退出系統(tǒng)
default: cout << "請(qǐng)輸入正確的操作" << endl;
}
system("pause");
}
return 0;
}
四、運(yùn)行界面截圖
剩下的就不展示了。。。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-437334.html
? 這個(gè)停車場(chǎng)管理系統(tǒng)最需要掌握的便是程序設(shè)計(jì)的思想以及文件操作,像分文件編寫或是文件操作我也寫過(guò)文章總結(jié),就在《C++面向?qū)ο蟛糠帧穼诶?,感興趣的朋友可以訂閱學(xué)習(xí)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-437334.html
到了這里,關(guān)于停車場(chǎng)管理系統(tǒng)文件錄入(C++版)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!