用c++编写一个停车场管理系统需要展现一个菜单界面,用户可以根据选项进入普通用户和管理员,用户界面普通用户只能查看汽车信息,只有管理员用户才能添加,修改,删除汽车信息,普通用户和管理员用户具备返回主菜单的功能
时间: 2024-04-30 18:21:04 浏览: 87
好的,以下是一个基本的停车场管理系统的 C++ 代码示例,包含了菜单界面、普通用户和管理员用户的功能。
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Car {
string plateNumber;
string brand;
string model;
};
vector<Car> cars;
void printMainMenu() {
cout << "Welcome to Parking Lot Management System!" << endl;
cout << "1. User Login" << endl;
cout << "2. Admin Login" << endl;
cout << "3. Exit" << endl;
cout << "Please enter your choice: ";
}
void printUserMenu() {
cout << "User Menu:" << endl;
cout << "1. View Car Information" << endl;
cout << "2. Main Menu" << endl;
cout << "Please enter your choice: ";
}
void printAdminMenu() {
cout << "Admin Menu:" << endl;
cout << "1. Add Car Information" << endl;
cout << "2. Modify Car Information" << endl;
cout << "3. Delete Car Information" << endl;
cout << "4. Main Menu" << endl;
cout << "Please enter your choice: ";
}
void viewCarInfo() {
cout << "Car Information:" << endl;
for (int i = 0; i < cars.size(); i++) {
cout << "Plate Number: " << cars[i].plateNumber << endl;
cout << "Brand: " << cars[i].brand << endl;
cout << "Model: " << cars[i].model << endl;
cout << endl;
}
}
void addCarInfo() {
Car car;
cout << "Enter Plate Number: ";
cin >> car.plateNumber;
cout << "Enter Brand: ";
cin >> car.brand;
cout << "Enter Model: ";
cin >> car.model;
cars.push_back(car);
cout << "Car Information Added Successfully!" << endl;
}
void modifyCarInfo() {
string plateNumber;
cout << "Enter Plate Number to Modify: ";
cin >> plateNumber;
for (int i = 0; i < cars.size(); i++) {
if (cars[i].plateNumber == plateNumber) {
cout << "Enter New Brand: ";
cin >> cars[i].brand;
cout << "Enter New Model: ";
cin >> cars[i].model;
cout << "Car Information Modified Successfully!" << endl;
return;
}
}
cout << "Car Not Found!" << endl;
}
void deleteCarInfo() {
string plateNumber;
cout << "Enter Plate Number to Delete: ";
cin >> plateNumber;
for (int i = 0; i < cars.size(); i++) {
if (cars[i].plateNumber == plateNumber) {
cars.erase(cars.begin() + i);
cout << "Car Information Deleted Successfully!" << endl;
return;
}
}
cout << "Car Not Found!" << endl;
}
int main() {
int choice;
do {
printMainMenu();
cin >> choice;
switch (choice) {
case 1: {
int userChoice;
do {
printUserMenu();
cin >> userChoice;
switch (userChoice) {
case 1:
viewCarInfo();
break;
case 2:
break;
default:
cout << "Invalid Choice!" << endl;
break;
}
} while (userChoice != 2);
break;
}
case 2: {
string username, password;
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
if (username == "admin" && password == "password") {
int adminChoice;
do {
printAdminMenu();
cin >> adminChoice;
switch (adminChoice) {
case 1:
addCarInfo();
break;
case 2:
modifyCarInfo();
break;
case 3:
deleteCarInfo();
break;
case 4:
break;
default:
cout << "Invalid Choice!" << endl;
break;
}
} while (adminChoice != 4);
} else {
cout << "Invalid Username or Password!" << endl;
}
break;
}
case 3:
cout << "Goodbye!" << endl;
break;
default:
cout << "Invalid Choice!" << endl;
break;
}
} while (choice != 3);
return 0;
}
```
在上述代码中,我们定义了一个 `Car` 结构体,用于存储汽车信息,然后定义了一个 `vector` 容器 `cars` 用于存储多个汽车信息。接下来,我们定义了一些函数用于打印菜单界面和执行用户选择的操作,例如查看汽车信息、添加汽车信息、修改汽车信息、删除汽车信息等。
在 `main` 函数中,我们使用一个 `do-while` 循环来实现用户不断选择菜单选项的功能。当用户选择普通用户或管理员用户时,我们再使用一个嵌套的 `do-while` 循环来实现用户不断选择用户菜单或管理员菜单选项的功能。当用户选择退出时,我们退出循环并结束程序。
当然,上述代码只是一个基本的示例,实际应用中可能还需要加入更多的功能和处理更多的异常情况。
阅读全文