用c语言实现显示航班管理系统中的乘客信息座位号
时间: 2024-03-22 07:37:31 浏览: 132
好的,要实现显示航班管理系统中的乘客信息座位号,可以按照以下步骤:
1. 在程序中定义一个结构体,包含乘客的姓名、座位号等信息,例如:
```
struct passenger {
char name[20];
int seat_num;
// 其他信息
};
```
2. 在程序中定义一个数组,用来存储所有乘客的信息,例如:
```
struct passenger passengers[100];
```
这里假设最多有100个乘客。
3. 在程序中读取乘客的信息,例如:
```
for (int i = 0; i < 100; i++) {
printf("请输入第%d个乘客的姓名和座位号:", i + 1);
scanf("%s %d", passengers[i].name, &passengers[i].seat_num);
// 其他信息的读取
}
```
这里假设乘客的姓名不超过20个字符。
4. 在程序中显示乘客的信息和座位号,例如:
```
for (int i = 0; i < 100; i++) {
printf("第%d个乘客的姓名是:%s,座位号是:%d\n", i + 1, passengers[i].name, passengers[i].seat_num);
// 其他信息的显示
}
```
这样就可以实现显示航班管理系统中的乘客信息和座位号了。当然,实际的航班管理系统可能会更加复杂,需要根据具体的需求来进行设计和实现。
相关问题
用c语言实现铁路售票管理系统的以下功能:1、不同车次信息的添加、删除、修改等功能。2、所有车次余票显示。3、查询车次、余票、票价等功能。4选择座位、填写乘客信息、预订车票等功能。
以下是用C语言实现铁路售票管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TRAIN_NUM 10
#define MAX_SEATS_NUM 100
typedef struct {
char train_num[20]; //车次
int seats[MAX_SEATS_NUM]; //余票
float price; //票价
} train;
train trains[MAX_TRAIN_NUM]; //所有车次信息
int train_count = 0; //当前车次数
/* 添加车次信息 */
void add_train() {
if (train_count >= MAX_TRAIN_NUM) {
printf("已达到最大车次数,无法添加新车次!\n");
return;
}
//输入车次信息
train new_train;
printf("请输入新车次信息:\n");
printf("车次号:");
scanf("%s", new_train.train_num);
printf("票价:");
scanf("%f", &new_train.price);
for (int i = 0; i < MAX_SEATS_NUM; i++) {
new_train.seats[i] = 50; //初始化余票为50
}
//将新车次信息添加到所有车次信息中
trains[train_count] = new_train;
train_count++;
printf("添加车次信息成功!\n");
}
/* 删除车次信息 */
void delete_train() {
char train_num[20];
printf("请输入要删除的车次号:");
scanf("%s", train_num);
int i;
for (i = 0; i < train_count; i++) {
if (strcmp(trains[i].train_num, train_num) == 0) {
//将要删除的车次信息从所有车次信息中删除
for (int j = i; j < train_count - 1; j++) {
trains[j] = trains[j+1];
}
train_count--;
printf("删除车次信息成功!\n");
break;
}
}
if (i >= train_count) {
printf("未找到要删除的车次!\n");
}
}
/* 修改车次信息 */
void modify_train() {
char train_num[20];
printf("请输入要修改的车次号:");
scanf("%s", train_num);
int i;
for (i = 0; i < train_count; i++) {
if (strcmp(trains[i].train_num, train_num) == 0) {
//输入新的车次信息
train new_train;
printf("请输入新车次信息:\n");
printf("车次号:");
scanf("%s", new_train.train_num);
printf("票价:");
scanf("%f", &new_train.price);
memcpy(new_train.seats, trains[i].seats, sizeof(trains[i].seats)); //将余票数复制
//将新车次信息替换现有车次信息
trains[i] = new_train;
printf("修改车次信息成功!\n");
break;
}
}
if (i >= train_count) {
printf("未找到要修改的车次!\n");
}
}
/* 显示所有车次余票 */
void show_all_seats() {
printf("所有车次余票数如下:\n");
for (int i = 0; i < train_count; i++) {
printf("车次号:%s,余票数:", trains[i].train_num);
int count = 0;
for (int j = 0; j < MAX_SEATS_NUM; j++) {
if (trains[i].seats[j] > 0) {
count++;
}
}
printf("%d\n", count);
}
}
/* 查询车次信息 */
void search_train() {
char train_num[20];
printf("请输入要查询的车次号:");
scanf("%s", train_num);
int i;
for (i = 0; i < train_count; i++) {
if (strcmp(trains[i].train_num, train_num) == 0) {
printf("车次号:%s,票价:%0.2f,余票数:", trains[i].train_num, trains[i].price);
int count = 0;
for (int j = 0; j < MAX_SEATS_NUM; j++) {
if (trains[i].seats[j] > 0) {
count++;
}
}
printf("%d\n", count);
break;
}
}
if (i >= train_count) {
printf("未找到要查询的车次!\n");
}
}
/* 选择座位、填写乘客信息、预订车票 */
void book_ticket() {
char train_num[20];
printf("请输入要预订的车次号:");
scanf("%s", train_num);
int i;
for (i = 0; i < train_count; i++) {
if (strcmp(trains[i].train_num, train_num) == 0) {
printf("车次号:%s,票价:%0.2f,余票数:", trains[i].train_num, trains[i].price);
int count = 0;
for (int j = 0; j < MAX_SEATS_NUM; j++) {
if (trains[i].seats[j] > 0) {
count++;
}
}
printf("%d\n", count);
if (count <= 0) {
printf("该车次已无余票,无法预订!\n");
break;
}
//选择座位
int seat_num;
printf("请选择座位号(1-%d):", count);
scanf("%d", &seat_num);
int available_seats = 0, index = -1;
for (int j = 0; j < MAX_SEATS_NUM; j++) {
if (trains[i].seats[j] > 0) {
available_seats++;
}
if (available_seats == seat_num) {
index = j;
break;
}
}
//填写乘客信息
char name[20];
int age;
printf("请填写乘客信息:\n");
printf("姓名:");
scanf("%s", name);
printf("年龄:");
scanf("%d", &age);
//预订车票
trains[i].seats[index]--;
printf("预订车票成功!\n");
printf("车次号:%s,座位号:%d,票价:%0.2f,姓名:%s,年龄:%d\n", trains[i].train_num, index+1, trains[i].price, name, age);
break;
}
}
if (i >= train_count) {
printf("未找到要预订的车次!\n");
}
}
int main() {
int choice = -1;
while (choice != 0) {
printf("欢迎使用铁路售票管理系统!\n");
printf("1. 添加车次信息\n");
printf("2. 删除车次信息\n");
printf("3. 修改车次信息\n");
printf("4. 显示所有车次余票\n");
printf("5. 查询车次信息\n");
printf("6. 选择座位、填写乘客信息、预订车票\n");
printf("0. 退出系统\n");
printf("请选择操作(0-6):");
scanf("%d", &choice);
switch (choice) {
case 1:
add_train();
break;
case 2:
delete_train();
break;
case 3:
modify_train();
break;
case 4:
show_all_seats();
break;
case 5:
search_train();
break;
case 6:
book_ticket();
break;
case 0:
printf("谢谢使用!\n");
break;
default:
printf("无效的操作!\n");
break;
}
}
return 0;
}
```
该系统实现了不同车次信息的添加、删除、修改等功能;所有车次余票显示;查询车次、余票、票价等功能;选择座位、填写乘客信息、预订车票等功能。
用C语言来实现:将所有数据以文件保存,用子函数实现各个子功能 (1) 航班的增加与删除功能。 (2) 航班查询功能:按航班号来查询所有航班信息,根据航班号查询订票乘客信息等列出该航班已订票的乘客名单。 (3) 航班订票功能:根据航班号为客户订票。 (4) 航班退票功能。 (5)显示功能:输入查询所有消息,即可显示出所有航班信息(航班号、出发地、目的地、乘客姓名、身份证号码、订购座位、航班余票),并统计出有多少条记录并且显示出来(所有航班的信息一起统计)
以下是用C语言实现的航班管理系统:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义航班结构体
struct Flight {
char flight_no[10]; // 航班号
char from[20]; // 出发地
char to[20]; // 目的地
int seats; // 座位数
int booked_seats; // 已订票座位数
char **passengers; // 乘客姓名数组
char **ids; // 乘客身份证号数组
};
// 定义全局变量
int flight_count = 0; // 航班总数
struct Flight *flights[100]; // 航班数组
// 函数声明
void add_flight();
void delete_flight();
void query_flight();
void book_ticket();
void cancel_ticket();
void show_all();
int main() {
int choice;
while (1) {
printf("1. Add Flight\n");
printf("2. Delete Flight\n");
printf("3. Query Flight\n");
printf("4. Book Ticket\n");
printf("5. Cancel Ticket\n");
printf("6. Show All\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_flight();
break;
case 2:
delete_flight();
break;
case 3:
query_flight();
break;
case 4:
book_ticket();
break;
case 5:
cancel_ticket();
break;
case 6:
show_all();
break;
case 7:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
// 添加航班
void add_flight() {
struct Flight *flight = (struct Flight *) malloc(sizeof(struct Flight));
printf("Enter flight number: ");
scanf("%s", flight->flight_no);
printf("Enter from: ");
scanf("%s", flight->from);
printf("Enter to: ");
scanf("%s", flight->to);
printf("Enter seats: ");
scanf("%d", &flight->seats);
flight->booked_seats = 0;
flight->passengers = NULL;
flight->ids = NULL;
flights[flight_count++] = flight;
printf("Flight added successfully!\n");
}
// 删除航班
void delete_flight() {
char flight_no[10];
printf("Enter flight number: ");
scanf("%s", flight_no);
int i;
for (i = 0; i < flight_count; i++) {
if (strcmp(flights[i]->flight_no, flight_no) == 0) {
free(flights[i]->passengers);
free(flights[i]->ids);
free(flights[i]);
int j;
for (j = i; j < flight_count - 1; j++) {
flights[j] = flights[j + 1];
}
flight_count--;
printf("Flight deleted successfully!\n");
return;
}
}
printf("Flight not found!\n");
}
// 查询航班
void query_flight() {
char flight_no[10];
printf("Enter flight number: ");
scanf("%s", flight_no);
int i;
for (i = 0; i < flight_count; i++) {
if (strcmp(flights[i]->flight_no, flight_no) == 0) {
printf("Flight number: %s\n", flights[i]->flight_no);
printf("From: %s\n", flights[i]->from);
printf("To: %s\n", flights[i]->to);
printf("Seats: %d\n", flights[i]->seats);
printf("Booked seats: %d\n", flights[i]->booked_seats);
if (flights[i]->booked_seats > 0) {
printf("Passengers:\n");
int j;
for (j = 0; j < flights[i]->booked_seats; j++) {
printf(" %d. %s (%s)\n", j + 1, flights[i]->passengers[j], flights[i]->ids[j]);
}
}
return;
}
}
printf("Flight not found!\n");
}
// 订票
void book_ticket() {
char flight_no[10];
printf("Enter flight number: ");
scanf("%s", flight_no);
int i;
for (i = 0; i < flight_count; i++) {
if (strcmp(flights[i]->flight_no, flight_no) == 0) {
if (flights[i]->booked_seats == flights[i]->seats) {
printf("No seats available!\n");
return;
}
char *passenger = (char *) malloc(50 * sizeof(char));
char *id = (char *) malloc(20 * sizeof(char));
printf("Enter passenger name: ");
scanf("%s", passenger);
printf("Enter passenger ID: ");
scanf("%s", id);
if (flights[i]->passengers == NULL) {
flights[i]->passengers = (char **) malloc(flights[i]->seats * sizeof(char *));
flights[i]->ids = (char **) malloc(flights[i]->seats * sizeof(char *));
}
flights[i]->passengers[flights[i]->booked_seats] = passenger;
flights[i]->ids[flights[i]->booked_seats] = id;
flights[i]->booked_seats++;
printf("Ticket booked successfully!\n");
return;
}
}
printf("Flight not found!\n");
}
// 退票
void cancel_ticket() {
char flight_no[10];
printf("Enter flight number: ");
scanf("%s", flight_no);
int i;
for (i = 0; i < flight_count; i++) {
if (strcmp(flights[i]->flight_no, flight_no) == 0) {
if (flights[i]->booked_seats == 0) {
printf("No tickets booked!\n");
return;
}
int seat;
printf("Enter seat number: ");
scanf("%d", &seat);
if (seat <= 0 || seat > flights[i]->booked_seats) {
printf("Invalid seat number!\n");
return;
}
free(flights[i]->passengers[seat - 1]);
free(flights[i]->ids[seat - 1]);
int j;
for (j = seat - 1; j < flights[i]->booked_seats - 1; j++) {
flights[i]->passengers[j] = flights[i]->passengers[j + 1];
flights[i]->ids[j] = flights[i]->ids[j + 1];
}
flights[i]->booked_seats--;
printf("Ticket cancelled successfully!\n");
return;
}
}
printf("Flight not found!\n");
}
// 显示所有航班信息
void show_all() {
int i, j, count = 0;
for (i = 0; i < flight_count; i++) {
printf("Flight number: %s\n", flights[i]->flight_no);
printf("From: %s\n", flights[i]->from);
printf("To: %s\n", flights[i]->to);
printf("Seats: %d\n", flights[i]->seats);
printf("Booked seats: %d\n", flights[i]->booked_seats);
if (flights[i]->booked_seats > 0) {
printf("Passengers:\n");
for (j = 0; j < flights[i]->booked_seats; j++) {
printf(" %d. %s (%s)\n", j + 1, flights[i]->passengers[j], flights[i]->ids[j]);
}
}
count++;
}
printf("Total flights: %d\n", count);
}
```
该程序通过结构体和指针实现了航班的增加、删除、查询、订票、退票以及显示所有航班信息的功能,并且将所有数据保存在内存中。如果需要将数据保存到文件中,可以使用文件读写函数来实现。
阅读全文