c++ 航班订票系统
时间: 2024-01-04 18:18:37 浏览: 105
C++_民航订票系统
5星 · 资源好评率100%
以下是一个简单的C++航班订票系统的实现,包括添加航班信息、查询航班信息、订票、修改航班信息和退票等功能:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义航班信息结构体
struct Flight {
string flightNo; // 航班号
string departureCity; // 出发城市
string arrivalCity; // 到达城市
string departureTime; // 出发时间
string arrivalTime; // 到达时间
double price; // 票价
double discount; // 折扣
int tickets; // 票数
};
// 定义航班信息数组
vector<Flight> flights;
// 添加航班信息
void addFlight() {
Flight flight;
cout << "请输入航班号:";
cin >> flight.flightNo;
cout << "请输入出发城市:";
cin >> flight.departureCity;
cout << "请输入到达城市:";
cin >> flight.arrivalCity;
cout << "请输入出发时间:";
cin >> flight.departureTime;
cout << "请输入到达时间:";
cin >> flight.arrivalTime;
cout << "请输入票价:";
cin >> flight.price;
cout << "请输入折扣:";
cin >> flight.discount;
cout << "请输入票数:";
cin >> flight.tickets;
flights.push_back(flight);
cout << "添加成功!" << endl;
}
// 按照航班号查询航班信息
void searchByFlightNo() {
string flightNo;
cout << "请输入航班号:";
cin >> flightNo;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo) {
cout << "航班号:" << flights[i].flightNo << endl;
cout << "出发城市:" << flights[i].departureCity << endl;
cout << "到达城市:" << flights[i].arrivalCity << endl;
cout << "出发时间:" << flights[i].departureTime << endl;
cout << "到达时间:" << flights[i].arrivalTime << endl;
cout << "票价:" << flights[i].price << endl;
cout << "折扣:" << flights[i].discount << endl;
cout << "票数:" << flights[i].tickets << endl;
return;
}
}
cout << "未找到航班信息!" << endl;
}
// 按照目的地查询航班信息
void searchByArrivalCity() {
string arrivalCity;
cout << "请输入目的地城市:";
cin >> arrivalCity;
bool found = false;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].arrivalCity == arrivalCity) {
cout << "航班号:" << flights[i].flightNo << endl;
cout << "出发城市:" << flights[i].departureCity << endl;
cout << "到达城市:" << flights[i].arrivalCity << endl;
cout << "出发时间:" << flights[i].departureTime << endl;
cout << "到达时间:" << flights[i].arrivalTime << endl;
cout << "票价:" << flights[i].price << endl;
cout << "折扣:" << flights[i].discount << endl;
cout << "票数:" << flights[i].tickets << endl;
found = true;
}
}
if (!found) {
cout << "未找到航班信息!" << endl;
}
}
// 订票
void bookTicket() {
string flightNo;
cout << "请输入航班号:";
cin >> flightNo;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo) {
if (flights[i].tickets > 0) {
flights[i].tickets--;
cout << "订票成功!" << endl;
} else {
cout << "该航班已售罄!" << endl;
}
return;
}
}
cout << "未找到航班信息!" << endl;
}
// 修改航班信息
void modifyFlight() {
string flightNo;
cout << "请输入需要修改的航班号:";
cin >> flightNo;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo) {
cout << "请输入新的出发城市:";
cin >> flights[i].departureCity;
cout << "请输入新的到达城市:";
cin >> flights[i].arrivalCity;
cout << "请输入新的出发时间:";
cin >> flights[i].departureTime;
cout << "请输入新的到达时间:";
cin >> flights[i].arrivalTime;
cout << "请输入新的票价:";
cin >> flights[i].price;
cout << "请输入新的折扣:";
cin >> flights[i].discount;
cout << "请输入新的票数:";
cin >> flights[i].tickets;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到航班信息!" << endl;
}
// 退票
void refundTicket() {
string flightNo;
cout << "请输入航班号:";
cin >> flightNo;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].flightNo == flightNo) {
flights[i].tickets++;
cout << "退票成功!" << endl;
return;
}
}
cout << "未找到航班信息!" << endl;
}
int main() {
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:
addFlight();
break;
case 2:
searchByFlightNo();
break;
case 3:
searchByArrivalCity();
break;
case 4:
bookTicket();
break;
case 5:
modifyFlight();
break;
case 6:
refundTicket();
break;
case 7:
return 0;
default:
cout << "无效的选择!" << endl;
break;
}
}
return 0;
}
```
阅读全文