用C++代码写一个火车票管理系统
时间: 2023-03-23 07:04:46 浏览: 99
好的,我可以回答这个问题。以下是一个简单的火车票管理系统的 C 代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TICKETS 100
struct Ticket {
int id;
char name[50];
char from[50];
char to[50];
char date[20];
int price;
};
struct Ticket tickets[MAX_TICKETS];
int num_tickets = ;
void add_ticket() {
if (num_tickets >= MAX_TICKETS) {
printf("Error: too many tickets!\n");
return;
}
struct Ticket t;
t.id = num_tickets + 1;
printf("Enter passenger name: ");
scanf("%s", t.name);
printf("Enter departure city: ");
scanf("%s", t.from);
printf("Enter destination city: ");
scanf("%s", t.to);
printf("Enter date (YYYY-MM-DD): ");
scanf("%s", t.date);
printf("Enter price: ");
scanf("%d", &t.price);
tickets[num_tickets++] = t;
printf("Ticket added successfully!\n");
}
void list_tickets() {
printf("ID\tName\tFrom\tTo\tDate\tPrice\n");
for (int i = ; i < num_tickets; i++) {
struct Ticket t = tickets[i];
printf("%d\t%s\t%s\t%s\t%s\t%d\n", t.id, t.name, t.from, t.to, t.date, t.price);
}
}
int main() {
while (1) {
printf("1. Add ticket\n");
printf("2. List tickets\n");
printf("3. Exit\n");
printf("Enter choice: ");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
add_ticket();
break;
case 2:
list_tickets();
break;
case 3:
printf("Goodbye!\n");
exit();
default:
printf("Invalid choice!\n");
}
}
return ;
}
阅读全文