用c++完成火车车票系统本管理系统有以下功能: 1 班次录入 9 2 浏览班次信息3 查询火车信息购买火车票5 退订火车票6 退出系统
时间: 2024-03-11 20:49:44 浏览: 128
好的,下面是一个简单的火车车票系统的实现,包含了你提到的所有功能:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
// 班次结构体
struct Train {
string trainNumber; // 班次号
string startStation; // 始发站
string endStation; // 终点站
string departureTime; // 发车时间
int seatNum; // 座位数
int price; // 票价
};
// 班次数组
vector<Train> trains;
// 读取班次信息
void readTrains() {
ifstream infile("trains.txt");
while (!infile.eof()) {
Train train;
infile >> train.trainNumber >> train.startStation >> train.endStation >> train.departureTime >> train.seatNum >> train.price;
if (!train.trainNumber.empty()) {
trains.push_back(train);
}
}
infile.close();
}
// 保存班次信息
void saveTrains() {
ofstream outfile("trains.txt");
for (int i = 0; i < trains.size(); i++) {
Train& train = trains[i];
outfile << train.trainNumber << " " << train.startStation << " " << train.endStation << " " << train.departureTime << " " << train.seatNum << " " << train.price << endl;
}
outfile.close();
}
// 显示菜单
void showMenu() {
cout << "1. 班次录入" << endl;
cout << "2. 浏览班次信息" << endl;
cout << "3. 查询火车信息" << endl;
cout << "4. 购买火车票" << endl;
cout << "5. 退订火车票" << endl;
cout << "6. 退出系统" << endl;
cout << "请选择操作:";
}
// 班次录入
void addTrain() {
Train train;
cout << "请输入班次号:";
cin >> train.trainNumber;
cout << "请输入始发站:";
cin >> train.startStation;
cout << "请输入终点站:";
cin >> train.endStation;
cout << "请输入发车时间:";
cin >> train.departureTime;
cout << "请输入座位数:";
cin >> train.seatNum;
cout << "请输入票价:";
cin >> train.price;
trains.push_back(train);
cout << "班次录入成功!" << endl;
}
// 浏览班次信息
void viewTrains() {
cout << "班次号 始发站 终点站 发车时间 座位数 票价" << endl;
for (int i = 0; i < trains.size(); i++) {
Train& train = trains[i];
cout << train.trainNumber << " " << train.startStation << " " << train.endStation << " " << train.departureTime << " " << train.seatNum << " " << train.price << endl;
}
}
// 查询火车信息
void searchTrain() {
string startStation, endStation;
cout << "请输入起点站:";
cin >> startStation;
cout << "请输入终点站:";
cin >> endStation;
bool found = false;
for (int i = 0; i < trains.size(); i++) {
Train& train = trains[i];
if (train.startStation == startStation && train.endStation == endStation) {
cout << train.trainNumber << " " << train.startStation << " " << train.endStation << " " << train.departureTime << " " << train.seatNum << " " << train.price << endl;
found = true;
}
}
if (!found) {
cout << "没有符合条件的班次!" << endl;
}
}
// 购买火车票
void buyTicket() {
string trainNumber;
int num;
cout << "请输入班次号:";
cin >> trainNumber;
cout << "请输入购买数量:";
cin >> num;
bool found = false;
for (int i = 0; i < trains.size(); i++) {
Train& train = trains[i];
if (train.trainNumber == trainNumber) {
if (train.seatNum >= num) {
train.seatNum -= num;
int totalPrice = train.price * num;
cout << "购买成功,总价:" << totalPrice << "元。" << endl;
found = true;
} else {
cout << "座位不足,购买失败!" << endl;
found = true;
}
break;
}
}
if (!found) {
cout << "没有找到该班次!" << endl;
} else {
saveTrains();
}
}
// 退订火车票
void refundTicket() {
string trainNumber;
int num;
cout << "请输入班次号:";
cin >> trainNumber;
cout << "请输入退订数量:";
cin >> num;
bool found = false;
for (int i = 0; i < trains.size(); i++) {
Train& train = trains[i];
if (train.trainNumber == trainNumber) {
train.seatNum += num;
cout << "退订成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "没有找到该班次!" << endl;
} else {
saveTrains();
}
}
int main() {
readTrains();
while (true) {
showMenu();
int choice;
cin >> choice;
switch (choice) {
case 1:
addTrain();
saveTrains();
break;
case 2:
viewTrains();
break;
case 3:
searchTrain();
break;
case 4:
buyTicket();
break;
case 5:
refundTicket();
break;
case 6:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "无效的选择!" << endl;
break;
}
}
return 0;
}
```
注意:以上代码只是一个简单的示例,仅供参考,实际开发中可能需要更完善的错误处理、数据验证、用户界面等方面的实现。
阅读全文