用C语言写一个车票管理系统,要求录入班次信息(信息用文件保存),可以不定时的增加班次数据,浏览班次信息,可显示所有班次当前的状态(如果当前系统时间超过了某班次的发车时间,显示“此版已发出”的提示信息)
时间: 2024-03-06 18:52:14 浏览: 254
用C语言做出来的车票管理系统
以下是一个简单的车票管理系统的 C 代码示例,实现了录入班次信息、增加班次数据、浏览班次信息和显示班次状态的功能。班次信息存储在一个文本文件中,每行记录一个班次的信息,包括班次号、起点、终点、发车时间和票价等。具体实现细节可以根据需要进行修改和完善。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_LINE_LEN 100
#define MAX_SCHEDULES 100
struct Schedule {
char number[20];
char from[20];
char to[20];
char time[20];
int price;
};
// 读取班次信息
int read_schedules(struct Schedule schedules[]) {
FILE *fp = fopen("schedules.txt", "r");
if (fp == NULL) {
printf("failed to open file schedules.txt\n");
return 0;
}
int count = 0;
char line[MAX_LINE_LEN];
while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
if (count >= MAX_SCHEDULES) {
printf("maximum number of schedules reached\n");
break;
}
sscanf(line, "%s %s %s %s %d", schedules[count].number, schedules[count].from, schedules[count].to, schedules[count].time, &schedules[count].price);
count++;
}
fclose(fp);
return count;
}
// 保存班次信息
void save_schedules(struct Schedule schedules[], int count) {
FILE *fp = fopen("schedules.txt", "w");
if (fp == NULL) {
printf("failed to open file schedules.txt\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %s %s %s %d\n", schedules[i].number, schedules[i].from, schedules[i].to, schedules[i].time, schedules[i].price);
}
fclose(fp);
}
// 增加班次信息
void add_schedule(struct Schedule schedules[], int *count) {
if (*count >= MAX_SCHEDULES) {
printf("maximum number of schedules reached\n");
return;
}
printf("enter schedule number: ");
scanf("%s", schedules[*count].number);
printf("enter from: ");
scanf("%s", schedules[*count].from);
printf("enter to: ");
scanf("%s", schedules[*count].to);
printf("enter time (YYYY-MM-DD HH:MM): ");
scanf("%s", schedules[*count].time);
printf("enter price: ");
scanf("%d", &schedules[*count].price);
(*count)++;
save_schedules(schedules, *count);
}
// 浏览班次信息
void view_schedules(struct Schedule schedules[], int count) {
printf("schedule number\tfrom\tto\ttime\tprice\n");
for (int i = 0; i < count; i++) {
printf("%s\t\t%s\t%s\t%s\t%d\n", schedules[i].number, schedules[i].from, schedules[i].to, schedules[i].time, schedules[i].price);
}
}
// 显示班次状态
void show_schedule_status(struct Schedule schedule) {
struct tm now = *localtime(&(time_t){time(NULL)});
struct tm time = {0};
sscanf(schedule.time, "%d-%d-%d %d:%d", &time.tm_year, &time.tm_mon, &time.tm_mday, &time.tm_hour, &time.tm_min);
time.tm_year -= 1900;
time.tm_mon--;
time_t now_t = mktime(&now);
time_t time_t = mktime(&time);
if (now_t >= time_t) {
printf("schedule %s has departed\n", schedule.number);
} else {
printf("schedule %s is scheduled to depart at %s\n", schedule.number, schedule.time);
}
}
int main() {
struct Schedule schedules[MAX_SCHEDULES];
int count = read_schedules(schedules);
if (count == 0) {
return 1;
}
int choice;
do {
printf("\n1. add schedule\n2. view schedules\n3. show schedule status\n0. exit\n");
printf("enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_schedule(schedules, &count);
break;
case 2:
view_schedules(schedules, count);
break;
case 3:
printf("enter schedule number: ");
char number[20];
scanf("%s", number);
for (int i = 0; i < count; i++) {
if (strcmp(schedules[i].number, number) == 0) {
show_schedule_status(schedules[i]);
break;
}
if (i == count - 1) {
printf("schedule not found\n");
}
}
break;
case 0:
break;
default:
printf("invalid choice\n");
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文