好的,以下是一个简单的示例代码,演示如何使用C++的多文件形式来实现民航订票系统。
1. main.cpp
```c++
#include
#include "flight.h"
using namespace std;
int main() {
Flight flights[3];
initFlights(flights, 3);
displayFlights(flights, 3);
int choice;
cout << "请选择要预订的航班编号:";
cin >> choice;
if (bookFlight(flights[choice - 1])) {
cout << "预订成功!" << endl;
} else {
cout << "预订失败,请选择其他航班。" << endl;
}
return 0;
}
```
2. flight.h
```c++
#ifndef FLIGHT_H
#define FLIGHT_H
struct Flight {
int id;
int seats;
bool booked;
};
void initFlights(Flight flights[], int n);
void displayFlights(Flight flights[], int n);
bool bookFlight(Flight& flight);
#endif
```
3. flight.cpp
```c++
#include
#include "flight.h"
using namespace std;
void initFlights(Flight flights[], int n) {
for (int i = 0; i < n; i++) {
flights[i].id = i + 1;
flights[i].seats = 50;
flights[i].booked = false;
}
}
void displayFlights(Flight flights[], int n) {
cout << "航班列表:" << endl;
for (int i = 0; i < n; i++) {
cout << "航班编号:" << flights[i].id << ",座位数:" << flights[i].seats;
if (flights[i].booked) {
cout << ",已预订。";
} else {
cout << ",未预订。";
}
cout << endl;
}
}
bool bookFlight(Flight& flight) {
if (flight.booked || flight.seats <= 0) {
return false;
}
flight.booked = true;
flight.seats--;
return true;
}
```
在这个示例中,我们