航班管理系统(数组版)
航班信息查询系统
1、 导言 用所学过的 C 语言,以及链表相关知识,实现一个航班信息的录入、查询、显示、 排序等功能。航班信息
节点设计,请参见附录。
2、 基本功能: 使用链表或数组,实现以下功能:
1) 录入(可以直接从键盘录入,也可通过文件存储)
2) 显示所有航班信息
3) 按照查询要求(比如日期、地点等),列出相关的航班信息。
4) 完善该项目功能。
5) 对录入的信息进行排序(比如按起飞时间排序、按起飞地址排序等)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define MAX_FLIGHTS 100 // 定义最大航班数量static int flight_num = 0; // 当前航班的数量typedef struct airplane
{char number[10]; // 航班号char Start_City[20]; // 起飞城市char End_City[20]; // 到达城市// char type; //机型char Start_Time[20]; // 起飞时间char End_Time[20]; // 到达时间float Price; // 机票价格float Discount; // 机票折扣int Tickets; // 机票剩余数量
} Plane;Plane flights[MAX_FLIGHTS]; // 存储航班信息的数组void Menu(); // 菜单
void addFlight(); // 添加
void findFlight(); // 查询
void displayFlights(); // 显示所有
void modifyFlight(); // 修改
void deleteFlight(); // 删除
void Sort(); // 排序/*功能: 显示菜单信息参数: 无返回值:无*/
void Menu()
{int choice;while (1){printf(" 航班管理系统 \n");printf("----------------------------\n");printf("|1、添加航班信息 |\n");printf("|2、显示所有航班信息 |\n");printf("|3、查询航班信息 |\n");printf("|4、修改航班信息 |\n");printf("|5、删除航班信息 |\n");printf("|6、排序 |\n");printf("|0、退出 |\n");printf("----------------------------\n");printf("请输入相应的序号选择! \n");scanf("%d", &choice);getchar();switch (choice){case 1:addFlight();break;case 2:displayFlights();break;case 3:findFlight();break;case 4:modifyFlight();break;case 5:deleteFlight();break;case 6:Sort();break;case 0:exit(0);break;default:printf("无效的选择,请重试!\n");}// 暂停操作printf("\n");system("pause"); // 这行代码会使程序等待用户按下任意键system("cls"); // 清屏操作}
}/*功能: 添加航班信息参数: 无返回值:无
*/
void addFlight()
{if (flight_num >= MAX_FLIGHTS){printf("无法添加更多航班,已达到最大限制!\n");return;}Plane newFlight;printf("请输入航班号: ");gets(newFlight.number);printf("请输入起飞城市: ");gets(newFlight.Start_City);printf("请输入到达城市: ");gets(newFlight.End_City);printf("请输入起飞时间: ");gets(newFlight.Start_Time);printf("请输入到达时间: ");gets(newFlight.End_Time);printf("请输入机票价格: ");scanf("%f", &newFlight.Price);getchar();printf("请输入机票折扣: ");scanf("%f", &newFlight.Discount);getchar();printf("请输入剩余票数: ");scanf("%d", &newFlight.Tickets);getchar();flights[flight_num++] = newFlight; // 将新航班添加到数组中printf("航班添加成功!\n");
}/*功能: 显示所有航班信息参数: 无返回值:无
*/
void displayFlights()
{if (flight_num == 0){ // 检查是否有航班信息printf("没有可显示的航班信息!\n");return;}printf("航班号\t\t起飞城市\t到达城市\t起飞时间\t到达时间\t机票价格\t优惠折扣\t剩余票数\n");for (int i = 0; i < flight_num; i++){printf("%s\t\t%s\t-->\t%s\t\t%s\t\t%s\t\t%.2f\t\t%.2f\t\t%d\n",flights[i].number, flights[i].Start_City, flights[i].End_City,flights[i].Start_Time, flights[i].End_Time,flights[i].Price, flights[i].Discount, flights[i].Tickets);}
}/*功能: 查询航班信息参数: 无返回值:无
*/
void findFlight()
{if (flight_num == 0){ // 检查是否有航班信息printf("没有可查询的航班信息!\n");return;}char fnum[10];printf("请输入要查询的航班号:");fgets(fnum, sizeof(fnum), stdin);fnum[strcspn(fnum, "\n")] = '\0'; // 去除换行符for (int i = 0; i < flight_num; i++){if (strcmp(fnum, flights[i].number) == 0){printf("航班号\t\t起飞城市\t到达城市\t起飞时间\t到达时间\t机票价格\t优惠折扣\t剩余票数\n");printf("%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%.2f\t\t%.2f\t\t%d\n",flights[i].number, flights[i].Start_City, flights[i].End_City,flights[i].Start_Time, flights[i].End_Time,flights[i].Price, flights[i].Discount, flights[i].Tickets);return;}}printf("未找到该航班!\n");
}/*功能: 修改航班信息参数: 无返回值:无
*/
void modifyFlight()
{if (flight_num == 0){ // 检查是否有航班信息printf("没有修改的航班信息!\n");return;}printf("请输入要修改的航班号:");char fnum[10];int flag = 0;fgets(fnum, 10, stdin);fnum[strcspn(fnum, "\n")] = 0; // 去掉换行符for (int i = 0; i < flight_num; i++){if (strcmp(fnum, flights[i].number) == 0){flag = 1;printf("修改开始\n请输入航班号: ");gets(flights[i].number);printf("请输入起飞城市: ");gets(flights[i].Start_City);printf("请输入到达城市: ");gets(flights[i].End_City);printf("请输入起飞时间: ");gets(flights[i].Start_Time);printf("请输入到达时间: ");gets(flights[i].End_Time);printf("请输入机票价格: ");scanf("%f", &flights[i].Price);getchar();printf("请输入机票折扣: ");scanf("%f", &flights[i].Discount);getchar();printf("请输入剩余票数: ");scanf("%d", &flights[i].Tickets);getchar();printf("航班修改成功!\n");break;}}if (flag == 0){printf("未找到该航班号对应的航班信息!\n");}
}/*功能: 删除航班信息参数: 无返回值:无
*/void deleteFlight()
{if (flight_num == 0){ // 检查是否有航班信息printf("没有可删除的航班信息!\n");return;}printf("请输入要删除的航班号:");char fnum[10];int flag = 0;fgets(fnum, sizeof(fnum), stdin);fnum[strcspn(fnum, "\n")] = 0;for (int i = 0; i < flight_num; i++){if (strcmp(fnum, flights[i].number) == 0){flag = 1;for (int j = i; j < flight_num - 1; j++){strcpy(flights[j].number, flights[j + 1].number);strcpy(flights[j].Start_City, flights[j + 1].Start_City);strcpy(flights[j].End_City, flights[j + 1].Start_Time);strcpy(flights[j].Start_Time, flights[j + 1].Start_Time);strcpy(flights[j].End_Time, flights[j + 1].End_Time);flights[j].Price = flights[j + 1].Price;flights[j].Discount = flights[j + 1].Discount;flights[j].Tickets = flights[j + 1].Tickets;}flight_num--;printf("删除成功!");break;}}if (!flag)printf("未找到该航班号对应的航班信息!\n");
}/*功能: 删除航班信息参数: 无返回值:无
*/
void Sort()
{if (flight_num == 0){ // 检查是否有航班信息printf("没有可排序的航班信息!\n");return;}printf("请选择排序方式:\n");printf("1.按日期升序排序\n2.按日期降序排序\n3.按价格升序排序\n4.按价格降序排序\n");int num;scanf("%d", &num);if (num == 1 || num == 2) // 1.按日期升序排序 2.按日期降序排序{for (int i = 0; i < flight_num - 1; i++){for (int j = 0; j < flight_num - i - 1; j++){if ((num == 1 && strcmp(flights[j].Start_Time, flights[j + 1].Start_Time) > 0) ||(num == 2 && strcmp(flights[j].Start_Time, flights[j + 1].Start_Time) < 0)){Plane temp = flights[j];flights[j] = flights[j + 1];flights[j + 1] = temp;}}}printf("日期排序成功!\n");}else if (num == 3 || num == 4) // 3.按价格升序排序 4.按价格降序排序{for (int i = 0; i < flight_num - 1; i++){for (int j = 0; j < flight_num - i - 1; j++){if ((num == 3 && (flights[j].Price - flights[j + 1].Price) > 0) ||(num == 4 && (flights[j].Price - flights[j + 1].Price) < 0)){Plane temp = flights[j];flights[j] = flights[j + 1];flights[j + 1] = temp;}}}printf("价格排序成功!\n");}printf("航班号\t\t起飞城市\t到达城市\t起飞时间\t到达时间\t机票价格\t优惠折扣\t剩余票数\n");for (int i = 0; i < flight_num; i++){printf("%s\t\t%s\t-->\t%s\t\t%s\t\t%s\t\t%.2f\t\t%.2f\t\t%d\n",flights[i].number, flights[i].Start_City, flights[i].End_City,flights[i].Start_Time, flights[i].End_Time,flights[i].Price, flights[i].Discount, flights[i].Tickets);}
}int main(int argc, char const *argv[])
{Menu();return 0;
}