飞机票订票系统C++
时间: 2023-12-28 19:05:26 浏览: 112
以下是一个简单的C++飞机票订票和退票系统的实现,包括订票、退票、查询航班信息、查询乘客信息和计算销售额等功能:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
// 定义航班信息结构体
struct Flight {
string flightNo; // 航班号
string date; // 日期
int price; // 价格
int seats[20][4]; // 座位信息,0表示空位,1表示已订
};
// 定义乘客信息结构体
struct Passenger {
string name; // 姓名
string id; // 身份证号
string flightNo; // 航班号
string date; // 日期
string seat; // 座位号
};
// 定义航班信息和乘客信息的容器
vector<Flight> flights;
vector<Passenger> passengers;
// 初始化航班信息
void initFlights() {
for (int i = 0; i < 10; i++) {
Flight flight;
flight.flightNo = "CA" + to_string(i + 1);
flight.date = "2022-01-01";
flight.price = 1000;
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 4; k++) {
flight.seats[j][k] = 0;
}
}
flights.push_back(flight);
}
}
// 显示航班信息
void showFlights() {
cout << "航班号\t日期\t价格\t余票" << endl;
for (int i = 0; i < flights.size(); i++) {
Flight flight = flights[i];
int count = 0;
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 4; k++) {
if (flight.seats[j][k] == 0) {
count++;
}
}
}
cout << flight.flightNo << "\t" << flight.date << "\t" << flight.price << "\t" << count << endl;
}
}
// 订票
void bookTicket() {
string name, id, flightNo, date, seat;
cout << "请输入姓名:";
cin >> name;
cout << "请输入身份证号:";
cin >> id;
cout << "请输入航班号:";
cin >> flightNo;
cout << "请输入乘坐日期:";
cin >> date;
cout << "请输入座位号:";
cin >> seat;
// 查找航班
int index = -1;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo && flights[i].date == date) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到该航班,请重新输入!" << endl;
return;
}
// 查找座位
int row = seat[0] - '0' - 1;
int col = seat[1] - 'A';
if (flights[index].seats[row][col] == 1) {
cout << "该座位已被预订,请重新选择!" << endl;
return;
}
// 订票
flights[index].seats[row][col] = 1;
Passenger passenger;
passenger.name = name;
passenger.id = id;
passenger.flightNo = flightNo;
passenger.date = date;
passenger.seat = seat;
passengers.push_back(passenger);
cout << "订票成功!" << endl;
}
// 退票
void cancelTicket() {
string id, flightNo, date;
cout << "请输入身份证号:";
cin >> id;
cout << "请输入航班号:";
cin >> flightNo;
cout << "请输入乘坐日期:";
cin >> date;
// 查找乘客信息
int index = -1;
for (int i = 0; i < passengers.size(); i++) {
if (passengers[i].id == id && passengers[i].flightNo == flightNo && passengers[i].date == date) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到该乘客信息,请重新输入!" << endl;
return;
}
// 退票
string seat = passengers[index].seat;
int row = seat[0] - '0' - 1;
int col = seat[1] - 'A';
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo && flights[i].date == date) {
flights[i].seats[row][col] = 0;
break;
}
}
passengers.erase(passengers.begin() + index);
cout << "退票成功!" << endl;
}
// 查询航班信息
void queryFlight() {
string flightNo, date;
cout << "请输入航班号:";
cin >> flightNo;
cout << "请输入乘坐日期:";
cin >> date;
// 查找航班
int index = -1;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo && flights[i].date == date) {
index = i;
break;
}
}
if (index == -1) {
cout << "没有找到该航班,请重新输入!" << endl;
return;
}
// 显示航班信息
cout << "航班号\t日期\t价格\t余票" << endl;
Flight flight = flights[index];
int count = 0;
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 4; k++) {
if (flight.seats[j][k] == 0) {
count++;
}
}
}
cout << flight.flightNo << "\t" << flight.date << "\t" << flight.price << "\t" << count << endl;
}
// 查询乘客信息
void queryPassenger() {
string flightNo, date;
cout << "请输入航班号:";
cin >> flightNo;
cout << "请输入乘坐日期:";
cin >> date;
// 查找乘客信息
map<string, string> passengerMap;
for (int i = 0; i < passengers.size(); i++) {
if (passengers[i].flightNo == flightNo && passengers[i].date == date) {
passengerMap[passengers[i].seat] = passengers[i].name;
}
}
// 显示乘客信息
cout << "座位号\t姓名" << endl;
for (int j = 0; j < 20; j++) {
for (int k = 0; k < 4; k++) {
string seat = to_string(j + 1) + string(1, 'A' + k);
if (passengerMap.count(seat) > 0) {
cout << seat << "\t" << passengerMap[seat] << endl;
}
else {
cout << seat << "\t" << "空位" << endl;
}
}
}
}
// 计算销售额
void calcRevenue() {
string date;
cout << "请输入乘坐日期:";
cin >> date;
int revenue = 0;
for (int i = 0; i < passengers.size(); i++) {
if (passengers[i].date == date) {
for (int j = 0; j < flights.size(); j++) {
if (flights[j].flightNo == passengers[i].flightNo && flights[j].date == date) {
revenue += flights[j].price;
break;
}
}
}
}
cout << "销售额为:" << revenue << endl;
}
int main() {
initFlights();
while (true) {
cout << "请选择功能:" << endl;
cout << "1. 显示航班信息" << endl;
cout << "2. 订票" << endl;
cout << "3. 退票" << endl;
cout << "4. 查询航班信息" << endl;
cout << "5. 查询乘客信息" << endl;
cout << "6. 计算销售额" << endl;
cout << "7. 退出系统" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
showFlights();
break;
case 2:
bookTicket();
break;
case 3:
cancelTicket();
break;
case 4:
queryFlight();
break;
case 5:
queryPassenger();
break;
case 6:
calcRevenue();
break;
case 7:
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
阅读全文