用cpp写个程序
用cpp写个程序实现一个停车场管理系统。 用户端可以查询自己信息(姓名、 年龄、 拥有的车辆信息、缴费情况) 和自己车辆的信息(车牌号、 颜色、 品牌、 进入和离开时间、 应
缴费用等), 并且可以对车辆进行增减、 进入停车场和离开停车场等操作。 管理端
可以查询当前停车场的状态(剩余车位、 在停车辆及车主等信息)、 准入、 准出、 登
记和删除车主及车辆信息等操作。 下面是一个用C++编写的停车场管理系统的示例程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 车辆信息结构体
struct Vehicle {
string licensePlate;
string color;
string brand;
string enterTime;
string exitTime;
double payment;
};
// 车主信息结构体
struct Owner {
string name;
int age;
vector<Vehicle> vehicles;
bool hasPaid;
};
// 全局变量
vector<Owner> owners;
int totalParkingSpaces = 100;
int remainingParkingSpaces = totalParkingSpaces;
// 函数声明
void addOwner();
void removeOwner();
void addVehicle();
void removeVehicle();
void enterParkingLot();
void exitParkingLot();
void displayParkingLotStatus();
void registerPayment();
int main() {
int choice;
while (true) {
cout << "停车场管理系统" << endl;
cout << "1. 添加车主" << endl;
cout << "2. 删除车主" << endl;
cout << "3. 添加车辆" << endl;
cout << "4. 删除车辆" << endl;
cout << "5. 车辆进入停车场" << endl;
cout << "6. 车辆离开停车场" << endl;
cout << "7. 显示停车场状态" << endl;
cout << "8. 登记缴费" << endl;
cout << "0. 退出" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
addOwner();
break;
case 2:
removeOwner();
break;
case 3:
addVehicle();
break;
case 4:
removeVehicle();
break;
case 5:
enterParkingLot();
break;
case 6:
exitParkingLot();
break;
case 7:
displayParkingLotStatus();
break;
case 8:
registerPayment();
break;
case 0:
return 0;
default:
cout << "无效的选择!" << endl;
}
cout << endl;
}
return 0;
}
// 添加车主
void addOwner() {
Owner owner;
cout << "请输入车主姓名:";
cin >> owner.name;
cout << "请输入车主年龄:";
cin >> owner.age;
cout << "是否已缴费(1-是,0-否):";
cin >> owner.hasPaid;
owners.push_back(owner);
cout << "车主添加成功!" << endl;
}
// 删除车主
void removeOwner() {
string name;
cout << "请输入要删除的车主姓名:";
cin >> name;
for (int i = 0; i < owners.size(); i++) {
if (owners[ i].name == name) {
owners.erase(owners.begin() + i);
cout << "车主删除成功!" << endl;
return;
}
}
cout << "未找到该车主!" << endl;
}
// 添加车辆
void addVehicle() {
string name;
cout << "请输入车主姓名:";
cin >> name;
for (int i = 0; i < owners.size(); i++) {
if (owners[ i].name == name) {
Vehicle vehicle;
cout << "请输入车牌号:";
cin >> vehicle.licensePlate;
cout << "请输入车辆颜色:";
cin >> vehicle.color;
cout << "请输入车辆品牌:";
cin >> vehicle.brand;
owners[ i].vehicles.push_back(vehicle);
cout << "车辆添加成功!" << endl;
return;
}
}
cout << "未找到该车主!" << endl;
}
// 删除车辆
void removeVehicle() {
string name;
cout << "请输入车主姓名:";
cin >> name;
for (int i = 0; i < owners.size(); i++) {
if (owners[ i].name == name) {
string licensePlate;
cout << "请输入要删除的车牌号:";
cin >> licensePlate;
for (int j = 0; j < owners[ i].vehicles.size(); j++) {
if (owners[ i].vehicles.licensePlate == licensePlate) {
owners[ i].vehicles.erase(owners[ i].vehicles.begin() + j);
cout << "车辆删除成功!" << endl;
return;
}
}
cout << "未找到该车辆!" << endl;
return;
}
}
cout << "未找到该车主!" << endl;
}
// 车辆进入停车场
void enterParkingLot() {
string licensePlate;
cout << "请输入车牌号:";
cin >> licensePlate;
for (int i = 0; i < owners.size(); i++) {
for (int j = 0; j < owners[ i].vehicles.size(); j++) {
if (owners[ i].vehicles.licensePlate == licensePlate) {
if (remainingParkingSpaces > 0) {
owners[ i].vehicles.enterTime = "当前时间";
remainingParkingSpaces--;
cout << "车辆进入停车场成功!" << endl;
} else {
cout << "停车场已满,无法进入!" << endl;
}
return;
}
}
}
cout << "未找到该车辆!" << endl;
}
// 车辆离开停车场
void exitParkingLot() {
string licensePlate;
cout << "请输入车牌号:";
cin >> licensePlate;
for (int i = 0; i < owners.size(); i++) {
for (int j = 0; j < owners[ i].vehicles.size(); j++) {
if (owners[ i].vehicles.licensePlate == licensePlate) {
owners[ i].vehicles.exitTime = "当前时间";
remainingParkingSpaces++;
cout << "车辆离开停车场成功!" << endl;
return;
}
}
}
cout << "未找到该车辆!" << endl;
}
// 显示停车场状态
void displayParkingLotStatus() {
cout << "总停车位数:" << totalParkingSpaces << endl;
cout << "剩余停车位数:" << remainingParkingSpaces << endl;
cout << "在停车辆及车主信息:" << endl;
for (int i = 0; i < owners.size(); i++) {
for (int j = 0; j < owners[ i].vehicles.size(); j++) {
cout << "车主姓名:" << owners[ i].name << endl;
cout << "车辆牌照:" << owners[ i].vehicles.licensePlate << endl;
cout << "车辆颜色:" << owners[ i].vehicles.color << endl;
cout << "车辆品牌:" << owners[ i].vehicles.brand << endl;
cout << "进入时间:" << owners[ i].vehicles.enterTime << endl;
cout << "离开时间:" << owners[ i].vehicles.exitTime << endl;
cout << "应缴费用:" << owners[ i].vehicles.payment << endl;
cout << endl;
}
}
}
// 登记缴费
void registerPayment() {
string licensePlate;
cout << "请输入车牌号:";
cin >> licensePlate;
for (int i = 0; i < owners.size(); i++) {
for (int j = 0; j < owners[ i].vehicles.size(); j++) {
if (owners[ i].vehicles.licensePlate == licensePlate) {
owners[ i].vehicles.payment = 10.0; // 假设每小时停车费用为10元
owners[ i].hasPaid = true;
cout << "缴费登记成功!" << endl;
return;
}
}
}
cout << "未找到该车辆!" << endl;
}
这个程序使用了结构体来存储车主和车辆的信息。它提供了以下功能:
1. 添加车主:输入车主的姓名、年龄和缴费情况,将其添加到车主列表中。
2. 删除车主:输入要删除的车主的姓名,从车主列表中删除该车主。
3. 添加车辆:输入车主的姓名,然后输入车辆的牌照、颜色和品牌,将车辆添加到对应车主的车辆列表中。
4. 删除车辆:输入车主的姓名和要删除的车辆的牌照,从对应车主的车辆列表中删除该车辆。
5. 车辆进入停车场:输入车辆的牌照,将车辆的进入时间记录下来,并减少剩余停车位数。
6. 车辆离开停车场:输入车辆的牌照,将车辆的离开时间记录下来,并增加剩余停车位数。
7. 显示停车场状态:显示总停车位数、剩余停车位数以及在停车辆和车主的信息。
8. 登记缴费:输入车辆的牌照,将车辆的应缴费用设置为固定值,并将车主的缴费情况设置为已缴费。
你可以根据实际需求对程序进行修改和扩展。
页:
[1]