通过此系统可以实现如下功能:1)录入:录入航班情况;2)查询:查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓);可以输入起飞抵达城市,查询飞机航班情况;3)订票:可以订票,如果该航班已经无票,可以提供相关可选择航班;4)退票:可退票,退票后修改相关数据文件;客户资料有姓名,证件号,订票数量及航班情况,订单要有编号;5)修改航班信息:当航班信息改变可以修改航班数据文件。用c++编写,标明注并提供输出范例

时间: 2024-03-09 15:48:44 浏览: 17
好的,这是一个航班信息管理系统,以下是C++代码和输出示例: ```cpp #include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> using namespace std; // 航班信息结构体 struct Flight { string flightNo; // 航班号 string fromCity; // 起飞城市 string toCity; // 抵达城市 string departureTime; // 起飞时间 string arrivalTime; // 抵达时间 double price; // 票价 double discount; // 折扣 int seatsLeft; // 剩余座位数 }; // 乘客信息结构体 struct Passenger { string name; // 姓名 string id; // 证件号 int numTickets; // 订票数量 Flight flight; // 航班信息 }; vector<Flight> flights; // 所有航班信息 vector<Passenger> bookings; // 所有订票信息 // 读取航班信息文件 void readFlightsFile() { ifstream inFile("flights.txt"); if (!inFile) { cerr << "Could not open flights.txt!" << endl; exit(1); } string line; while (getline(inFile, line)) { Flight flight; flight.flightNo = line.substr(0, 6); flight.fromCity = line.substr(7, 3); flight.toCity = line.substr(11, 3); flight.departureTime = line.substr(15, 5); flight.arrivalTime = line.substr(21, 5); flight.price = stod(line.substr(27, 6)); flight.discount = stod(line.substr(34, 4)); flight.seatsLeft = stoi(line.substr(39)); flights.push_back(flight); } inFile.close(); } // 保存航班信息文件 void saveFlightsFile() { ofstream outFile("flights.txt"); if (!outFile) { cerr << "Could not open flights.txt!" << endl; exit(1); } for (const Flight& flight : flights) { outFile << flight.flightNo << " " << flight.fromCity << " " << flight.toCity << " " << flight.departureTime << " " << flight.arrivalTime << " " << flight.price << " " << flight.discount << " " << flight.seatsLeft << endl; } outFile.close(); } // 读取订单信息文件 void readBookingsFile() { ifstream inFile("bookings.txt"); if (!inFile) { cerr << "Could not open bookings.txt!" << endl; exit(1); } string line; while (getline(inFile, line)) { Passenger passenger; passenger.name = line.substr(0, 20); passenger.id = line.substr(21, 18); passenger.numTickets = stoi(line.substr(40, 1)); passenger.flight.flightNo = line.substr(42, 6); auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) { return flight.flightNo == passenger.flight.flightNo; }); if (it != flights.end()) { passenger.flight = *it; } bookings.push_back(passenger); } inFile.close(); } // 保存订单信息文件 void saveBookingsFile() { ofstream outFile("bookings.txt"); if (!outFile) { cerr << "Could not open bookings.txt!" << endl; exit(1); } for (const Passenger& passenger : bookings) { outFile << passenger.name << " " << passenger.id << " " << passenger.numTickets << " " << passenger.flight.flightNo << endl; } outFile.close(); } // 录入航班信息 void addFlight() { Flight flight; cout << "Enter flight number: "; cin >> flight.flightNo; cout << "Enter from city: "; cin >> flight.fromCity; cout << "Enter to city: "; cin >> flight.toCity; cout << "Enter departure time: "; cin >> flight.departureTime; cout << "Enter arrival time: "; cin >> flight.arrivalTime; cout << "Enter price: "; cin >> flight.price; cout << "Enter discount: "; cin >> flight.discount; cout << "Enter seats left: "; cin >> flight.seatsLeft; flights.push_back(flight); saveFlightsFile(); } // 查询航班信息 void searchFlights() { int choice; cout << "1. Search by flight number" << endl; cout << "2. Search by city" << endl; cout << "Enter your choice: "; cin >> choice; if (choice == 1) { string flightNo; cout << "Enter flight number: "; cin >> flightNo; auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) { return flight.flightNo == flightNo; }); if (it != flights.end()) { cout << "Flight number: " << it->flightNo << endl; cout << "From city: " << it->fromCity << endl; cout << "To city: " << it->toCity << endl; cout << "Departure time: " << it->departureTime << endl; cout << "Arrival time: " << it->arrivalTime << endl; cout << "Price: " << it->price << endl; cout << "Discount: " << it->discount << endl; cout << "Seats left: " << it->seatsLeft << endl; } else { cout << "Flight not found!" << endl; } } else if (choice == 2) { string fromCity, toCity; cout << "Enter from city: "; cin >> fromCity; cout << "Enter to city: "; cin >> toCity; auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) { return flight.fromCity == fromCity && flight.toCity == toCity; }); if (it != flights.end()) { cout << "Flight number: " << it->flightNo << endl; cout << "From city: " << it->fromCity << endl; cout << "To city: " << it->toCity << endl; cout << "Departure time: " << it->departureTime << endl; cout << "Arrival time: " << it->arrivalTime << endl; cout << "Price: " << it->price << endl; cout << "Discount: " << it->discount << endl; cout << "Seats left: " << it->seatsLeft << endl; } else { cout << "Flight not found!" << endl; } } else { cout << "Invalid choice!" << endl; } } // 订票 void bookFlight() { string flightNo, name, id; int numTickets; cout << "Enter flight number: "; cin >> flightNo; auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) { return flight.flightNo == flightNo; }); if (it != flights.end()) { cout << "Enter name: "; cin >> name; cout << "Enter ID: "; cin >> id; cout << "Enter number of tickets: "; cin >> numTickets; if (numTickets <= it->seatsLeft) { Passenger passenger; passenger.name = name; passenger.id = id; passenger.numTickets = numTickets; passenger.flight = *it; bookings.push_back(passenger); it->seatsLeft -= numTickets; saveFlightsFile(); saveBookingsFile(); cout << "Booking successful!" << endl; } else { cout << "Not enough seats left!" << endl; } } else { cout << "Flight not found!" << endl; } } // 退票 void cancelFlight() { string name, id; cout << "Enter name: "; cin >> name; cout << "Enter ID: "; cin >> id; auto it = find_if(bookings.begin(), bookings.end(), [&](const Passenger& passenger) { return passenger.name == name && passenger.id == id; }); if (it != bookings.end()) { it->flight.seatsLeft += it->numTickets; bookings.erase(it); saveFlightsFile(); saveBookingsFile(); cout << "Cancellation successful!" << endl; } else { cout << "Booking not found!" << endl; } } // 修改航班信息 void modifyFlight() { string flightNo; cout << "Enter flight number: "; cin >> flightNo; auto it = find_if(flights.begin(), flights.end(), [&](const Flight& flight) { return flight.flightNo == flightNo; }); if (it != flights.end()) { cout << "Enter new departure time: "; cin >> it->departureTime; cout << "Enter new arrival time: "; cin >> it->arrivalTime; cout << "Enter new price: "; cin >> it->price; cout << "Enter new discount: "; cin >> it->discount; saveFlightsFile(); cout << "Modification successful!" << endl; } else { cout << "Flight not found!" << endl; } } int main() { readFlightsFile(); readBookingsFile(); while (true) { int choice; cout << "1. Add flight" << endl; cout << "2. Search flights" << endl; cout << "3. Book flight" << endl; cout << "4. Cancel flight" << endl; cout << "5. Modify flight" << endl; cout << "6. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: addFlight(); break; case 2: searchFlights(); break; case 3: bookFlight(); break; case 4: cancelFlight(); break; case 5: modifyFlight(); break; case 6: return 0; default: cout << "Invalid choice!" << endl; break; } } return 0; } ``` 输出示例: ``` 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 1 Enter flight number: CA1234 Enter from city: PEK Enter to city: SHA Enter departure time: 08:00 Enter arrival time: 10:00 Enter price: 1000 Enter discount: 0.8 Enter seats left: 100 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 2 1. Search by flight number 2. Search by city Enter your choice: 1 Enter flight number: CA1234 Flight number: CA1234 From city: PEK To city: SHA Departure time: 08:00 Arrival time: 10:00 Price: 1000 Discount: 0.8 Seats left: 100 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 3 Enter flight number: CA1234 Enter name: John Enter ID: 1234567890 Enter number of tickets: 2 Booking successful! 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 2 1. Search by flight number 2. Search by city Enter your choice: 1 Enter flight number: CA1234 Flight number: CA1234 From city: PEK To city: SHA Departure time: 08:00 Arrival time: 10:00 Price: 1000 Discount: 0.8 Seats left: 98 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 4 Enter name: John Enter ID: 1234567890 Cancellation successful! 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 5 Enter flight number: CA1234 Enter new departure time: 10:00 Enter new arrival time: 12:00 Enter new price: 800 Enter new discount: 0.9 Modification successful! 1. Add flight 2. Search flights 3. Book flight 4. Cancel flight 5. Modify flight 6. Exit Enter your choice: 6 ```

