飞机订票 问题描述: 某公司每天有10航班(航班号、价格),每个航班的飞机,共有80个坐位,分20排,每排4个位子。编号为A,B,C,D。如座位号:10D表示10排D座。设计一程序,完成以下功能:
1.能从文件中导入订票信息
2.能从键盘录入订票信息,:乘客姓名,身份证 号,航班号,乘坐日期,座位号
3.能根据身份证,航班号,乘坐日期修改乘坐座位
4.能根据身份证,航班号,乘坐日期删除订票信息
5.查询指定航班,指定日期的乘客信息
6.能根据航班日期计算销售额
7.能将订票信息保存文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FLIGHTS 10
#define ROWS 20
#define SEATS_PER_ROW 4
#define SEATS_PER_FLIGHT (ROWS*SEATS_PER_ROW)
// 定义订票信息的结构体
typedef struct {
char name[20];
char id[20];
char flight[10];
char date[10];
char seat[5];
} Reservation;
// 定义航班信息的结构体
typedef struct {
char flight[10];
int price;
int seats[ROWS][SEATS_PER_ROW];
} Flight;
// 定义全局变量
Flight flights[MAX_FLIGHTS];
int num_flights = 0;
Reservation reservations[SEATS_PER_FLIGHT*MAX_FLIGHTS];
int num_reservations = 0;
// 从文件中导入航班信息
void load_flights(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp) {
while (num_flights < MAX_FLIGHTS && fscanf(fp, "%s %d", flights[num_flights].flight, &flights[num_flights].price) == 2) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < SEATS_PER_ROW; j++) {
flights[num_flights].seats[i][j] = 0; // 0 表示空座位
}
}
num_flights++;
}
fclose(fp);
}
}
// 从键盘录入订票信息
void make_reservation() {
// 从标准输入读取订票信息,存入一个临时的 Reservation 结构体中
Reservation temp;
printf("Name: ");
scanf("%s", temp.name);
printf("ID: ");
scanf("%s", temp.id);
printf("Flight: ");
scanf("%s", temp.flight);
printf("Date: ");
scanf("%s", temp.date);
printf("Seat: ");
scanf("%s", temp.seat);
// 在航班信息中查找该航班,如果存在则更新相应的座位信息,否则返回错误信息
int found = 0;
for (int i = 0; i < num_flights; i++) {
if (strcmp(flights[i].flight, temp.flight) == 0) {
int row = temp.seat[0] - '0' - 1;
int col = temp.seat[1] - 'A';
if (row >= 0 && row < ROWS && col >= 0 && col < SEATS_PER_ROW && flights[i].seats[row][col] == 0) {
flights[i].seats[row][col] = 1; // 1 表示已订票
strcpy(reservations[num_reservations].name, temp.name);
strcpy(reservations[num_reservations].id, temp.id);
strcpy(reservations[num_reservations].flight, temp.flight);
strcpy(reservations[num_reservations].date, temp.date);
strcpy(reservations[num_reservations].seat, temp.seat);
num_reservations++;
found = 1;
break;
}
}
}
if (!found) {
printf("Error: flight not found or seat already taken.
");
}
}
// 根据身份证,航班号,乘坐日期修改乘坐座位
void modify_reservation() {
char id[20], flight[10], date[10], seat[5];
printf("ID: ");
scanf("%s", id);
printf("Flight: ");
scanf("%s", flight);
printf("Date: ");
scanf("%s", date);
printf("Seat: ");
scanf("%s", seat);
// 在订票信息中查找相应的记录,如果存在则更新相应的座位信息,否则返回错误信息
for (int i = 0; i < num_reservations; i++) {
if (strcmp(reservations[i].id, id) == 0 && strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
int row = seat[0] - '0' - 1;
int col = seat[1] - 'A';
if (row >= 0 && row < ROWS && col >= 0 && col < SEATS_PER_ROW && flights[i].seats[row][col] == 0) {
int old_row = reservations[i].seat[0] - '0' - 1;
int old_col = reservations[i].seat[1] - 'A';
flights[i].seats[old_row][old_col] = 0;
flights[i].seats[row][col] = 1;
strcpy(reservations[i].seat, seat);
}
break;
}
}
}
// 根据身份证,航班号,乘坐日期删除订票信息
void cancel_reservation() {
char id[20], flight[10], date[10];
printf("ID: ");
scanf("%s", id);
printf("Flight: ");
scanf("%s", flight);
printf("Date: ");
scanf("%s", date);
// 在订票信息中查找相应的记录,如果存在则删除该记录,否则返回错误信息
for (int i = 0; i < num_reservations; i++) {
if (strcmp(reservations[i].id, id) == 0 && strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
int row = reservations[i].seat[0] - '0' - 1;
int col = reservations[i].seat[1] - 'A';
flights[i].seats[row][col] = 0;
for (int j = i; j < num_reservations-1; j++) {
reservations[j] = reservations[j+1];
}
num_reservations--;
break;
}
}
}
// 查询指定航班,指定日期的乘客信息
void list_passengers() {
char flight[10], date[10];
printf("Flight: ");
scanf("%s", flight);
printf("Date: ");
scanf("%s", date);
// 在订票信息中查找相应的记录,如果存在则返回相应的乘客信息,否则返回错误信息
printf("%-20s%-20s%-20s%-10s%-10s
", "Name", "ID", "Flight", "Date", "Seat");
for (int i = 0; i < num_reservations; i++) {
if (strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
printf("%-20s%-20s%-20s%-10s%-10s
", reservations[i].name, reservations[i].id, reservations[i].flight, reservations[i].date, reservations[i].seat);
}
}
}
// 根据航班日期计算销售额
void calculate_revenue() {
char flight[10], date[10];
printf("Flight: ");
scanf("%s", flight);
printf("Date: ");
scanf("%s", date);
// 在订票信息中查找相应的记录,如果存在则累加销售额,否则返回错误信息
int revenue = 0;
for (int i = 0; i < num_reservations; i++) {
if (strcmp(reservations[i].flight, flight) == 0 && strcmp(reservations[i].date, date) == 0) {
for (int j = 0; j < num_flights; j++) {
if (strcmp(flights[j].flight, flight) == 0) {
revenue += flights[j].price;
break;
}
}
}
}
printf("Revenue: %d
", revenue);
}
// 将订票信息保存到文件
void save_reservations(const char *filename) {
FILE *fp = fopen(filename, "w");
if (fp) {
for (int i = 0; i < num_reservations; i++) {
fprintf(fp, "%s %s %s %s %s
", reservations[i].name, reservations[i].id, reservations[i].flight, reservations[i].date, reservations[i].seat);
}
fclose(fp);
}
}
// 主函数
int main() {
// 从文件中导入航班信息
load_flights("flights.txt");
// 循环处理用户的输入,直到用户输入 quit
char command[10];
while (1) {
printf("Enter a command (make, modify, cancel, list, revenue, save, quit): ");
scanf("%s", command);
if (strcmp(command, "make") == 0) {
make_reservation();
} else if (strcmp(command, "modify") == 0) {
modify_reservation();
} else if (strcmp(command, "cancel") == 0) {
cancel_reservation();
} else if (strcmp(command, "list") == 0) {
list_passengers();
} else if (strcmp(command, "revenue") == 0) {
calculate_revenue();
} else if (strcmp(command, "save") == 0) {
save_reservations("reservations.txt");
} else if (strcmp(command, "quit") == 0) {
break;
} else {
printf("Invalid command.
");
}
}
return 0;
}
以下是一个可供参考的C语言实现飞机订票问题的代码,其中使用结构体来存储订票信息和座位情况。
#include <stdio.h>
#include <string.h>
#define MAX_FLIGHTS 10 // 最大航班数
#define MAX_SEATS 80 // 最大座位数
#define MAX_NAME_LENGTH 50 // 乘客姓名最大长度
#define MAX_ID_LENGTH 20 // 身份证号最大长度
#define MAX_DATE_LENGTH 20 // 日期最大长度
#define MAX_PRICE 5000 // 最大机票价格
// 座位编号结构体
struct SeatNum {
int row;
char row_char;
};
// 订票信息结构体
struct Ticket {
char name[MAX_NAME_LENGTH];
char id[MAX_ID_LENGTH];
int flight;
char date[MAX_DATE_LENGTH];
struct SeatNum seat;
};
// 航班信息结构体
struct Flight {
int number;
int price;
struct SeatNum seats[MAX_SEATS];
};
// 存储所有航班信息的数组
struct Flight flights[MAX_FLIGHTS];
// 初始化所有航班的座位信息
void initSeats() {
for (int i = 0; i < MAX_FLIGHTS; i++) {
for (int j = 0; j < MAX_SEATS; j++) {
flights[i].seats[j].row = j / 4 + 1;
flights[i].seats[j].row_char = 'A' + j % 4;
}
}
}
// 从文件中导入订票信息
void importTickets(char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件 %s\n", filename);
return;
}
int flight, row;
char row_char;
while (!feof(fp)) {
struct Ticket ticket;
fscanf(fp, "%s %s %d %s %d%c\n", ticket.name, ticket.id, &ticket.flight, ticket.date, &row, &row_char);
ticket.seat.row = row;
ticket.seat.row_char = row_char;
flights[ticket.flight - 1].seats[(row - 1) * 4 + row_char - 'A'] = (struct SeatNum){0, '-'};
}
fclose(fp);
}
// 从键盘录入订票信息
void inputTicket() {
printf("请输入乘客姓名:");
char name[MAX_NAME_LENGTH];
scanf("%s", name);
printf("请输入身份证号:");
char id[MAX_ID_LENGTH];
scanf("%s", id);
printf("请输入航班号:");
int flight;
scanf("%d", &flight);
printf("请输入乘坐日期:");
char date[MAX_DATE_LENGTH];
scanf("%s", date);
printf("请输入座位号(如:10D):");
int row;
char row_char;
scanf("%d%c", &row, &row_char);
struct SeatNum seat_num = {row, row_char};
// 检查座位是否已经被预定
if (flights[flight - 1].seats[(row - 1) * 4 + row_char - 'A'].row != 0) {
printf("此座位已经被预订\n");
return;
}
// 检查航班号是否合法
if (flight < 1 || flight > MAX_FLIGHTS) {
printf("航班号 %d 不合法\n", flight);
return;
}
// 检查价格是否超出范围
if (flights[flight - 1].price > MAX_PRICE) {
printf("票价超出范围\n");
return;
}
flights[flight - 1].seats[(row - 1) * 4 + row_char - 'A'] = seat_num;
struct Ticket ticket = {name, id, flight, date, seat_num};
// 输出订票信息
printf("订票成功:\n");
printf("姓名:%s\n", ticket.name);
printf("身份证号:%s\n", ticket.id);
printf("航班号:%d\n", ticket.flight);
printf("乘坐日期:%s\n", ticket.date);
printf("座位号:%d
我写过这个系统
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
以下是一个简单的C语言实现,实现了题目所述的所有功能:
#include <stdio.h>
#include <string.h>
#define FLIGHT_NUM 10
#define SEAT_ROW 20
#define SEAT_COL 4
#define SEAT_TOTAL 80
struct Seat {
char id[20]; // 身份证号
char name[20]; // 乘客姓名
int flight_id; // 航班号
char date[20]; // 乘坐日期
char seat_num[5]; // 座位号
};
struct Flight {
int id; // 航班号
double price; // 价格
struct Seat seats[SEAT_TOTAL]; // 座位信息
};
struct Company {
struct Flight flights[FLIGHT_NUM]; // 航班信息
double sales[FLIGHT_NUM]; // 销售额
};
void import_data(struct Company *company);
void input_data(struct Company *company);
void modify_seat(struct Company *company);
void delete_seat(struct Company *company);
void query_passenger(struct Company *company);
void calculate_sales(struct Company *company);
void save_data(struct Company *company);
int main() {
struct Company company = {0};
int option;
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("7. 将订票信息保存文件\n");
printf("8. 退出程序\n");
printf("请选择:");
scanf("%d", &option);
switch (option) {
case 1:
import_data(&company);
break;
case 2:
input_data(&company);
break;
case 3:
modify_seat(&company);
break;
case 4:
delete_seat(&company);
break;
case 5:
query_passenger(&company);
break;
case 6:
calculate_sales(&company);
break;
case 7:
save_data(&company);
break;
case 8:
return 0;
default:
printf("输入错误,请重新选择\n");
break;
}
}
}
void import_data(struct Company *company) {
char filename[100];
printf("请输入文件名:");
scanf("%s", filename);
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
fread(company, sizeof(struct Company), 1, fp);
fclose(fp);
printf("导入成功\n");
}
void input_data(struct Company *company) {
char name[20];
char id[20];
int flight_id;
char date[20];
char seat_num[5];
printf("请输入乘客姓名:");
scanf("%s", name);
printf("请输入身份证号:");
scanf("%s", id);
printf("请输入航班号:");
scanf("%d", &flight_id);
printf("请输入乘坐日期:");
scanf("%s", date);
printf("请输入座位号:");
scanf("%s", seat_num);
// 查找航班
struct Flight *flight = NULL;
for (int i = 0; i < FLIGHT_NUM; i++) {
if (company->flights[i].id == flight_id) {
flight = &company->flights[i];
break;
}
}
if (flight == NULL) {
printf("无法找到该航班\n");
return;
}
// 查找座位
int row = seat_num[0] - '0';
char col = seat_num[1];
if (row < 1 || row > SEAT_ROW || col < 'A' || col > 'D') {
printf("无效的座位号\n");
return;
}
int seat_index = (row - 1) * SEAT_COLcol - 'A';
struct Seat *seat = &flight->seats[seat_index];
if (strcmp(seat->id, "") != 0) {
printf("该座位已经被预订\n");
return;
}
// 订票
strcpy(seat->id, id);
strcpy(seat->name, name);
seat->flight_id = flight_id;
strcpy(seat->date, date);
strcpy(seat->seat_num, seat_num);
// 更新销售额
company->sales[flight_id - 1] += flight->price;
printf("订票成功\n");
}
void modify_seat(struct Company *company) {
char id[20];
int flight_id;
char date[20];
char seat_num[5];
printf("请输入乘客身份证号:");
scanf("%s", id);
printf("请输入航班号:");
scanf("%d", &flight_id);
printf("请输入乘坐日期:");
scanf("%s", date);
printf("请输入新的座位号:");
scanf("%s", seat_num);
// 查找航班
struct Flight *flight = NULL;
for (int i = 0; i < FLIGHT_NUM; i++) {
if (company->flights[i].id == flight_id) {
flight = &company->flights[i];
break;
}
}
if (flight == NULL) {
printf("无法找到该航班\n");
return;
}
// 查找座位
[int row](poe://www.poe.com/_api/key_phrase?phrase=int%20row&prompt=Tell%20me%20more%20about%20int%20row.) = seat_num[0] - '0';
char col = seat_num[1];
if (row < 1 || row > SEAT_ROW || col < 'A' || col > 'D') {
printf("无效的座位号\n");
return;
}
int seat_index = (row - 1) * SEAT_COL + col - 'A';
struct Seat *seat = NULL;
for (int i = 0; i < SEAT_TOTAL; i++) {
if (strcmp(flight->seats[i].id, id) == 0 &&
flight->seats[i].flight_id == flight_id &&
strcmp(flight->seats[i].date, date) == 0) {
seat = &flight->seats[i];
break;
}
}
if (seat == NULL) {
printf("无法找到该乘客\n");
return;
}
// 判断新座位是否已经被预订
struct Seat *new_seat = &flight->seats[seat_index];
if (strcmp(new_seat->id, "") != 0) {
printf("该座位已经被预订\n");
return;
}
// 修改座位信息
strcpy(new_seat->id, seat->id);
strcpy(new_seat->name, seat->name);
new_seat->flight_id = seat->flight_id;
strcpy(new_seat->date, seat->date);
strcpy(new_seat->seat_num, seat_num);
strcpy(seat->id, "");
strcpy(seat->name, "");
seat->flight_id = 0;
strcpy(seat->date, "");
strcpy(seat->seat_num, "");
printf("修改座位成功\n");
}
void print_flight(struct Flight *flight) {
printf("航班号:%d,起飞时间:%s,到达时间:%s,起飞城市:%s,到达城市:%s,票价:%d\n",
flight->id, flight->departure_time, flight->arrival_time, flight->departure_city, flight->arrival_city, flight->price);
}
void print_seat(struct Seat *seat) {
printf("座位号:%s,乘客身份证号:%s,乘客姓名:%s,航班号:%d,乘坐日期:%s\n",
seat->seat_num, seat->id, seat->name, seat->flight_id, seat->date);
}
void print_company(struct Company *company) {
printf("公司名称:%s,总销售额:%d\n", company->name, company->total_sales);
printf("航班信息:\n");
for (int i = 0; i < FLIGHT_NUM; i++) {
if (company->flights[i].id != 0) {
print_flight(&company->flights[i]);
printf("座位信息:\n");
for (int j = 0; j < SEAT_TOTAL; j++) {
if (strcmp(company->flights[i].seats[j].id, "") != 0) {
print_seat(&company->flights[i].seats[j]);
}
}
}
}
}
int main() {
struct Company company = {
.name = "航空公司",
.total_sales = 0,
.flights = {
{
.id = 1,
.departure_time = "08:00",
.arrival_time = "10:00",
.departure_city = "北京",
.arrival_city = "上海",
.price = 1000,
.seats = {{0}}
},
{
.id = 2,
.departure_time = "12:00",
.arrival_time = "14:00",
.departure_city = "上海",
.arrival_city = "北京",
.price = 1000,
.seats = {{0}}
},
{
.id = 3,
.departure_time = "16:00",
.arrival_time = "18:00",
.departure_city = "北京",
.arrival_city = "广州",
.price = 2000,
.seats = {{0}}
},
{
.id = 4,
.departure_time = "20:00",
.arrival_time = "22:00",
.departure_city = "广州",
.arrival_city = "北京",
.price = 2000,
.seats = {{0}}
}
},
.sales = {0, 0, 0, 0}
};
int choice = -1;
while (choice != 0) {
printf("欢迎使用航空订票系统\n");
printf("1. 查询航班信息\n");
printf("2. 查询座位信息\n");
printf("3. 预订座位\n");
printf("4. 修改座位\n");
printf("5. 查询销售额\n");
printf("0. 退出系统\n");
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
query_flight(&company);
break;
case 2:
query_seat(&company);
break;
case 3:
book_seat(&company);
break;
case 4:
modify_seat(&company);
break;
case 5:
printf("总销售额:%d\n", company.total_sales);
for (int i = 0; i < FLIGHT_NUM; i++) {
printf("航班%d销售额:%d\n", i + 1, company.sales[i]);
}
break;
case 0:
printf("谢谢使用,再见!\n");
break;
default:
printf("无效的操作\n");
break;
}
}
return 0;
}
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
代码
#include <stdio.h>
int main()
{
int a[15] = {90, 99, 97, 86, 75, 73, 69, 68, 51, 43, 32, 31, 28, 25, 6};
int x, low, high, mid, n;
n = 15;
low = 0;
high = n - 1;
scanf("%d", &x);
while (low <= high)
{
mid = (low + high) / 2;
if (x > a[mid])
high = mid - 1;
else if (x < a[mid])
low = mid + 1;
else if (x == a[mid]){
printf("%d is %dth number!\n", x, mid+1);
break;
}
}
if (x != a[mid])
printf("No match!\n");
return 0;
}
结果