设计并实现一个基本航班管理系统:该系统应具备以下功能:
1.添加航班:输入航班号、出发地、目的地、起飞时间、到达时间、座位总数和票价。
2.删除航班:根据航班号删除航班信息3.修改航班信息:根据航班号修改航班的起飞时间、到达时间或其他信息。
4.查询航班:根据航班号查询航班的详细信息
5.预订座位:为乘客预订座位,记录乘客信息和座位号。
6.退订座位:根据乘客信息或座位号取消座位预订。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 航班信息
typedef struct Flight {
char flightNumber[10]; // 航班号
char departure[20]; // 出发地
char destination[20]; // 目的地
char departureTime[20]; // 起飞时间
char arrivalTime[20]; // 到达时间
int seatTotal; // 座位总数
float price; // 票价
} Flight;
// 乘客座位预订信息
typedef struct Booking {
char passengerName[50]; // 乘客姓名
int seatNumber; // 座位号
} Booking;
// 单链表节点
typedef struct Node {
Flight flight;
Booking *bookings; // 指向乘客座位预订信息的数组
int bookedSeats; // 已预订的座位数
struct Node *next;
} Node;
Node *head = NULL; // 单链表头指针
// 函数声明
void addFlight();
void deleteFlight(char *flightNumber);
void modifyFlight(char *flightNumber);
void searchFlight(char *flightNumber);
void bookSeat(char *flightNumber, char *passengerName, int seatNumber);
void cancelBooking(char *flightNumber, char *passengerName, int seatNumber);
void printList();
void initializeFlights();
void printAllFlights();
// 设定已有5个航班信息
void initializeFlights() {
Flight flights[5] = {
{"C1", "北京", "上海", "09:00", "11:00", 100, 800.0},
{"C2", "北京", "广州", "10:00", "13:00", 100, 1200.0},
{"C3", "上海", "北京", "14:00", "16:00", 120, 800.0},
{"C4", "广州", "大理", "13:00", "18:00", 110, 900.0},
{"C5", "昆明", "天津", "11:00", "16:00", 150, 1100.0}
};
for (int i = 0; i < 5; i++) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->flight = flights[i];
newNode->bookedSeats = 0;
newNode->bookings = (Booking *)malloc(flights[i].seatTotal * sizeof(Booking));
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
Node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
}
int main() {
int choice;
char flightNumber[10], passengerName[50];
int seatNumber;
initializeFlights();
while (1) {
printf("\n航班信息管理系统\n");
printf("1. 添加航班\n");
printf("2. 删除航班\n");
printf("3. 修改航班信息\n");
printf("4. 查询航班\n");
printf("5. 预订座位\n");
printf("6. 退订座位\n");
printf("7. 打印所有航班信息\n");
printf("8. 退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addFlight();
break;
case 2:
printf("请输入要删除的航班号: ");
scanf("%s", flightNumber);
deleteFlight(flightNumber);
break;
case 3:
printf("请输入要修改的航班号: ");
scanf("%s", flightNumber);
modifyFlight(flightNumber);
break;
case 4:
printf("请输入要查询的航班号: ");
scanf("%s", flightNumber);
searchFlight(flightNumber);
break;
case 5:
printf("请输入航班号: ");
scanf("%s", flightNumber);
printf("请输入乘客姓名: ");
scanf("%s", passengerName);
printf("请输入座位号: ");
scanf("%d", &seatNumber);
bookSeat(flightNumber, passengerName, seatNumber);
break;
case 6:
printf("请输入航班号: ");
scanf("%s", flightNumber);
printf("请输入乘客姓名: ");
scanf("%s", passengerName);
printf("请输入座位号: ");
scanf("%d", &seatNumber);
cancelBooking(flightNumber, passengerName, seatNumber);
break;
case 7:
printAllFlights();
break;
case 8:
return 0;
default:
printf("无效的选择,请重新选择!\n");
}
}
return 0;
}
// 添加航班
void addFlight() {
Flight newFlight;
printf("请输入航班号: ");
scanf("%s", newFlight.flightNumber);
printf("请输入出发地: ");
scanf("%s", newFlight.departure);
printf("请输入目的地: ");
scanf("%s", newFlight.destination);
printf("请输入起飞时间: ");
scanf("%s", newFlight.departureTime);
printf("请输入到达时间: ");
scanf("%s", newFlight.arrivalTime);
printf("请输入座位总数: ");
scanf("%d", &newFlight.seatTotal);
printf("请输入票价: ");
scanf("%f", &newFlight.price);
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->flight = newFlight;
newNode->bookedSeats = 0;
newNode->bookings = (Booking *)malloc(newFlight.seatTotal * sizeof(Booking));
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
Node *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// 删除航班
void deleteFlight(char *flightNumber) {
Node *current = head;
Node *previous = NULL;
while (current != NULL && strcmp(current->flight.flightNumber, flightNumber) != 0) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("没有找到该航班\n");
} else if (previous == NULL) {
head = current->next;
free(current->bookings); // 释放乘客座位预订信息内存
free(current);
} else {
previous->next = current->next;
free(current->bookings); // 释放乘客座位预订信息内存
free(current);
}
}
// 修改航班信息
void modifyFlight(char *flightNumber) {
Node *temp = head;
while (temp != NULL && strcmp(temp->flight.flightNumber, flightNumber) != 0) {
temp = temp->next;
}
if (temp != NULL) {
char input[200]; // 用于临时存储输入
char newDepartureTime[20], newArrivalTime[20];
float newPrice;
printf("请输入新的起飞时间: ");
scanf("%200s", input); // 限制输入长度以防止缓冲区溢出
if (strlen(input) > 0) {
strcpy(newDepartureTime, input);
strcpy(temp->flight.departureTime, newDepartureTime);
}
printf("请输入新的到达时间: ");
scanf("%200s", input);
if (strlen(input) > 0) {
strcpy(newArrivalTime, input);
strcpy(temp->flight.arrivalTime, newArrivalTime);
}
printf("请输入新的票价: ");
scanf("%200s", input);
if (strlen(input) > 0 && sscanf(input, "%f", &newPrice) == 1) {
temp->flight.price = newPrice;
}
printf("航班信息已成功修改。\n");
} else {
printf("未找到该航班。\n");
}
}
// 查询航班
void searchFlight(char *flightNumber) {
Node *temp = head;
while (temp != NULL && strcmp(temp->flight.flightNumber, flightNumber) != 0) {
temp = temp->next;
}
if (temp != NULL) {
printf("航班号: %s 出发地: %s 目的地: %s 起飞时间: %s 到达时间: %s 座位总数: %d 票价: %.2f\n",
temp->flight.flightNumber, temp->flight.departure, temp->flight.destination,
temp->flight.departureTime, temp->flight.arrivalTime, temp->flight.seatTotal, temp->flight.price);
} else {
printf("未找到该航班。\n");
}
}
// 预订座位
void bookSeat(char *flightNumber, char *passengerName, int seatNumber) {
Node *temp = head;
while (temp != NULL && strcmp(temp->flight.flightNumber, flightNumber) != 0) {
temp = temp->next;
}
if (temp != NULL && seatNumber > 0 && seatNumber <= temp->flight.seatTotal && temp->bookedSeats < temp->flight.seatTotal) {
for (int i = 0; i < temp->flight.seatTotal; i++) {
if (temp->bookings[i].seatNumber == seatNumber) {
printf("该座位已被预订。\n");
return;
}
}
temp->bookings[temp->bookedSeats].seatNumber = seatNumber;
strcpy(temp->bookings[temp->bookedSeats].passengerName, passengerName);
temp->bookedSeats++;
printf("座位预订成功。\n");
} else {
printf("无法预订该座位。\n");
}
}
// 退订座位
void cancelBooking(char *flightNumber, char *passengerName, int seatNumber) {
Node *temp = head;
while (temp != NULL && strcmp(temp->flight.flightNumber, flightNumber) != 0) {
temp = temp->next;
}
if (temp != NULL) {
for (int i = 0; i < temp->bookedSeats; i++) {
if (temp->bookings[i].seatNumber == seatNumber && strcmp(temp->bookings[i].passengerName, passengerName) == 0) {
for (int j = i; j < temp->bookedSeats - 1; j++) {
temp->bookings[j] = temp->bookings[j + 1];
}
temp->bookedSeats--;
printf("座位退订成功。\n");
return;
}
}
printf("未找到对应的座位预订。\n");
} else {
printf("未找到该航班。\n");
}
}
// 打印单链表中的航班信息
void printList() {
Node *temp = head;
while (temp != NULL) {
printf("航班号: %s 出发地: %s 目的地: %s 起飞时间: %s 到达时间: %s 座位总数: %d 票价: %.2f\n",
temp->flight.flightNumber, temp->flight.departure, temp->flight.destination,
temp->flight.departureTime, temp->flight.arrivalTime, temp->flight.seatTotal, temp->flight.price);
temp = temp->next;
}
}
// 打印所有航班信息
void printAllFlights() {
Node *temp = head;
if (temp == NULL) {
printf("当前没有航班信息。\n");
return;
}
printf("\n所有航班信息:\n");
printf("航班号\t出发地\t目的地\t起飞时间\t到达时间\t座位总数\t票价\n");
while (temp != NULL) {
printf("%s\t%s\t%s\t%s\t%s\t%d\t%.2f\n",
temp->flight.flightNumber, temp->flight.departure, temp->flight.destination,
temp->flight.departureTime, temp->flight.arrivalTime, temp->flight.seatTotal, temp->flight.price);
temp = temp->next;
}
}