火车订票系统的C语言代码
时间: 2023-03-12 12:44:44 浏览: 261
我不知道具体的C语言代码,但是可以告诉你大致的实现步骤: 1. 定义火车订票系统的相关数据结构;2. 编写函数来实现订票系统的相关功能;3. 编写main函数来调用之前定义的函数;4. 测试程序,修正错误;5. 编译并运行程序。
相关问题
设计一个c语言的火车订票系统的高级代码
好的,以下是一个简单的火车订票系统的C语言高级代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAX_SEATS 100
struct Train {
char name[50];
int number;
int seats[MAX_SEATS];
int availableSeats;
};
struct Train trains[3] = {
{"Express", 12345, {0}, MAX_SEATS},
{"Superfast", 67890, {0}, MAX_SEATS},
{"Local", 24680, {0}, MAX_SEATS}
};
void displayMenu() {
printf("************ Train Booking System ************\n");
printf("1. Book a ticket\n");
printf("2. Cancel a ticket\n");
printf("3. Display train details\n");
printf("4. Exit\n");
printf("***********************************************\n");
}
void bookTicket(int trainIndex) {
int seatNo;
bool isSeatAvailable = false;
for(int i=0; i<MAX_SEATS; i++) {
if(trains[trainIndex].seats[i] == 0) {
isSeatAvailable = true;
seatNo = i+1;
break;
}
}
if(isSeatAvailable) {
printf("Your seat has been booked. Seat number: %d\n", seatNo);
trains[trainIndex].seats[seatNo-1] = 1;
trains[trainIndex].availableSeats--;
} else {
printf("Sorry, no seats available.\n");
}
}
void cancelTicket(int trainIndex) {
int seatNo;
printf("Enter the seat number to cancel: ");
scanf("%d", &seatNo);
if(trains[trainIndex].seats[seatNo-1] == 1) {
printf("Your ticket has been cancelled. Seat number: %d\n", seatNo);
trains[trainIndex].seats[seatNo-1] = 0;
trains[trainIndex].availableSeats++;
} else {
printf("Invalid seat number.\n");
}
}
void displayTrainDetails(int trainIndex) {
printf("Train Name: %s\n", trains[trainIndex].name);
printf("Train Number: %d\n", trains[trainIndex].number);
printf("Total seats: %d\n", MAX_SEATS);
printf("Available seats: %d\n", trains[trainIndex].availableSeats);
}
int main() {
int choice, trainIndex;
bool isQuit = false;
while(!isQuit) {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the train number (1-3): ");
scanf("%d", &trainIndex);
bookTicket(trainIndex-1);
break;
case 2:
printf("Enter the train number (1-3): ");
scanf("%d", &trainIndex);
cancelTicket(trainIndex-1);
break;
case 3:
printf("Enter the train number (1-3): ");
scanf("%d", &trainIndex);
displayTrainDetails(trainIndex-1);
break;
case 4:
isQuit = true;
break;
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}
```
这个火车订票系统可以让用户选择预订、取消预订或查看火车详情。火车的座位数是固定的,每个座位都可以预订或取消预订。当一张车票被预订或取消预订时,系统会自动更新可用座位的数量。
阅读全文