用c语言编写一个车票管理系统
时间: 2023-08-22 11:04:25 浏览: 89
C语言编写的火车票管理系统
4星 · 用户满意度95%
好的,下面是一个简单的班次信息管理系统的代码示例,可以根据您的需求进行修改和完善:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_NUM 100 // 最大班次数
#define MAX_STR 50 // 最大字符串长度
typedef struct {
int id; // 班次号
char departure[MAX_STR]; // 发车时间
char start[MAX_STR]; // 起点站
char end[MAX_STR]; // 终点站
int time; // 行车时间(单位:分钟)
int capacity; // 额定载客量
int booked; // 已定票人数
} Schedule;
Schedule schedules[MAX_NUM]; // 班次数组
int num_schedules = 0; // 班次数目
// 从文件中读取班次信息
void load_schedules() {
FILE *fp = fopen("schedules.txt", "r");
if (fp == NULL) {
num_schedules = 0;
return;
}
fscanf(fp, "%d", &num_schedules);
for (int i = 0; i < num_schedules; i++) {
fscanf(fp, "%d %s %s %s %d %d %d", &schedules[i].id, schedules[i].departure,
schedules[i].start, schedules[i].end, &schedules[i].time,
&schedules[i].capacity, &schedules[i].booked);
}
fclose(fp);
}
// 将班次信息写入文件
void save_schedules() {
FILE *fp = fopen("schedules.txt", "w");
fprintf(fp, "%d\n", num_schedules);
for (int i = 0; i < num_schedules; i++) {
fprintf(fp, "%d %s %s %s %d %d %d\n", schedules[i].id, schedules[i].departure,
schedules[i].start, schedules[i].end, schedules[i].time,
schedules[i].capacity, schedules[i].booked);
}
fclose(fp);
}
// 添加新的班次信息
void add_schedule() {
if (num_schedules >= MAX_NUM) {
printf("Error: too many schedules!\n");
return;
}
printf("Please input the new schedule information:\n");
printf("id: ");
scanf("%d", &schedules[num_schedules].id);
printf("departure: ");
scanf("%s", schedules[num_schedules].departure);
printf("start: ");
scanf("%s", schedules[num_schedules].start);
printf("end: ");
scanf("%s", schedules[num_schedules].end);
printf("time: ");
scanf("%d", &schedules[num_schedules].time);
printf("capacity: ");
scanf("%d", &schedules[num_schedules].capacity);
schedules[num_schedules].booked = 0;
num_schedules++;
save_schedules();
printf("New schedule added successfully!\n");
}
// 显示所有班次信息
void show_schedules() {
printf("%-5s %-10s %-10s %-10s %-10s %-10s %-10s\n", "id", "departure", "start", "end", "time", "capacity", "booked");
for (int i = 0; i < num_schedules; i++) {
printf("%-5d %-10s %-10s %-10s %-10d %-10d %-10d", schedules[i].id, schedules[i].departure,
schedules[i].start, schedules[i].end, schedules[i].time,
schedules[i].capacity, schedules[i].booked);
time_t now;
time(&now);
struct tm *tm_now = localtime(&now);
char str_now[MAX_STR];
strftime(str_now, MAX_STR, "%H:%M", tm_now);
if (strcmp(schedules[i].departure, str_now) <= 0) {
printf(" (This schedule has already departed)");
}
printf("\n");
}
}
// 根据起点和终点查询班次信息
void query_schedule() {
char start[MAX_STR], end[MAX_STR];
printf("Please input the start and end stations:\n");
printf("start: ");
scanf("%s", start);
printf("end: ");
scanf("%s", end);
printf("%-5s %-10s %-10s %-10s %-10s %-10s %-10s\n", "id", "departure", "start", "end", "time", "capacity", "booked");
for (int i = 0; i < num_schedules; i++) {
if (strcmp(schedules[i].start, start) == 0 && strcmp(schedules[i].end, end) == 0) {
printf("%-5d %-10s %-10s %-10s %-10d %-10d %-10d\n", schedules[i].id, schedules[i].departure,
schedules[i].start, schedules[i].end, schedules[i].time,
schedules[i].capacity, schedules[i].booked);
}
}
}
int main() {
load_schedules();
while (1) {
printf("\nPlease select the operation:\n");
printf("1. Add a new schedule\n");
printf("2. Show all schedules\n");
printf("3. Query schedule by start and end stations\n");
printf("4. Exit\n");
int op;
scanf("%d", &op);
switch (op) {
case 1:
add_schedule();
break;
case 2:
show_schedules();
break;
case 3:
query_schedule();
break;
case 4:
return 0;
default:
printf("Error: invalid operation!\n");
}
}
return 0;
}
```
该程序使用结构体存储班次信息,可以进行添加、浏览、查询等操作。班次信息保存在文件`schedules.txt`中,程序运行时会自动读入文件内容。在显示班次信息时,如果当前系统时间超过了某个班次的发车时间,则会提示该班次已经发出。
阅读全文