模拟电影院售票系统c++
时间: 2023-08-03 09:34:53 浏览: 73
好的,下面是一个简单的模拟电影院售票系统的 C++ 代码示例:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Seat {
public:
int row;
int column;
bool is_available;
Seat(int r, int c) {
row = r;
column = c;
is_available = true;
}
};
class Cinema {
public:
int rows;
int columns;
vector<vector<Seat>> seats;
Cinema(int r, int c) {
rows = r;
columns = c;
seats.resize(rows);
for (int i = 0; i < rows; i++) {
seats[i].resize(columns);
for (int j = 0; j < columns; j++) {
seats[i][j] = Seat(i, j);
}
}
}
bool is_seat_available(int row, int column) {
return seats[row][column].is_available;
}
void book_seat(int row, int column) {
seats[row][column].is_available = false;
}
void print_seats() {
cout << "------------------------" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (seats[i][j].is_available) {
cout << "O ";
} else {
cout << "X ";
}
}
cout << endl;
}
cout << "------------------------" << endl;
}
};
int main() {
Cinema cinema(5, 6);
cinema.print_seats();
int row, column;
cout << "Please enter row number: ";
cin >> row;
cout << "Please enter column number: ";
cin >> column;
if (cinema.is_seat_available(row, column)) {
cinema.book_seat(row, column);
cout << "Booked successfully!" << endl;
cinema.print_seats();
} else {
cout << "The seat is not available." << endl;
}
return 0;
}
```
这个代码示例中,我们定义了两个类:`Seat` 和 `Cinema`。
`Seat` 类表示电影院中的一个座位,包含了座位的行号、列号和是否可用的状态。
`Cinema` 类表示整个电影院,包含了电影院的行数、列数和座位的二维数组。它还提供了三个方法:`is_seat_available()` 可以判断一个座位是否可用,`book_seat()` 可以预订一个座位,`print_seats()` 可以打印出当前座位的状态。
在 `main()` 函数中,我们先创建了一个 `Cinema` 对象,并打印出当前座位的状态。然后,我们从用户输入中获取一个座位的行号和列号,如果这个座位可用,就预订它并打印出当前座位的状态;否则,告诉用户这个座位不可用。