写一个飞机订票系统c语言代码,要求包含一下内容7.管理员功能:特定账号密码可以进入管理员功能界面。 8.管理员新增加机票功能:可新增加机票信息。 9.管理员修改航班信息功能。
时间: 2024-03-19 15:43:29 浏览: 101
以下是一个包含管理员功能、新增机票和修改航班信息功能的飞机订票系统的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FLIGHTS 10
#define MAX_SEATS 50
struct flight {
int id;
char destination[50];
int seats[MAX_SEATS];
int num_seats;
int price;
};
struct flight flights[MAX_FLIGHTS];
int num_flights = 0;
void add_flight() {
if (num_flights >= MAX_FLIGHTS) {
printf("Maximum number of flights reached.\n");
return;
}
struct flight f;
f.id = num_flights + 1;
printf("Enter destination: ");
fgets(f.destination, 50, stdin);
f.destination[strcspn(f.destination, "\n")] = 0;
printf("Enter price: ");
scanf("%d", &f.price);
f.num_seats = MAX_SEATS;
for (int i = 0; i < MAX_SEATS; i++) {
f.seats[i] = 0;
}
flights[num_flights] = f;
num_flights++;
}
void show_flights() {
printf("Flights:\n");
for (int i = 0; i < num_flights; i++) {
printf("%d. %s, %d seats, $%d\n", flights[i].id, flights[i].destination, flights[i].num_seats, flights[i].price);
}
}
void book_seat() {
int flight_id, seat_num;
printf("Enter flight ID: ");
scanf("%d", &flight_id);
if (flight_id < 1 || flight_id > num_flights) {
printf("Invalid flight ID.\n");
return;
}
struct flight *f = &flights[flight_id - 1];
printf("Enter seat number: ");
scanf("%d", &seat_num);
if (seat_num < 1 || seat_num > MAX_SEATS) {
printf("Invalid seat number.\n");
return;
}
if (f->seats[seat_num - 1] == 1) {
printf("Seat already booked.\n");
return;
}
f->seats[seat_num - 1] = 1;
f->num_seats--;
printf("Seat booked successfully.\n");
}
void admin_menu() {
char username[50], password[50];
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
if (strcmp(username, "admin") == 0 && strcmp(password, "admin123") == 0) {
int choice;
do {
printf("\n1. Add flight\n2. Modify flight\n3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_flight();
break;
case 2:
printf("Enter flight ID: ");
int flight_id;
scanf("%d", &flight_id);
if (flight_id < 1 || flight_id > num_flights) {
printf("Invalid flight ID.\n");
break;
}
struct flight *f = &flights[flight_id - 1];
printf("Enter new destination: ");
fgets(f->destination, 50, stdin);
f->destination[strcspn(f->destination, "\n")] = 0;
printf("Enter new price: ");
scanf("%d", &f->price);
break;
case 3:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 3);
} else {
printf("Invalid username or password.\n");
}
}
int main() {
int choice;
do {
printf("\n1. Show flights\n2. Book seat\n3. Admin menu\n4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
show_flights();
break;
case 2:
book_seat();
break;
case 3:
admin_menu();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 4);
return 0;
}
```
在这个示例中,我们添加了一个管理员菜单,只有在输入正确的用户名和密码之后才能进入。管理员可以新增机票和修改航班信息。在修改航班信息时,管理员需要输入要修改的航班的ID,然后输入新的目的地和价格。注意,在修改目的地时,我们使用了fgets函数而不是scanf函数,这是因为scanf函数会留下换行符,而fgets函数会自动将其去除。
阅读全文