用c++写一个民航订票系统,要求使用文件储存数据,不使用链表、vector、数据库,有管理员类,实现查询航班、增加航班、删除航班、修改航班、显示航班等功能。
时间: 2023-06-14 15:08:31 浏览: 127
以下是一个简单的民航订票系统的代码示例:
```c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 航班类
class Flight {
public:
Flight() {}
Flight(string num, string start, string end, string time) : num_(num), start_(start), end_(end), time_(time) {}
// 获取航班号
string GetNum() const {
return num_;
}
// 获取起点
string GetStart() const {
return start_;
}
// 获取终点
string GetEnd() const {
return end_;
}
// 获取时间
string GetTime() const {
return time_;
}
// 设置航班信息
void SetFlight(string num, string start, string end, string time) {
num_ = num;
start_ = start;
end_ = end;
time_ = time;
}
private:
string num_; // 航班号
string start_; // 起点
string end_; // 终点
string time_; // 时间
};
// 管理员类
class Admin {
public:
// 构造函数
Admin() {
LoadData();
}
// 析构函数
~Admin() {
SaveData();
}
// 显示菜单
void ShowMenu() {
cout << "1. 查询航班" << endl;
cout << "2. 增加航班" << endl;
cout << "3. 删除航班" << endl;
cout << "4. 修改航班" << endl;
cout << "5. 显示航班" << endl;
cout << "6. 退出系统" << endl;
}
// 查询航班
void QueryFlight() {
string num;
cout << "请输入航班号:";
cin >> num;
if (flight_map_.count(num) == 0) {
cout << "该航班不存在!" << endl;
}
else {
Flight flight = flight_map_[num];
cout << "航班号:" << flight.GetNum() << endl;
cout << "起点:" << flight.GetStart() << endl;
cout << "终点:" << flight.GetEnd() << endl;
cout << "时间:" << flight.GetTime() << endl;
}
}
// 增加航班
void AddFlight() {
string num, start, end, time;
cout << "请输入航班号:";
cin >> num;
if (flight_map_.count(num) > 0) {
cout << "该航班已存在!" << endl;
}
else {
cout << "请输入起点:";
cin >> start;
cout << "请输入终点:";
cin >> end;
cout << "请输入时间:";
cin >> time;
Flight flight(num, start, end, time);
flight_map_.insert(make_pair(num, flight));
cout << "添加成功!" << endl;
}
}
// 删除航班
void DeleteFlight() {
string num;
cout << "请输入航班号:";
cin >> num;
if (flight_map_.count(num) == 0) {
cout << "该航班不存在!" << endl;
}
else {
flight_map_.erase(num);
cout << "删除成功!" << endl;
}
}
// 修改航班
void ModifyFlight() {
string num;
cout << "请输入航班号:";
cin >> num;
if (flight_map_.count(num) == 0) {
cout << "该航班不存在!" << endl;
}
else {
string start, end, time;
cout << "请输入起点(原:" << flight_map_[num].GetStart() << "):";
cin >> start;
cout << "请输入终点(原:" << flight_map_[num].GetEnd() << "):";
cin >> end;
cout << "请输入时间(原:" << flight_map_[num].GetTime() << "):";
cin >> time;
Flight flight(num, start, end, time);
flight_map_[num] = flight;
cout << "修改成功!" << endl;
}
}
// 显示航班
void ShowFlight() {
cout << "航班信息如下:" << endl;
for (auto it = flight_map_.begin(); it != flight_map_.end(); ++it) {
cout << "航班号:" << it->second.GetNum() << endl;
cout << "起点:" << it->second.GetStart() << endl;
cout << "终点:" << it->second.GetEnd() << endl;
cout << "时间:" << it->second.GetTime() << endl;
cout << endl;
}
}
// 加载数据
void LoadData() {
ifstream fin("data.txt");
if (fin.is_open()) {
string num, start, end, time;
while (fin >> num >> start >> end >> time) {
Flight flight(num, start, end, time);
flight_map_.insert(make_pair(num, flight));
}
fin.close();
}
}
// 保存数据
void SaveData() {
ofstream fout("data.txt");
if (fout.is_open()) {
for (auto it = flight_map_.begin(); it != flight_map_.end(); ++it) {
fout << it->second.GetNum() << " " << it->second.GetStart() << " " << it->second.GetEnd() << " " << it->second.GetTime() << endl;
}
fout.close();
}
}
private:
map<string, Flight> flight_map_; // 航班信息
};
int main() {
Admin admin;
int choice;
do {
admin.ShowMenu();
cout << "请输入选择:";
cin >> choice;
switch (choice) {
case 1:
admin.QueryFlight();
break;
case 2:
admin.AddFlight();
break;
case 3:
admin.DeleteFlight();
break;
case 4:
admin.ModifyFlight();
break;
case 5:
admin.ShowFlight();
break;
case 6:
break;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
} while (choice != 6);
return 0;
}
```
在该示例代码中,使用了 `fstream` 头文件来读写数据文件,使用了 `map` 容器来存储航班信息,实现了管理员类的基本功能。注意,该示例只是一个简单的实现,对于复杂的民航订票系统还需要继续完善。
阅读全文