用c++类和文件写火车购票管理系统的代码
时间: 2023-12-03 21:44:41 浏览: 73
下面是一个简单的火车购票管理系统的代码,使用了C++类和文件来实现:
```
#include <iostream>
#include <fstream> // 文件操作
#include <string>
using namespace std;
class Train {
private:
string name;
int numSeats;
public:
Train() {}
Train(string n, int s) : name(n), numSeats(s) {}
string getName() const {
return name;
}
int getNumSeats() const {
return numSeats;
}
void reserveSeat() {
if (numSeats > 0) {
numSeats--;
cout << "Successfully reserved a seat.\n";
}
else {
cout << "Sorry, no more seats available.\n";
}
}
void printInfo() const {
cout << "Train name: " << name << '\n';
cout << "Number of available seats: " << numSeats << '\n';
}
};
class TrainManager {
private:
Train trains[10];
int numTrains;
public:
TrainManager() : numTrains(0) {}
void addTrain(const Train& t) {
if (numTrains < 10) {
trains[numTrains] = t;
numTrains++;
cout << "Successfully added a train.\n";
}
else {
cout << "Sorry, the maximum number of trains has been reached.\n";
}
}
void reserveSeat(const string& trainName) {
for (int i = 0; i < numTrains; i++) {
if (trains[i].getName() == trainName) {
trains[i].reserveSeat();
return;
}
}
cout << "Train not found.\n";
}
void printTrainInfo(const string& trainName) const {
for (int i = 0; i < numTrains; i++) {
if (trains[i].getName() == trainName) {
trains[i].printInfo();
return;
}
}
cout << "Train not found.\n";
}
void saveToFile(const string& fileName) const {
ofstream outFile(fileName);
if (outFile) {
outFile << numTrains << '\n';
for (int i = 0; i < numTrains; i++) {
outFile << trains[i].getName() << '\n';
outFile << trains[i].getNumSeats() << '\n';
}
cout << "Successfully saved data to file.\n";
}
else {
cout << "Error: could not open file for writing.\n";
}
}
void loadFromFile(const string& fileName) {
ifstream inFile(fileName);
if (inFile) {
inFile >> numTrains;
for (int i = 0; i < numTrains; i++) {
string name;
int numSeats;
inFile >> name >> numSeats;
trains[i] = Train(name, numSeats);
}
cout << "Successfully loaded data from file.\n";
}
else {
cout << "Error: could not open file for reading.\n";
}
}
};
int main() {
TrainManager manager;
int choice;
string trainName;
string fileName;
while (true) {
cout << "1. Add train\n";
cout << "2. Reserve seat\n";
cout << "3. Print train info\n";
cout << "4. Save to file\n";
cout << "5. Load from file\n";
cout << "6. Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
string name;
int numSeats;
cout << "Enter train name: ";
cin >> name;
cout << "Enter number of seats: ";
cin >> numSeats;
manager.addTrain(Train(name, numSeats));
break;
case 2:
cout << "Enter train name: ";
cin >> trainName;
manager.reserveSeat(trainName);
break;
case 3:
cout << "Enter train name: ";
cin >> trainName;
manager.printTrainInfo(trainName);
break;
case 4:
cout << "Enter file name: ";
cin >> fileName;
manager.saveToFile(fileName);
break;
case 5:
cout << "Enter file name: ";
cin >> fileName;
manager.loadFromFile(fileName);
break;
case 6:
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}
```
该程序使用Train类来表示每个火车,TrainManager类用于管理所有火车。用户可以添加新的火车,预订座位,打印火车信息,保存和加载数据到文件中。
在该程序中,我们使用了fstream库中的ofstream和ifstream类来进行文件的写入和读取。当用户选择保存数据时,程序将把当前所有火车的信息写入到指定的文件中;当用户选择从文件加载数据时,程序将从指定的文件中读取数据并恢复火车列表。
阅读全文