飞机订票系统课设c语言代码
时间: 2024-05-12 08:11:42 浏览: 139
模拟飞机票订票系统c语言源代码
5星 · 资源好评率100%
飞机订票系统是一个比较复杂的课程设计,需要涉及到多种知识点,包括C语言的基本语法、文件读写、结构体、指针等。下面是一个简单的飞机订票系统的C语言代码,仅供参考:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SEATS 100
struct flight {
char id[10];
char origin[20];
char destination[20];
int seats[MAX_SEATS];
int num_seats;
};
struct flight flights[10];
int num_flights = 0;
void read_flights() {
FILE *fp = fopen("flights.txt", "r");
if (fp == NULL) {
printf("Error opening flights.txt\n");
exit(1);
}
while (!feof(fp)) {
fscanf(fp, "%s %s %s", flights[num_flights].id, flights[num_flights].origin, flights[num_flights].destination);
for (int i = 0; i < MAX_SEATS; i++) {
fscanf(fp, "%d", &flights[num_flights].seats[i]);
if (flights[num_flights].seats[i] == 0) {
flights[num_flights].num_seats = i;
break;
}
}
num_flights++;
}
fclose(fp);
}
void write_flights() {
FILE *fp = fopen("flights.txt", "w");
if (fp == NULL) {
printf("Error opening flights.txt\n");
exit(1);
}
for (int i = 0; i < num_flights; i++) {
fprintf(fp, "%s %s %s", flights[i].id, flights[i].origin, flights[i].destination);
for (int j = 0; j < flights[i].num_seats; j++) {
fprintf(fp, " %d", flights[i].seats[j]);
}
fprintf(fp, "\n");
}
fclose(fp);
}
void print_flights() {
for (int i = 0; i < num_flights; i++) {
printf("%d. %s (%s to %s)\n", i+1, flights[i].id, flights[i].origin, flights[i].destination);
}
}
void print_seats(int flight_num) {
printf("Seats available on flight %s:\n", flights[flight_num-1].id);
for (int i = 0; i < flights[flight_num-1].num_seats; i++) {
printf("%d ", flights[flight_num-1].seats[i]);
}
printf("\n");
}
void book_seat(int flight_num, int seat_num) {
flights[flight_num-1].seats[seat_num-1] = 1;
flights[flight_num-1].num_seats--;
}
int main() {
read_flights();
int choice;
while (1) {
printf("1. List available flights\n");
printf("2. Book a seat\n");
printf("3. Quit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
print_flights();
break;
case 2:
print_flights();
int flight_num, seat_num;
printf("Enter flight number: ");
scanf("%d", &flight_num);
print_seats(flight_num);
printf("Enter seat number: ");
scanf("%d", &seat_num);
book_seat(flight_num, seat_num);
write_flights();
printf("Seat booked!\n");
break;
case 3:
return 0;
default:
printf("Invalid choice.\n");
break;
}
}
}
```
以上代码实现了一个基本的飞机订票系统,能够读取和写入航班信息,列出可用航班,预订座位等功能。如果需要更完整的代码或者有其他问题,请及时提出相关问题。
阅读全文