C++编写汽车站车票管理系统,功能包含增加车次信息,查看所有车次信息,车辆信息查询,注销车次,车辆信息查询,购票,退票案例
时间: 2023-08-12 14:48:13 浏览: 67
好的,下面是一个简单的汽车站车票管理系统的实现示例:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// 车次信息结构体
struct Train {
string id; // 车次号
string start; // 起始站点
string end; // 终点站点
string time; // 发车时间
double price; // 车票价格
int seats; // 座位数量
};
// 全局变量,存储所有车次信息
vector<Train> trains;
// 从文件中读取车次信息
void loadTrainsFromFile() {
ifstream fin("trains.txt");
if (!fin.is_open()) {
cout << "无法打开文件 trains.txt" << endl;
return;
}
trains.clear();
string line;
while (getline(fin, line)) {
Train train;
sscanf(line.c_str(), "%s %s %s %s %lf %d", &train.id, &train.start, &train.end, &train.time, &train.price, &train.seats);
trains.push_back(train);
}
fin.close();
}
// 将车次信息写入文件
void saveTrainsToFile() {
ofstream fout("trains.txt");
for (auto train : trains) {
fout << train.id << " " << train.start << " " << train.end << " " << train.time << " " << train.price << " " << train.seats << endl;
}
fout.close();
}
// 显示所有车次信息
void showAllTrains() {
cout << "所有车次信息:" << endl;
for (auto train : trains) {
cout << train.id << " " << train.start << " -> " << train.end << " " << train.time << " " << train.price << " " << train.seats << endl;
}
}
// 查询车次信息
void searchTrains() {
string query;
cout << "请输入查询关键字:";
cin >> query;
bool found = false;
for (auto train : trains) {
if (train.id == query || train.start == query || train.end == query || train.time == query) {
cout << train.id << " " << train.start << " -> " << train.end << " " << train.time << " " << train.price << " " << train.seats << endl;
found = true;
}
}
if (!found) {
cout << "没有找到匹配的车次信息" << endl;
}
}
// 增加车次信息
void addTrain() {
Train train;
cout << "请输入车次号:";
cin >> train.id;
cout << "请输入起始站点:";
cin >> train.start;
cout << "请输入终点站点:";
cin >> train.end;
cout << "请输入发车时间:";
cin >> train.time;
cout << "请输入车票价格:";
cin >> train.price;
cout << "请输入座位数量:";
cin >> train.seats;
trains.push_back(train);
saveTrainsToFile();
cout << "车次信息已添加" << endl;
}
// 注销车次信息
void removeTrain() {
string id;
cout << "请输入要注销的车次号:";
cin >> id;
bool found = false;
for (auto it = trains.begin(); it != trains.end(); ++it) {
if (it->id == id) {
trains.erase(it);
found = true;
break;
}
}
if (found) {
saveTrainsToFile();
cout << "车次信息已注销" << endl;
} else {
cout << "没有找到匹配的车次信息" << endl;
}
}
// 购票
void buyTicket() {
string id;
cout << "请输入要购买的车次号:";
cin >> id;
bool found = false;
for (auto it = trains.begin(); it != trains.end(); ++it) {
if (it->id == id) {
if (it->seats > 0) {
// 有余票,可以购买
--it->seats;
saveTrainsToFile();
cout << "车票已购买" << endl;
} else {
// 没有余票
cout << "该车次已无余票" << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "没有找到匹配的车次信息" << endl;
}
}
// 退票
void returnTicket() {
string id;
cout << "请输入要退票的车次号:";
cin >> id;
bool found = false;
for (auto it = trains.begin(); it != trains.end(); ++it) {
if (it->id == id) {
++it->seats;
saveTrainsToFile();
cout << "车票已退回" << endl;
found = true;
break;
}
}
if (!found) {
cout << "没有找到匹配的车次信息" << endl;
}
}
int main() {
loadTrainsFromFile(); // 从文件中读取车次信息
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 查看所有车次信息" << endl;
cout << "2. 查询车次信息" << endl;
cout << "3. 增加车次信息" << endl;
cout << "4. 注销车次信息" << endl;
cout << "5. 购票" << endl;
cout << "6. 退票" << endl;
cout << "0. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
showAllTrains();
break;
case 2:
searchTrains();
break;
case 3:
addTrain();
break;
case 4:
removeTrain();
break;
case 5:
buyTicket();
break;
case 6:
returnTicket();
break;
case 0:
return 0;
default:
cout << "无效的选择" << endl;
break;
}
}
return 0;
}
```
这个程序使用了结构体 `Train` 来表示每个车次的信息,使用了容器 `vector` 来存储所有车次信息,并使用了文件读写来实现数据的持久化。用户可以通过菜单选择不同的操作来实现对车票信息的管理,包括查看所有车次信息、查询车次信息、增加车次信息、注销车次信息、购票和退票等。当用户进行某个操作时,程序会调用相应的函数来实现该功能,例如在购票功能中,程序会查找匹配的车次信息并更新座位数量,然后将更新后的信息写入文件中。