for (const Flight& flight : flights)怎么更改可以不用C++11
时间: 2023-12-13 08:04:14 浏览: 82
你可以使用传统的循环来替代范围-based for循环,如下所示:
```cpp
for (std::vector<Flight>::const_iterator it = flights.begin(); it != flights.end(); ++it) {
const Flight& flight = *it;
// 在这里进行你的操作
}
```
这里使用了一个迭代器 `it` 来遍历 `flights` 向量中的每个元素,并将其赋值给 `flight`。你可以在循环体内进行你的操作,就像在范围-based for循环中一样。要注意的是,`const_iterator` 用于确保不会更改 `flights` 中的元素。
相关问题
通过此系统可以实现如下功能:1)录入:录入航班情况;2)查询:查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓);可以输入起飞抵达城市,查询飞机航班情况;3)订票:可以订票,如果该航班已经无票,可以提供相关可选择航班;4)退票:可退票,退票后修改相关数据文件;客户资料有姓名,证件号,订票数量及航班情况,订单要有编号;5)修改航班信息:当航班信息改变可以修改航班数据文件。用c++编写,标明注并提供输出范例
好的,这是一个航班信息管理系统,以下是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
```
航空客运订票系统c++
航空客运订票系统的设计与实现是一个可以实现航空客运订票的系统。它包括了浏览和查询航班信息、机票预订和办理退票等主要业务活动。以下是一个使用C++实现的简单示例:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Flight {
string start;
string destination;
string number;
string airplaneNumber;
string flightDate;
int totalSeats;
int availableSeats;
int firstClassSeats;
int businessClassSeats;
int economyClassSeats;
double firstClassPrice;
double businessClassPrice;
double economyClassPrice;
};
vector<Flight> flights;
void addFlight() {
Flight flight;
cout << "Enter start: ";
cin >> flight.start;
cout << "Enter destination: ";
cin >> flight.destination;
cout << "Enter number: ";
cin >> flight.number;
cout << "Enter airplane number: ";
cin >> flight.airplaneNumber;
cout << "Enter flight date: ";
cin >> flight.flightDate;
cout << "Enter total seats: ";
cin >> flight.totalSeats;
cout << "Enter available seats: ";
cin >> flight.availableSeats;
cout << "Enter first class seats: ";
cin >> flight.firstClassSeats;
cout << "Enter business class seats: ";
cin >> flight.businessClassSeats;
cout << "Enter economy class seats: ";
cin >> flight.economyClassSeats;
cout << "Enter first class price: ";
cin >> flight.firstClassPrice;
cout << "Enter business class price: ";
cin >> flight.businessClassPrice;
cout << "Enter economy class price: ";
cin >> flight.economyClassPrice;
flights.push_back(flight);
cout << "Flight added successfully!" << endl;
}
void deleteFlight() {
string number;
cout << "Enter flight number to delete: ";
cin >> number;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].number == number) {
flights.erase(flights.begin() + i);
cout << "Flight deleted successfully!" << endl;
return;
}
}
cout << "Flight not found!" << endl;
}
void displayFlights() {
for (const Flight& flight : flights) {
cout << "Start: " << flight.start << endl;
cout << "Destination: " << flight.destination << endl;
cout << "Number: " << flight.number << endl;
cout << "Airplane Number: " << flight.airplaneNumber << endl;
cout << "Flight Date: " << flight.flightDate << endl;
cout << "Total Seats: " << flight.totalSeats << endl;
cout << "Available Seats: " << flight.availableSeats << endl;
cout << "First Class Seats: " << flight.firstClassSeats << endl;
cout << "Business Class Seats: " << flight.businessClassSeats << endl;
cout << "Economy Class Seats: " << flight.economyClassSeats << endl;
cout << "First Class Price: " << flight.firstClassPrice << endl;
cout << "Business Class Price: " << flight.businessClassPrice << endl;
cout << "Economy Class Price: " << flight.economyClassPrice << endl;
cout << endl;
}
}
int main() {
int choice;
do {
cout << "1. Add Flight" << endl;
cout << "2. Delete Flight" << endl;
cout << "3. Display Flights" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addFlight();
break;
case 2:
deleteFlight();
break;
case 3:
displayFlights();
break;
case 0:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice!" << endl;
break;
}
cout << endl;
} while (choice != 0);
return 0;
}
```
这个示例实现了航空客运订票系统的基本功能,包括添加航班、删除航班和显示航班信息。你可以根据自己的需求进行扩展和修改。
阅读全文