用c++写一个民航订票系统,使用文件储存数据,不使用链表、容器,包含用户和管理两个类,实现预订机票、退票、查询航班、增加航班、删除航班、修改航班、显示航班等功能。
时间: 2023-10-20 08:05:09 浏览: 123
好的,下面是一个简单的实现,仅供参考:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX_FLIGHTS = 100; // 最大航班数
const int MAX_NAME_LEN = 20; // 最大姓名长度
const int MAX_TICKET_NUM = 5; // 最大订票数
// 航班信息结构体
struct Flight {
int flightNo; // 航班号
char departureCity[MAX_NAME_LEN]; // 出发城市
char destinationCity[MAX_NAME_LEN]; // 目的地城市
int departureTime; // 出发时间
int arrivalTime; // 到达时间
int price; // 价格
int seatNum; // 座位数
int bookedSeatNum; // 已预订座位数
};
// 用户信息结构体
struct User {
char name[MAX_NAME_LEN]; // 姓名
int flightNo[MAX_TICKET_NUM]; // 订票的航班号
int ticketNum; // 订票数
};
// 管理类
class Management {
public:
Management() {
flightNum = 0;
userNum = 0;
// 从文件中读取航班信息
ifstream infile("flights.txt");
if (infile) {
while (infile >> flights[flightNum].flightNo
>> flights[flightNum].departureCity
>> flights[flightNum].destinationCity
>> flights[flightNum].departureTime
>> flights[flightNum].arrivalTime
>> flights[flightNum].price
>> flights[flightNum].seatNum
>> flights[flightNum].bookedSeatNum) {
flightNum++;
}
infile.close();
}
// 从文件中读取用户信息
infile.open("users.txt");
if (infile) {
while (infile >> users[userNum].name) {
infile >> users[userNum].ticketNum;
for (int i = 0; i < users[userNum].ticketNum; i++) {
infile >> users[userNum].flightNo[i];
}
userNum++;
}
infile.close();
}
}
~Management() {
// 将航班信息和用户信息保存到文件
ofstream outfile("flights.txt");
if (outfile) {
for (int i = 0; i < flightNum; i++) {
outfile << flights[i].flightNo << " "
<< flights[i].departureCity << " "
<< flights[i].destinationCity << " "
<< flights[i].departureTime << " "
<< flights[i].arrivalTime << " "
<< flights[i].price << " "
<< flights[i].seatNum << " "
<< flights[i].bookedSeatNum << endl;
}
outfile.close();
}
outfile.open("users.txt");
if (outfile) {
for (int i = 0; i < userNum; i++) {
outfile << users[i].name << " " << users[i].ticketNum << " ";
for (int j = 0; j < users[i].ticketNum; j++) {
outfile << users[i].flightNo[j] << " ";
}
outfile << endl;
}
outfile.close();
}
}
void displayFlights() const {
cout << "航班号 出发城市 目的地城市 出发时间 到达时间 价格 座位数 已预订数" << endl;
for (int i = 0; i < flightNum; i++) {
cout << flights[i].flightNo << " "
<< flights[i].departureCity << " "
<< flights[i].destinationCity << " "
<< flights[i].departureTime << " "
<< flights[i].arrivalTime << " "
<< flights[i].price << " "
<< flights[i].seatNum << " "
<< flights[i].bookedSeatNum << endl;
}
}
void addFlight() {
if (flightNum >= MAX_FLIGHTS) {
cout << "航班数已达上限,无法添加!" << endl;
return;
}
Flight flight;
cout << "请输入航班号:";
cin >> flight.flightNo;
cout << "请输入出发城市:";
cin >> flight.departureCity;
cout << "请输入目的地城市:";
cin >> flight.destinationCity;
cout << "请输入出发时间(如 830 表示 8:30):";
cin >> flight.departureTime;
cout << "请输入到达时间(如 1230 表示 12:30):";
cin >> flight.arrivalTime;
cout << "请输入价格:";
cin >> flight.price;
cout << "请输入座位数:";
cin >> flight.seatNum;
flight.bookedSeatNum = 0;
flights[flightNum] = flight;
flightNum++;
cout << "添加成功!" << endl;
}
void deleteFlight() {
int flightNo;
cout << "请输入要删除的航班号:";
cin >> flightNo;
int i = 0;
for (; i < flightNum; i++) {
if (flights[i].flightNo == flightNo) {
// 将数组后面的元素向前移动
for (int j = i + 1; j < flightNum; j++) {
flights[j - 1] = flights[j];
}
flightNum--;
cout << "删除成功!" << endl;
return;
}
}
if (i == flightNum) {
cout << "未找到要删除的航班!" << endl;
}
}
void modifyFlight() {
int flightNo;
cout << "请输入要修改的航班号:";
cin >> flightNo;
for (int i = 0; i < flightNum; i++) {
if (flights[i].flightNo == flightNo) {
cout << "请输入出发城市(原来为 " << flights[i].departureCity << "):";
cin >> flights[i].departureCity;
cout << "请输入目的地城市(原来为 " << flights[i].destinationCity << "):";
cin >> flights[i].destinationCity;
cout << "请输入出发时间(原来为 " << flights[i].departureTime << "):";
cin >> flights[i].departureTime;
cout << "请输入到达时间(原来为 " << flights[i].arrivalTime << "):";
cin >> flights[i].arrivalTime;
cout << "请输入价格(原来为 " << flights[i].price << "):";
cin >> flights[i].price;
cout << "请输入座位数(原来为 " << flights[i].seatNum << "):";
cin >> flights[i].seatNum;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到要修改的航班!" << endl;
}
void bookTicket() {
char name[MAX_NAME_LEN];
int flightNo;
cout << "请输入您的姓名:";
cin >> name;
cout << "请输入要订票的航班号:";
cin >> flightNo;
int i = 0;
for (; i < flightNum; i++) {
if (flights[i].flightNo == flightNo) {
if (flights[i].bookedSeatNum >= flights[i].seatNum) {
cout << "该航班已满座,无法订票!" << endl;
return;
}
flights[i].bookedSeatNum++;
break;
}
}
if (i == flightNum) {
cout << "未找到要订票的航班!" << endl;
return;
}
i = 0;
for (; i < userNum; i++) {
if (strcmp(users[i].name, name) == 0) {
if (users[i].ticketNum >= MAX_TICKET_NUM) {
cout << "您已订满 " << MAX_TICKET_NUM << " 张票,无法再订!" << endl;
return;
}
users[i].flightNo[users[i].ticketNum] = flightNo;
users[i].ticketNum++;
break;
}
}
if (i == userNum) {
if (userNum >= MAX_FLIGHTS) {
cout << "用户数已达上限,无法订票!" << endl;
flights[i].bookedSeatNum--;
return;
}
strcpy(users[userNum].name, name);
users[userNum].flightNo[0] = flightNo;
users[userNum].ticketNum = 1;
userNum++;
}
cout << "订票成功!" << endl;
}
void cancelTicket() {
char name[MAX_NAME_LEN];
int flightNo;
cout << "请输入您的姓名:";
cin >> name;
cout << "请输入要退票的航班号:";
cin >> flightNo;
int i = 0;
for (; i < flightNum; i++) {
if (flights[i].flightNo == flightNo) {
flights[i].bookedSeatNum--;
break;
}
}
if (i == flightNum) {
cout << "未找到要退票的航班!" << endl;
return;
}
i = 0;
for (; i < userNum; i++) {
if (strcmp(users[i].name, name) == 0) {
int j = 0;
for (; j < users[i].ticketNum; j++) {
if (users[i].flightNo[j] == flightNo) {
// 将数组后面的元素向前移动
for (int k = j + 1; k < users[i].ticketNum; k++) {
users[i].flightNo[k - 1] = users[i].flightNo[k];
}
users[i].ticketNum--;
break;
}
}
if (j == users[i].ticketNum) {
cout << "您未订购该航班,无法退票!" << endl;
flights[i].bookedSeatNum++;
return;
}
break;
}
}
if (i == userNum) {
cout << "未找到您的订票信息,无法退票!" << endl;
flights[i].bookedSeatNum++;
return;
}
cout << "退票成功!" << endl;
}
private:
Flight flights[MAX_FLIGHTS]; // 航班数组
int flightNum; // 航班数
User users[MAX_FLIGHTS]; // 用户数组
int userNum; // 用户数
};
// 用户类
class UserClass {
public:
UserClass() {
management = new Management();
}
~UserClass() {
delete management;
}
void displayFlights() const {
management->displayFlights();
}
void bookTicket() {
management->bookTicket();
}
void cancelTicket() {
management->cancelTicket();
}
private:
Management *management; // 管理类指针
};
int main() {
UserClass user;
int choice;
do {
cout << "====== 民航订票系统 ======" << endl;
cout << "1. 查询航班" << endl;
cout << "2. 订票" << endl;
cout << "3. 退票" << endl;
cout << "4. 退出" << endl;
cout << "请输入您的选择:";
cin >> choice;
switch (choice) {
case 1:
user.displayFlights();
break;
case 2:
user.bookTicket();
break;
case 3:
user.cancelTicket();
break;
case 4:
break;
default:
cout << "输入有误,请重新输入!" << endl;
}
} while (choice != 4);
return 0;
}
```
注意,这个实现仅仅是一个简单的示例,还有很多需要完善的地方,例如输入校验、异常处理、界面美化等。
阅读全文