数据结构车票管理系统的实验课设,用C语言来实现,要有完整源代码
时间: 2024-03-07 14:49:25 浏览: 238
c语言课程设计(车票管理系统)
4星 · 用户满意度95%
以下是一个简单的车票管理系统的实验课设,使用 C 语言编写,包括完整的源代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILENAME "tickets.txt" // 存储车票信息的文件名
// 车票结构体
typedef struct {
char train_num[10]; // 车次
char start_city[20]; // 起点城市
char end_city[20]; // 终点城市
char start_time[10]; // 出发时间
float price; // 票价
int remain_num; // 剩余数量
} Ticket;
// 添加车票信息
void add_ticket(Ticket *tickets, int *num_tickets) {
// 打开文件
FILE *fp = fopen(FILENAME, "a");
if (fp == NULL) {
printf("Open file failed!\n");
return;
}
// 输入车票信息
printf("Enter train number: ");
scanf("%s", tickets[*num_tickets].train_num);
printf("Enter start city: ");
scanf("%s", tickets[*num_tickets].start_city);
printf("Enter end city: ");
scanf("%s", tickets[*num_tickets].end_city);
printf("Enter start time: ");
scanf("%s", tickets[*num_tickets].start_time);
printf("Enter price: ");
scanf("%f", &tickets[*num_tickets].price);
printf("Enter remain number: ");
scanf("%d", &tickets[*num_tickets].remain_num);
// 写入文件
fprintf(fp, "%s %s %s %s %.2f %d\n", tickets[*num_tickets].train_num,
tickets[*num_tickets].start_city,
tickets[*num_tickets].end_city,
tickets[*num_tickets].start_time,
tickets[*num_tickets].price,
tickets[*num_tickets].remain_num);
// 关闭文件
fclose(fp);
// 更新票数
(*num_tickets)++;
}
// 删除车票信息
void delete_ticket(Ticket *tickets, int *num_tickets) {
// 打开文件
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
printf("Open file failed!\n");
return;
}
// 输入车次
char train_num[10];
printf("Enter train number: ");
scanf("%s", train_num);
// 读取文件
Ticket temp;
int i = 0, deleted = 0;
while (fscanf(fp, "%s %s %s %s %f %d", temp.train_num, temp.start_city,
temp.end_city, temp.start_time,
&temp.price, &temp.remain_num) != EOF) {
if (strcmp(temp.train_num, train_num) == 0) {
deleted = 1;
continue;
}
tickets[i++] = temp;
}
fclose(fp);
// 写入文件
fp = fopen(FILENAME, "w");
if (fp == NULL) {
printf("Open file failed!\n");
return;
}
for (int j = 0; j < i; j++) {
fprintf(fp, "%s %s %s %s %.2f %d\n", tickets[j].train_num,
tickets[j].start_city,
tickets[j].end_city,
tickets[j].start_time,
tickets[j].price,
tickets[j].remain_num);
}
fclose(fp);
// 更新票数
if (deleted) {
(*num_tickets)--;
printf("Delete ticket succeeded!\n");
} else {
printf("Train number not found!\n");
}
}
// 修改车票信息
void modify_ticket(Ticket *tickets, int num_tickets) {
// 打开文件
FILE *fp = fopen(FILENAME, "r+");
if (fp == NULL) {
printf("Open file failed!\n");
return;
}
// 输入车次
char train_num[10];
printf("Enter train number: ");
scanf("%s", train_num);
// 查找车票信息
Ticket temp;
int found = 0;
long offset = 0;
while (!found && fscanf(fp, "%s %s %s %s %f %d", temp.train_num, temp.start_city,
temp.end_city, temp.start_time,
&temp.price, &temp.remain_num) != EOF) {
if (strcmp(temp.train_num, train_num) == 0) {
found = 1;
} else {
offset = ftell(fp);
}
}
if (!found) {
printf("Train number not found!\n");
fclose(fp);
return;
}
// 修改车票信息
printf("Enter new start city (current: %s): ", temp.start_city);
scanf("%s", temp.start_city);
printf("Enter new end city (current: %s): ", temp.end_city);
scanf("%s", temp.end_city);
printf("Enter new start time (current: %s): ", temp.start_time);
scanf("%s", temp.start_time);
printf("Enter new price (current: %.2f): ", temp.price);
scanf("%f", &temp.price);
printf("Enter new remain number (current: %d): ", temp.remain_num);
scanf("%d", &temp.remain_num);
// 写入文件
fseek(fp, offset, SEEK_SET);
fprintf(fp, "%s %s %s %s %.2f %d\n", temp.train_num,
temp.start_city,
temp.end_city,
temp.start_time,
temp.price,
temp.remain_num);
// 关闭文件
fclose(fp);
printf("Modify ticket succeeded!\n");
}
// 查询车票信息
void query_ticket(Ticket *tickets, int num_tickets) {
// 打开文件
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
printf("Open file failed!\n");
return;
}
// 输入车次
char train_num[10];
printf("Enter train number: ");
scanf("%s", train_num);
// 查找车票信息
Ticket temp;
int found = 0;
while (!found && fscanf(fp, "%s %s %s %s %f %d", temp.train_num, temp.start_city,
temp.end_city, temp.start_time,
&temp.price, &temp.remain_num) != EOF) {
if (strcmp(temp.train_num, train_num) == 0) {
found = 1;
printf("Train Number: %s\n", temp.train_num);
printf("From: %s\n", temp.start_city);
printf("To: %s\n", temp.end_city);
printf("Departure time: %s\n", temp.start_time);
printf("Price: %.2f\n", temp.price);
printf("Remain number: %d\n", temp.remain_num);
}
}
if (!found) {
printf("Train number not found!\n");
}
// 关闭文件
fclose(fp);
}
// 显示车票信息
void display_tickets(Ticket *tickets, int num_tickets) {
// 打开文件
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
printf("Open file failed!\n");
return;
}
// 读取车票信息
Ticket temp;
int i = 0;
while (fscanf(fp, "%s %s %s %s %f %d", temp.train_num, temp.start_city,
temp.end_city, temp.start_time,
&temp.price, &temp.remain_num) != EOF) {
tickets[i++] = temp;
}
fclose(fp);
// 按照车次排序
for (int j = 0; j < num_tickets-1; j++) {
for (int k = 0; k < num_tickets-j-1; k++) {
if (strcmp(tickets[k].train_num, tickets[k+1].train_num) > 0) {
Ticket temp = tickets[k];
tickets[k] = tickets[k+1];
tickets[k+1] = temp;
}
}
}
// 显示车票信息
printf("Train Number\tFrom\t\tTo\t\tDeparture Time\tPrice\tRemain Number\n");
for (int j = 0; j < num_tickets; j++) {
printf("%-10s\t%-12s\t%-12s\t%-12s\t%-.2f\t%d\n", tickets[j].train_num,
tickets[j].start_city,
tickets[j].end_city,
tickets[j].start_time,
tickets[j].price,
tickets[j].remain_num);
}
}
int main() {
Ticket tickets[100];
int num_tickets = 0;
while (1) {
// 显示菜单
printf("\n");
printf("1. Add ticket\n");
printf("2. Delete ticket\n");
printf("3. Modify ticket\n");
printf("4. Query ticket\n");
printf("5. Display tickets\n");
printf("0. Exit\n");
// 选择操作
int op;
printf("Enter operation: ");
scanf("%d", &op);
switch (op) {
case 1:
add_ticket(tickets, &num_tickets);
break;
case 2:
delete_ticket(tickets, &num_tickets);
break;
case 3:
modify_ticket(tickets, num_tickets);
break;
case 4:
query_ticket(tickets, num_tickets);
break;
case 5:
display_tickets(tickets, num_tickets);
break;
case 0:
exit(0);
default:
printf("Invalid operation!\n");
}
}
return 0;
}
```
注意事项:
- 本程序使用了一个全局变量 `FILENAME`,表示存储车票信息的文件名,请确保程序运行目录下有该文件并有写入权限。
- 本程序最多支持存储 100 条车票信息,可以根据需要修改代码。
- 本程序没有进行异常处理,请确保输入的数据合法。
阅读全文