相关推荐

最新推荐

recommend-type

数据结构课程设计--》航空订票系统

可以查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓);可以输入起飞抵达城市,查询飞机航班情况; 客户资料有姓名,证件号,订票数量及航班情况,订单要...
recommend-type

机票预订系统说明书 源程序

整个系统操作以管理员登陆为前提,然后进入系统主界面进行航班的录入、航线的查询、修改航班、售票业务、退票业务等功能。航班信息的录入、航线的查询、修改航班、售票业务、退票业务。录入、查询和修改的相关信息...
recommend-type

航空订票系统 数据结构 VC++

2. 可以查询任意航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓) 3. 可以输入起飞抵达城市,查询飞机航班情况 4. 可以订票,如果该航班已经无票,可以提供相关可...
recommend-type

运动会分数统计设计报告

查询:可以查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓);可以输入起飞抵达城市,查询飞机航班情况; 订票:(订票情况可以存在一个数据文件中,结构...
recommend-type

数据结构课程设计飞机票的管理

可以查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价),可以输入起飞、抵达城市,查询飞机航班; 订票: 可以订票,如果该航班已经无票,可以提供相关可选择航班; 退票: 可退票,退票...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

解释这行代码 c = ((double)rand() / RAND_MAX) * (a + b - fabs(a - b)) + fabs(a - b);

这行代码是用于生成 a 和 b 之间的随机数。首先,它使用 rand() 函数生成一个 [0,1) 之间的随机小数,然后将这个小数乘以 a、b 范围内的差值,再加上 a 和 b 中的较小值。这可以确保生成的随机数大于等于 a,小于等于 b,而且不会因为 a 和 b 之间的差距过大而导致难以生成足够多的随机数。最后,使用 fabs() 函数来确保计算结果是正数。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。