用C语言写一个火车票订票系统的程序
时间: 2023-10-17 17:05:34 浏览: 169
以下是一个简单的火车票订票系统的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义火车票结构体
struct train_ticket {
char train_id[10];
char start_station[20];
char end_station[20];
char departure_time[10];
int total_seats;
int sold_seats;
float price;
};
// 定义一个全局的火车票数组
struct train_ticket tickets[100];
// 定义一个全局的票数计数器
int ticket_count = 0;
// 函数声明
void add_ticket();
void show_tickets();
void search_ticket();
void book_ticket();
int main() {
int choice;
while (1) {
printf("\nWelcome to the Train Ticket Booking System!\n");
printf("1. Add new ticket\n");
printf("2. Show all tickets\n");
printf("3. Search ticket\n");
printf("4. Book ticket\n");
printf("5. Exit\n");
printf("Please enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_ticket();
break;
case 2:
show_tickets();
break;
case 3:
search_ticket();
break;
case 4:
book_ticket();
break;
case 5:
printf("Thank you for using the Train Ticket Booking System!\n");
exit(0);
default:
printf("Invalid choice, please enter again.\n");
}
}
return 0;
}
// 添加新火车票
void add_ticket() {
printf("\nAdd new ticket\n");
printf("Please enter the train ID: ");
scanf("%s", tickets[ticket_count].train_id);
printf("Please enter the start station: ");
scanf("%s", tickets[ticket_count].start_station);
printf("Please enter the end station: ");
scanf("%s", tickets[ticket_count].end_station);
printf("Please enter the departure time: ");
scanf("%s", tickets[ticket_count].departure_time);
printf("Please enter the total number of seats: ");
scanf("%d", &tickets[ticket_count].total_seats);
printf("Please enter the ticket price: ");
scanf("%f", &tickets[ticket_count].price);
tickets[ticket_count].sold_seats = 0;
ticket_count++;
printf("Ticket added successfully.\n");
}
// 显示所有火车票信息
void show_tickets() {
printf("\nAll tickets\n");
printf("%-10s%-20s%-20s%-10s%-10s%-10s%-10s\n", "ID", "Start", "End", "Departure", "Total", "Sold", "Price");
for (int i = 0; i < ticket_count; i++) {
printf("%-10s%-20s%-20s%-10s%-10d%-10d%-10.2f\n", tickets[i].train_id, tickets[i].start_station, tickets[i].end_station, tickets[i].departure_time, tickets[i].total_seats, tickets[i].sold_seats, tickets[i].price);
}
}
// 搜索火车票
void search_ticket() {
char train_id[10];
printf("\nSearch ticket\n");
printf("Please enter the train ID: ");
scanf("%s", train_id);
for (int i = 0; i < ticket_count; i++) {
if (strcmp(train_id, tickets[i].train_id) == 0) {
printf("%-10s%-20s%-20s%-10s%-10d%-10d%-10.2f\n", tickets[i].train_id, tickets[i].start_station, tickets[i].end_station, tickets[i].departure_time, tickets[i].total_seats, tickets[i].sold_seats, tickets[i].price);
return;
}
}
printf("Ticket not found.\n");
}
// 订票
void book_ticket() {
char train_id[10];
int num_tickets;
printf("\nBook ticket\n");
printf("Please enter the train ID: ");
scanf("%s", train_id);
for (int i = 0; i < ticket_count; i++) {
if (strcmp(train_id, tickets[i].train_id) == 0) {
printf("%-10s%-20s%-20s%-10s%-10d%-10d%-10.2f\n", tickets[i].train_id, tickets[i].start_station, tickets[i].end_station, tickets[i].departure_time, tickets[i].total_seats, tickets[i].sold_seats, tickets[i].price);
printf("Please enter the number of tickets to book: ");
scanf("%d", &num_tickets);
if (num_tickets > tickets[i].total_seats - tickets[i].sold_seats) {
printf("Not enough seats available.\n");
return;
}
tickets[i].sold_seats += num_tickets;
printf("Tickets booked successfully.\n");
return;
}
}
printf("Ticket not found.\n");
}
```
这个程序使用结构体来表示火车票,并提供了添加新票、查看所有票、搜索票和订票等功能。你可以根据实际需求进行修改和扩展。
阅读全文