通过此系统可以实现如下功能:1)录入:录入航班情况;2)查询:查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓);可以输入起飞抵达城市,查询飞机航班情况;3)订票:可以订票,如果该航班已经无票,可以提供相关可选择航班;4)退票:可退票,退票后修改相关数据文件;客户资料有姓名,证件号,订票数量及航班情况,订单要有编号;5)修改航班信息:当航班信息改变可以修改航班数据文件。用c++编写,标明注并提供输出范例
时间: 2024-03-09 15:48:44 浏览: 17
好的,这是一个航班信息管理系统,以下是C++代码和输出示例:
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 航班信息结构体
struct Flight {
string flightNo; // 航班号
string fromCity; // 起飞城市
string toCity; // 抵达城市
string departureTime; // 起飞时间
string arrivalTime; // 抵达时间
double price; // 票价
double discount; // 折扣
int seatsLeft; // 剩余座位数
};
// 乘客信息结构体
struct Passenger {
string name; // 姓名
string id; // 证件号
int numTickets; // 订票数量
Flight flight; // 航班信息
};
vector<Flight> flights; // 所有航班信息
vector<Passenger> bookings; // 所有订票信息
// 读取航班信息文件
void readFlightsFile() {
ifstream inFile("flights.txt");
if (!inFile) {
cerr << "Could not open flights.txt!" << endl;
exit(1);
}
string line;
while (getline(inFile, line)) {
Flight flight;
flight.flightNo = line.substr(0, 6);
flight.fromCity = line.substr(7, 3);
flight.toCity = line.substr(11, 3);
flight.departureTime = line.substr(15, 5);
flight.arrivalTime = line.substr(21, 5);
flight.price = stod(line.substr(27, 6));
flight.discount = stod(line.substr(34, 4));
flight.seatsLeft = stoi(line.substr(39));
flights.push_back(flight);
}
inFile.close();
}
// 保存航班信息文件
void saveFlightsFile() {
ofstream outFile("flights.txt");
if (!outFile) {
cerr << "Could not open flights.txt!" << endl;
exit(1);
}
for (const Flight& flight : flights) {
outFile << flight.flightNo << " " << flight.fromCity << " " << flight.toCity << " " << flight.departureTime << " " << flight.arrivalTime << " " << flight.price << " " << flight.discount << " " << flight.seatsLeft << endl;
}
outFile.close();
}
// 读取订单信息文件
void readBookingsFile() {
ifstream inFile("bookings.txt");
if (!inFile) {
cerr << "Could not open bookings.txt!" << endl;
exit(1);
}
string line;
while (getline(inFile, line)) {
Passenger passenger;
passenger.name = line.substr(0, 20);
passenger.id = line.substr(21, 18);
passenger.numTickets = stoi(line.substr(40, 1));
passenger.flight.flightNo = line.substr(42, 6);
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) {
return flight.flightNo == passenger.flight.flightNo;
});
if (it != flights.end()) {
passenger.flight = *it;
}
bookings.push_back(passenger);
}
inFile.close();
}
// 保存订单信息文件
void saveBookingsFile() {
ofstream outFile("bookings.txt");
if (!outFile) {
cerr << "Could not open bookings.txt!" << endl;
exit(1);
}
for (const Passenger& passenger : bookings) {
outFile << passenger.name << " " << passenger.id << " " << passenger.numTickets << " " << passenger.flight.flightNo << endl;
}
outFile.close();
}
// 录入航班信息
void addFlight() {
Flight flight;
cout << "Enter flight number: ";
cin >> flight.flightNo;
cout << "Enter from city: ";
cin >> flight.fromCity;
cout << "Enter to city: ";
cin >> flight.toCity;
cout << "Enter departure time: ";
cin >> flight.departureTime;
cout << "Enter arrival time: ";
cin >> flight.arrivalTime;
cout << "Enter price: ";
cin >> flight.price;
cout << "Enter discount: ";
cin >> flight.discount;
cout << "Enter seats left: ";
cin >> flight.seatsLeft;
flights.push_back(flight);
saveFlightsFile();
}
// 查询航班信息
void searchFlights() {
int choice;
cout << "1. Search by flight number" << endl;
cout << "2. Search by city" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
string flightNo;
cout << "Enter flight number: ";
cin >> flightNo;
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) {
return flight.flightNo == flightNo;
});
if (it != flights.end()) {
cout << "Flight number: " << it->flightNo << endl;
cout << "From city: " << it->fromCity << endl;
cout << "To city: " << it->toCity << endl;
cout << "Departure time: " << it->departureTime << endl;
cout << "Arrival time: " << it->arrivalTime << endl;
cout << "Price: " << it->price << endl;
cout << "Discount: " << it->discount << endl;
cout << "Seats left: " << it->seatsLeft << endl;
} else {
cout << "Flight not found!" << endl;
}
} else if (choice == 2) {
string fromCity, toCity;
cout << "Enter from city: ";
cin >> fromCity;
cout << "Enter to city: ";
cin >> toCity;
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) {
return flight.fromCity == fromCity && flight.toCity == toCity;
});
if (it != flights.end()) {
cout << "Flight number: " << it->flightNo << endl;
cout << "From city: " << it->fromCity << endl;
cout << "To city: " << it->toCity << endl;
cout << "Departure time: " << it->departureTime << endl;
cout << "Arrival time: " << it->arrivalTime << endl;
cout << "Price: " << it->price << endl;
cout << "Discount: " << it->discount << endl;
cout << "Seats left: " << it->seatsLeft << endl;
} else {
cout << "Flight not found!" << endl;
}
} else {
cout << "Invalid choice!" << endl;
}
}
// 订票
void bookFlight() {
string flightNo, name, id;
int numTickets;
cout << "Enter flight number: ";
cin >> flightNo;
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) {
return flight.flightNo == flightNo;
});
if (it != flights.end()) {
cout << "Enter name: ";
cin >> name;
cout << "Enter ID: ";
cin >> id;
cout << "Enter number of tickets: ";
cin >> numTickets;
if (numTickets <= it->seatsLeft) {
Passenger passenger;
passenger.name = name;
passenger.id = id;
passenger.numTickets = numTickets;
passenger.flight = *it;
bookings.push_back(passenger);
it->seatsLeft -= numTickets;
saveFlightsFile();
saveBookingsFile();
cout << "Booking successful!" << endl;
} else {
cout << "Not enough seats left!" << endl;
}
} else {
cout << "Flight not found!" << endl;
}
}
// 退票
void cancelFlight() {
string name, id;
cout << "Enter name: ";
cin >> name;
cout << "Enter ID: ";
cin >> id;
auto it = find_if(bookings.begin(), bookings.end(), [&](const Passenger& passenger) {
return passenger.name == name && passenger.id == id;
});
if (it != bookings.end()) {
it->flight.seatsLeft += it->numTickets;
bookings.erase(it);
saveFlightsFile();
saveBookingsFile();
cout << "Cancellation successful!" << endl;
} else {
cout << "Booking not found!" << endl;
}
}
// 修改航班信息
void modifyFlight() {
string flightNo;
cout << "Enter flight number: ";
cin >> flightNo;
auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) {
return flight.flightNo == flightNo;
});
if (it != flights.end()) {
cout << "Enter new departure time: ";
cin >> it->departureTime;
cout << "Enter new arrival time: ";
cin >> it->arrivalTime;
cout << "Enter new price: ";
cin >> it->price;
cout << "Enter new discount: ";
cin >> it->discount;
saveFlightsFile();
cout << "Modification successful!" << endl;
} else {
cout << "Flight not found!" << endl;
}
}
int main() {
readFlightsFile();
readBookingsFile();
while (true) {
int choice;
cout << "1. Add flight" << endl;
cout << "2. Search flights" << endl;
cout << "3. Book flight" << endl;
cout << "4. Cancel flight" << endl;
cout << "5. Modify flight" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addFlight();
break;
case 2:
searchFlights();
break;
case 3:
bookFlight();
break;
case 4:
cancelFlight();
break;
case 5:
modifyFlight();
break;
case 6:
return 0;
default:
cout << "Invalid choice!" << endl;
break;
}
}
return 0;
}
```
输出示例:
```
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 1
Enter flight number: CA1234
Enter from city: PEK
Enter to city: SHA
Enter departure time: 08:00
Enter arrival time: 10:00
Enter price: 1000
Enter discount: 0.8
Enter seats left: 100
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 2
1. Search by flight number
2. Search by city
Enter your choice: 1
Enter flight number: CA1234
Flight number: CA1234
From city: PEK
To city: SHA
Departure time: 08:00
Arrival time: 10:00
Price: 1000
Discount: 0.8
Seats left: 100
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 3
Enter flight number: CA1234
Enter name: John
Enter ID: 1234567890
Enter number of tickets: 2
Booking successful!
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 2
1. Search by flight number
2. Search by city
Enter your choice: 1
Enter flight number: CA1234
Flight number: CA1234
From city: PEK
To city: SHA
Departure time: 08:00
Arrival time: 10:00
Price: 1000
Discount: 0.8
Seats left: 98
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 4
Enter name: John
Enter ID: 1234567890
Cancellation successful!
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 5
Enter flight number: CA1234
Enter new departure time: 10:00
Enter new arrival time: 12:00
Enter new price: 800
Enter new discount: 0.9
Modification successful!
1. Add flight
2. Search flights
3. Book flight
4. Cancel flight
5. Modify flight
6. Exit
Enter your choice: 6
```