用C语言写一个飞机订票系统
时间: 2023-05-15 08:05:04 浏览: 69
非常感谢您的提问,我可以为您提供一份基于C语言的飞机订票系统的代码示例,如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FLIGHTS 10
#define MAX_NAME_LENGTH 20
typedef struct {
char name[MAX_NAME_LENGTH];
int seats;
int booked;
} Flight;
Flight flights[MAX_FLIGHTS];
void initializeFlights() {
strcpy(flights[0].name, "Flight 1");
flights[0].seats = 50;
flights[0].booked = 0;
strcpy(flights[1].name, "Flight 2");
flights[1].seats = 100;
flights[1].booked = 0;
strcpy(flights[2].name, "Flight 3");
flights[2].seats = 150;
flights[2].booked = 0;
// Add more flights here if needed
}
void displayFlights() {
printf("Available flights:\n");
for (int i = 0; i < MAX_FLIGHTS; i++) {
if (flights[i].seats > 0) {
printf("%d. %s (%d seats)\n", i+1, flights[i].name, flights[i].seats);
}
}
}
void bookFlight(int flightIndex, int numSeats) {
if (flightIndex < 0 || flightIndex >= MAX_FLIGHTS) {
printf("Invalid flight index.\n");
return;
}
Flight *flight = &flights[flightIndex];
if (flight->seats - flight->booked < numSeats) {
printf("Not enough seats available.\n");
return;
}
flight->booked += numSeats;
printf("Booked %d seats on %s.\n", numSeats, flight->name);
}
int main() {
initializeFlights();
int choice = 0;
while (choice != 3) {
printf("\n1. Display available flights\n");
printf("2. Book a flight\n");
printf("3. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
displayFlights();
break;
case 2:
printf("Enter flight index: ");
int flightIndex;
scanf("%d", &flightIndex);
printf("Enter number of seats to book: ");
int numSeats;
scanf("%d", &numSeats);
bookFlight(flightIndex-1, numSeats);
break;
case 3:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
希望这份代码能够帮助您实现一个简单的飞机订票系统。
阅读全文