利用C++实现:电影管理:实现电影信息和用户信息维护的基本操作。如电影的增加、修改、删除、查找、显示等。
时间: 2023-10-19 15:43:09 浏览: 212
您可以使用C++来实现电影管理系统,以下是一个简单的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 电影类
class Movie {
public:
string title;
string director;
int year;
Movie(string title, string director, int year) {
this->title = title;
this->director = director;
this->year = year;
}
};
// 电影管理系统类
class MovieManager {
private:
vector<Movie> movies;
public:
// 添加电影
void addMovie(string title, string director, int year) {
Movie movie(title, director, year);
movies.push_back(movie);
cout << "电影添加成功!" << endl;
}
// 修改电影信息
void modifyMovie(string title) {
for (int i = 0; i < movies.size(); i++) {
if (movies[i].title == title) {
cout << "请输入新的导演名字:";
cin >> movies[i].director;
cout << "请输入新的上映年份:";
cin >> movies[i].year;
cout << "电影信息修改成功!" << endl;
return;
}
}
cout << "未找到该电影!" << endl;
}
// 删除电影
void deleteMovie(string title) {
for (int i = 0; i < movies.size(); i++) {
if (movies[i].title == title) {
movies.erase(movies.begin() + i);
cout << "电影删除成功!" << endl;
return;
}
}
cout << "未找到该电影!" << endl;
}
// 查找电影
void searchMovie(string title) {
for (int i = 0; i < movies.size(); i++) {
if (movies[i].title == title) {
cout << "电影信息:" << endl;
cout << "片名:" << movies[i].title << endl;
cout << "导演:" << movies[i].director << endl;
cout << "上映年份:" << movies[i].year << endl;
return;
}
}
cout << "未找到该电影!" << endl;
}
// 显示所有电影
void displayMovies() {
if (movies.empty()) {
cout << "暂无电影信息!" << endl;
return;
}
cout << "所有电影信息:" << endl;
for (int i = 0; i < movies.size(); i++) {
cout << "片名:" << movies[i].title << endl;
cout << "导演:" << movies[i].director << endl;
cout << "上映年份:" << movies[i].year << endl;
cout << endl;
}
}
};
int main() {
MovieManager movieManager;
while (true) {
cout << "请选择操作:\n1. 添加电影\n2. 修改电影信息\n3. 删除电影\n4. 查找电影\n5. 显示所有电影\n6. 退出" << endl;
int choice;
cin >> choice;
if (choice == 1) {
string title, director;
int year;
cout << "请输入电影片名:";
cin >> title;
cout << "请输入导演名字:";
cin >> director;
cout << "请输入上映年份:";
cin >> year;
movieManager.addMovie(title, director, year);
} else if (choice == 2) {
string title;
cout << "请输入要修改的电影片名:";
cin >> title;
movieManager.modifyMovie(title);
} else if (choice == 3) {
string title;
cout << "请输入要删除的电影片名:";
cin >> title;
movieManager.deleteMovie(title);
} else if (choice == 4) {
string title;
cout << "请输入要查找的电影片名:";
cin >> title;
movieManager.searchMovie(title);
} else if (choice == 5) {
movieManager.displayMovies();
} else if (choice == 6) {
break;
} else {
cout << "无效的选择!" << endl;
}
cout << endl;
}
return 0;
}
```
以上代码实现了一个简单的电影管理系统,可以进行电影的增加、修改、删除、查找和显示操作。您可以根据自己的需求进行扩展和优化。
阅读全文