请用C++实现:1. 订票系统 任务:请根据以下要求,设计航班信息、订票信息的存储结构,设计程序完成订票系统的相关功能。 要求: (1)录入:可以录入航班情况,数据可以存储在一个数据文件中,数据结构自定; (2)查询:可以查询某个航线的情况(如,输入航班号,查询起降时间,起飞抵达城市,航班票价,票价折扣,确定航班是否满仓);可以输入起飞抵达城市,查询飞机航班情况; (3)订票:(订票情况可以存在一个数据文件中,结构自己设定)可以订票,如果该航班已经无票,可以提供相关可选择航班; (4)退票: 可退票,退票后修改相关数据文件;客户资料有姓名,证件号,订票数量及航班情况,订单要有编号; (5)修改航班信息:当航班信息改变可以修改航班数据文件。
时间: 2024-03-13 14:44:07 浏览: 111
好的,我会为您解答。以下是一个简单的订票系统的C++实现,包括录入航班信息、查询、订票、退票和修改航班信息等功能。
```
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Flight {
char flight_no[10]; // 航班号
char departure_time[20]; // 起飞时间
char arrival_time[20]; // 抵达时间
char departure_city[20]; // 起飞城市
char arrival_city[20]; // 抵达城市
double price; // 票价
double discount; // 折扣
int seats; // 座位数
};
struct Order {
int id; // 订单编号
char name[20]; // 姓名
char id_no[20]; // 身份证号
char flight_no[10]; // 航班号
int num; // 订票数量
};
void save_flight(Flight f) {
ofstream out("flights.dat", ios::app | ios::binary);
out.write((char*)&f, sizeof(f));
out.close();
}
void read_flights() {
ifstream in("flights.dat", ios::binary);
Flight f;
while (in.read((char*)&f, sizeof(f))) {
cout << "航班号:" << f.flight_no << endl;
cout << "起飞时间:" << f.departure_time << endl;
cout << "抵达时间:" << f.arrival_time << endl;
cout << "起飞城市:" << f.departure_city << endl;
cout << "抵达城市:" << f.arrival_city << endl;
cout << "票价:" << f.price << endl;
cout << "折扣:" << f.discount << endl;
cout << "座位数:" << f.seats << endl << endl;
}
in.close();
}
void query_flight(char flight_no[10]) {
ifstream in("flights.dat", ios::binary);
Flight f;
bool found = false;
while (in.read((char*)&f, sizeof(f))) {
if (strcmp(f.flight_no, flight_no) == 0) {
cout << "航班号:" << f.flight_no << endl;
cout << "起飞时间:" << f.departure_time << endl;
cout << "抵达时间:" << f.arrival_time << endl;
cout << "起飞城市:" << f.departure_city << endl;
cout << "抵达城市:" << f.arrival_city << endl;
cout << "票价:" << f.price << endl;
cout << "折扣:" << f.discount << endl;
cout << "座位数:" << f.seats << endl << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该航班信息!" << endl;
}
in.close();
}
void query_flight(char departure_city[20], char arrival_city[20]) {
ifstream in("flights.dat", ios::binary);
Flight f;
bool found = false;
while (in.read((char*)&f, sizeof(f))) {
if (strcmp(f.departure_city, departure_city) == 0 && strcmp(f.arrival_city, arrival_city) == 0) {
cout << "航班号:" << f.flight_no << endl;
cout << "起飞时间:" << f.departure_time << endl;
cout << "抵达时间:" << f.arrival_time << endl;
cout << "起飞城市:" << f.departure_city << endl;
cout << "抵达城市:" << f.arrival_city << endl;
cout << "票价:" << f.price << endl;
cout << "折扣:" << f.discount << endl;
cout << "座位数:" << f.seats << endl << endl;
found = true;
}
}
if (!found) {
cout << "未找到符合条件的航班信息!" << endl;
}
in.close();
}
void book_ticket(int id, char name[20], char id_no[20], char flight_no[10], int num) {
ifstream in("flights.dat", ios::binary);
Flight f;
bool found = false;
while (in.read((char*)&f, sizeof(f))) {
if (strcmp(f.flight_no, flight_no) == 0) {
if (f.seats < num) {
cout << "该航班已满仓,请选择其他航班!" << endl;
} else {
f.seats -= num;
save_flight(f);
Order o;
o.id = id;
strcpy(o.name, name);
strcpy(o.id_no, id_no);
strcpy(o.flight_no, flight_no);
o.num = num;
ofstream out("orders.dat", ios::app | ios::binary);
out.write((char*)&o, sizeof(o));
out.close();
cout << "订票成功!" << endl;
}
found = true;
break;
}
}
if (!found) {
cout << "未找到该航班信息!" << endl;
}
in.close();
}
void cancel_ticket(int id) {
ifstream in("orders.dat", ios::binary);
Order o;
bool found = false;
while (in.read((char*)&o, sizeof(o))) {
if (o.id == id) {
ifstream in2("flights.dat", ios::binary);
Flight f;
while (in2.read((char*)&f, sizeof(f))) {
if (strcmp(f.flight_no, o.flight_no) == 0) {
f.seats += o.num;
save_flight(f);
break;
}
}
in2.close();
ofstream out("temp.dat", ios::app | ios::binary);
Order o2;
while (in.read((char*)&o2, sizeof(o2))) {
if (o2.id != id) {
out.write((char*)&o2, sizeof(o2));
}
}
out.close();
remove("orders.dat");
rename("temp.dat", "orders.dat");
cout << "退票成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到该订单信息!" << endl;
}
in.close();
}
void modify_flight(char flight_no[10], Flight new_f) {
ifstream in("flights.dat", ios::binary);
Flight f;
bool found = false;
while (in.read((char*)&f, sizeof(f))) {
if (strcmp(f.flight_no, flight_no) == 0) {
ofstream out("temp.dat", ios::app | ios::binary);
out.write((char*)&new_f, sizeof(new_f));
found = true;
} else {
ofstream out("temp.dat", ios::app | ios::binary);
out.write((char*)&f, sizeof(f));
}
}
if (!found) {
cout << "未找到该航班信息!" << endl;
} else {
remove("flights.dat");
rename("temp.dat", "flights.dat");
cout << "修改航班信息成功!" << endl;
}
in.close();
}
int main() {
int choice;
cout << "欢迎使用订票系统!" << endl;
do {
cout << "请选择功能:" << endl;
cout << "1. 录入航班信息" << endl;
cout << "2. 查询航班信息" << endl;
cout << "3. 订票" << endl;
cout << "4. 退票" << endl;
cout << "5. 修改航班信息" << endl;
cout << "0. 退出系统" << endl;
cout << "选择:";
cin >> choice;
switch (choice) {
case 1: {
Flight f;
cout << "请输入航班号:";
cin >> f.flight_no;
cout << "请输入起飞时间:";
cin >> f.departure_time;
cout << "请输入抵达时间:";
cin >> f.arrival_time;
cout << "请输入起飞城市:";
cin >> f.departure_city;
cout << "请输入抵达城市:";
cin >> f.arrival_city;
cout << "请输入票价:";
cin >> f.price;
cout << "请输入折扣:";
cin >> f.discount;
cout << "请输入座位数:";
cin >> f.seats;
save_flight(f);
cout << "录入成功!" << endl;
break;
}
case 2: {
int query_choice;
cout << "请选择查询方式:" << endl;
cout << "1. 查询航班号" << endl;
cout << "2. 查询起降城市" << endl;
cout << "选择:";
cin >> query_choice;
if (query_choice == 1) {
char flight_no[10];
cout << "请输入航班号:";
cin >> flight_no;
query_flight(flight_no);
} else if (query_choice == 2) {
char departure_city[20], arrival_city[20];
cout << "请输入起飞城市:";
cin >> departure_city;
cout << "请输入抵达城市:";
cin >> arrival_city;
query_flight(departure_city, arrival_city);
}
break;
}
case 3: {
int id, num;
char name[20], id_no[20], flight_no[10];
cout << "请输入订单编号:";
cin >> id;
cout << "请输入姓名:";
cin >> name;
cout << "请输入身份证号:";
cin >> id_no;
cout << "请输入航班号:";
cin >> flight_no;
cout << "请输入订票数量:";
cin >> num;
book_ticket(id, name, id_no, flight_no, num);
break;
}
case 4: {
int id;
cout << "请输入订单编号:";
cin >> id;
cancel_ticket(id);
break;
}
case 5: {
char flight_no[10];
cout << "请输入要修改的航班号:";
cin >> flight_no;
Flight new_f;
cout << "请输入起飞时间:";
cin >> new_f.departure_time;
cout << "请输入抵达时间:";
cin >> new_f.arrival_time;
cout << "请输入起飞城市:";
cin >> new_f.departure_city;
cout << "请输入抵达城市:";
cin >> new_f.arrival_city;
cout << "请输入票价:";
cin >> new_f.price;
cout << "请输入折扣:";
cin >> new_f.discount;
cout << "请输入座位数:";
cin >> new_f.seats;
modify_flight(flight_no, new_f);
break;
}
case 0: {
cout << "谢谢使用!" << endl;
break;
}
default: {
cout << "输入错误,请重新输入!" << endl;
break;
}
}
} while (choice != 0);
return 0;
}
```
以上代码实现了一个简单的订票系统,包括录入航班信息、查询、订票、退票和修改航班信息等功能。其中航班信息和订单信息存储在二进制文件中。
阅读全文