给我c++代码是飞机订票系统设计
时间: 2024-03-09 09:46:50 浏览: 30
好的,以下是一个简单的飞机订票系统的 C++ 代码示例:
```c++
#include <iostream>
using namespace std;
const int ROWS = 10;
const int SEATS_PER_ROW = 6;
char seats[ROWS][SEATS_PER_ROW];
void initSeats() {
for(int i = 0; i < ROWS; i++) {
for(int j = 0; j < SEATS_PER_ROW; j++) {
seats[i][j] = 'O';
}
}
}
void printSeats() {
cout << " A B C D E F" << endl;
for(int i = 0; i < ROWS; i++) {
cout << i+1 << " ";
for(int j = 0; j < SEATS_PER_ROW; j++) {
cout << seats[i][j] << " ";
}
cout << endl;
}
}
bool reserveSeat(int row, char seat) {
int col = seat - 'A';
if(row < 1 || row > ROWS || col < 0 || col >= SEATS_PER_ROW) {
cout << "Invalid seat selection." << endl;
return false;
}
if(seats[row-1][col] == 'X') {
cout << "That seat is already reserved." << endl;
return false;
}
seats[row-1][col] = 'X';
cout << "Seat " << seat << row << " has been reserved." << endl;
return true;
}
int main() {
initSeats();
printSeats();
int row;
char seat;
cout << "Enter row number (1-10): ";
cin >> row;
cout << "Enter seat letter (A-F): ";
cin >> seat;
reserveSeat(row, seat);
cout << endl;
printSeats();
return 0;
}
```
代码实现了以下功能:
1. 初始化座位矩阵。
2. 打印座位矩阵。
3. 预定指定座位。
4. 在预定座位后重新打印座位矩阵。
当程序运行时,它会要求用户输入行号和座位字母,然后尝试预定该座位。如果预定成功,它会显示 "Seat X1 has been reserved."(其中 X 为座位字母)。如果预定失败,它会显示相应的错误信息。最后,它会显示更新后的座位矩阵。