我的C语言课程设计时间:2022-05-16 00:21:12Admin.h #ifndef ADMIN_H #define ADMIN_H #define ACCOUNTSIZE 21 //规定账号最大长度 #define PASSWORDSIZE 21 //规定密码最大长度 typedef struct Admin { char account[ACCOUNTSIZE]; char password[PASSWORDSIZE]; }Admin; typedef struct AdminNode { Admin data; struct AdminNode *next; }AdminNode, *AdminList; void InitAdminList(AdminList *L); //初始化管理员链表 void DestroyAdminList(AdminList *L); //销毁管理员链表 void InsAdminList(AdminList L); //增加管理员 void DelAdminList(AdminList L); //删除管理员 void ShowAdminList(AdminList L); //显示管理员清单 void InitAdminFile(); //初始化管理员文件 void WriteAdminFile(AdminList L); //将内存中的信息写入Admin.dat void ReadAdminFile(AdminList L); //从Admin.dat中读取信息 void ChangePassword(AdminNode *CurrentUser); //更改管理员密码 #endif Admin.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Admin.h" void InitAdminList(AdminList *L) { (*L) = (AdminNode *)malloc(sizeof(AdminNode)); (*L)->next = NULL; } void DestroyAdminList(AdminList *L) { AdminNode *lpNodeDel; lpNodeDel = (*L)->next; while (lpNodeDel != NULL) { (*L)->next = lpNodeDel->next; free(lpNodeDel); lpNodeDel = (*L)->next; } lpNodeDel = (*L); free(lpNodeDel); (*L) = NULL; } void InsAdminList(AdminList L) { char szAccount[ACCOUNTSIZE] = ""; char szPassword[PASSWORDSIZE] = ""; char szConfirm[PASSWORDSIZE] = ""; AdminNode *lpNodeIns; AdminNode *lpNodePoint; printf("/n"); printf("请输入账号:"); scanf("%s", szAccount); printf("请输入密码:"); scanf("%s", szPassword); printf("请再次输入密码:"); scanf("%s", szConfirm); if (strcmp(szPassword, szConfirm) != 0) { printf("两次密码不同!/n/n"); return; } lpNodeIns = (AdminNode *)malloc(sizeof(AdminNode)); strcpy(lpNodeIns->data.account, szAccount); strcpy(lpNodeIns->data.password, szPassword); lpNodePoint = L; while (lpNodePoint->next != NULL && strcmp(lpNodePoint->next->data.account, lpNodeIns->data.account) < 0) lpNodePoint = lpNodePoint->next; if (lpNodePoint->next == NULL) { lpNodeIns->next = lpNodePoint->next; lpNodePoint->next = lpNodeIns; printf("账号创建成功!/n/n"); return; } else if (strcmp(lpNodePoint->next->data.account, lpNodeIns->data.account) == 0) { printf("账号已存在!/n/n"); return; } else { lpNodeIns->next = lpNodePoint->next; lpNodePoint->next = lpNodeIns; printf("账号创建成功!/n/n"); return; } } void DelAdminList(AdminList L) { char szAccount[ACCOUNTSIZE]; AdminNode *lpNodePoint; AdminNode *lpNodeDel; printf("/n"); printf("请输入要删除的账号:"); scanf("%s", szAccount); lpNodePoint = L; while (lpNodePoint->next != NULL && strcmp(lpNodePoint->next->data.account, szAccount) < 0) lpNodePoint = lpNodePoint->next; if (lpNodePoint->next == NULL || strcmp(lpNodePoint->next->data.account, szAccount) > 0) { printf("无此账号!/n/n"); return; } else { lpNodeDel = lpNodePoint->next; lpNodePoint->next = lpNodeDel->next; free(lpNodeDel); printf("删除成功!/n/n"); return; } } void ShowAdminList(AdminList L) { AdminNode *lpNodePoint; printf("/n"); printf("用户有:/n"); lpNodePoint = L->next; while (lpNodePoint != NULL) { printf("%s/n", lpNodePoint->data.account); lpNodePoint = lpNodePoint->next; } printf("/n"); } void InitAdminFile() { FILE *fpWrite; Admin admin; strcpy(admin.account, "admin"); strcpy(admin.password, "123456"); fpWrite = fopen("Admin.dat", "wb"); // fprintf(fpWrite, "%s/n%s/n", "admin", "123456"); fprintf(fpWrite, "%d/n", 1); fwrite(&admin, sizeof(admin), 1, fpWrite); fclose(fpWrite); } void WriteAdminFile(AdminList L) { AdminNode *lpNodePoint; FILE *fpWrite; int iNum = 0; int nI = 0; fpWrite = fopen("Admin.dat", "wb"); lpNodePoint = L->next; while (lpNodePoint != NULL) { iNum++; lpNodePoint = lpNodePoint->next; } fprintf(fpWrite, "%d/n", iNum); lpNodePoint = L->next; while (nI < iNum) { // fprintf(fpWrite, "%s/n%s/n", lpNodePoint->data.account, lpNodePoint->data.password); fwrite(&(lpNodePoint->data), sizeof(lpNodePoint->data), 1, fpWrite); lpNodePoint = lpNodePoint->next; nI++; } fclose(fpWrite); } void ReadAdminFile(AdminList L) { FILE *fpRead; AdminNode *lpNodeIns; int iNum = 0; int nI = 0; lpNodeIns = L; fpRead = fopen("Admin.dat", "rb"); fscanf(fpRead, "%d/n", &iNum); while (nI < iNum) { lpNodeIns->next = (AdminNode *)malloc(sizeof(AdminNode)); lpNodeIns = lpNodeIns->next; // fscanf(fpRead, "%s/n%s/n", lpNodeIns->data.account, lpNodeIns->data.password); fread(&(lpNodeIns->data), sizeof(lpNodeIns->data), 1, fpRead); nI++; } lpNodeIns->next = NULL; fclose(fpRead); } void ChangePassword(AdminNode *CurrentUser) { char szPassword[PASSWORDSIZE] = ""; char szConfirm[PASSWORDSIZE] = ""; printf("/n"); printf("请输入新密码:"); scanf("%s", szPassword); printf("请再次输入密码:"); scanf("%s", szConfirm); if (strcmp(szPassword, szConfirm) == 0) { strcpy(CurrentUser->data.password, szPassword); printf("密码修改成功!/n/n"); } else printf("两次密码输入不同!/n/n"); } Menu.h #include "Admin.h" void MainMenu(); //主菜单 void UserMenu(); //用户菜单 void PasswordMenu(); //登陆菜单 void SysAdminMenu(AdminList UserData, AdminNode *CurrentUser); //系统管理员菜单 void AdminMenu(AdminNode *CurrentUser); //普通管理员菜单 Menu.c #include <stdio.h> #include <string.h> #include <windows.h> #include "Admin.h" #include "Menu.h" #include "Train.h" void MainMenu() { int iChoose; while (1) { printf("/n"); printf("请选择你的身份:/n"); printf("1、用户/n"); printf("2、管理员/n"); printf("0、退出/n"); printf("请输入你的选择:"); scanf("%d%*c", &iChoose); switch (iChoose) { case 1: UserMenu(); break; case 2: PasswordMenu(); break; default: return; } } } void UserMenu() { int iChoose; char chChoose; TrainList AllData; TrainList UseData; InitTrainList(&AllData); InitTrainList(&UseData); ReadTrainFile(AllData); while (1) { printf("/n"); UseData = SrcToDest(AllData); printf("请输入排序顺序:/n"); printf("1、按发车时间排序/n"); printf("2、按所用金额排序/n"); printf("3、按列车号排序/n"); printf("请输入你的选择:"); scanf("%d%*c", &iChoose); switch (iChoose) { case 1: SortTrainListTime(UseData); break; case 2: SortTrainListMoney(UseData); break; case 3: SortTrainListNum(UseData); break; default: DestroyTrainList(&AllData); DestroyTrainList(&UseData); return; } ShowTrainList(UseData); DestroyTrainList(&UseData); InitTrainList(&UseData); printf("是否继续(y/n):"); scanf("%c%*c", &chChoose); if (chChoose == 'n') break; } DestroyTrainList(&AllData); DestroyTrainList(&UseData); } void PasswordMenu() { char szAccount[ACCOUNTSIZE]; char szPassword[PASSWORDSIZE]; AdminList UserData; AdminNode *lpNodePoint; printf("/n"); InitAdminList(&UserData); ReadAdminFile(UserData); printf("请输入用户名:"); scanf("%s", szAccount); printf("请输入密码:"); scanf("%s", szPassword); lpNodePoint = UserData->next; while (lpNodePoint != NULL && strcmp(lpNodePoint->data.account, szAccount) < 0) lpNodePoint = lpNodePoint->next; if (lpNodePoint == NULL || strcmp(lpNodePoint->data.account, szAccount) > 0) { printf("无此用户!/n"); return; } else if (strcmp(lpNodePoint->data.password ,szPassword) != 0) { printf("密码错误!/n"); return; } else { if (strcmp(szAccount, "admin") == 0) SysAdminMenu(UserData, lpNodePoint); else AdminMenu(lpNodePoint); } DestroyAdminList(&UserData); } void SysAdminMenu(AdminList UserData, AdminNode *CurrentUser) { int iChoose; TrainList AllData; InitTrainList(&AllData); ReadTrainFile(AllData); while (1) { printf("/n"); printf("请选择功能:/n"); printf("1、查看所有管理员/n"); printf("2、添加管理员/n"); printf("3、删除管理员/n"); printf("4、手动添加列车信息/n"); printf("5、从文件中添加列车信息/n"); printf("6、删除列车信息/n"); printf("7、查看所有火车车次/n"); printf("8、查看某一火车的详细信息/n"); printf("9、更改密码/n"); printf("0、注销/n"); printf("请输入你的选择:"); scanf("%d%*c", &iChoose); switch (iChoose) { case 1: ShowAdminList(UserData); break; case 2: InsAdminList(UserData); break; case 3: DelAdminList(UserData); break; case 4: InsTrainList(AllData); break; case 5: ReadFromFile(AllData); break; case 6: DelTrainList(AllData); break; case 7: ShowTrainNum(AllData); break; case 8: ShowOneTrain(AllData); break; case 9: ChangePassword(CurrentUser); break; default: WriteAdminFile(UserData); WriteTrainFile(AllData); DestroyTrainList(&AllData); return; } } } void AdminMenu(AdminNode *CurrentUser) { int iChoose; TrainList AllData; InitTrainList(&AllData); ReadTrainFile(AllData); while (1) { printf("/n"); printf("请选择功能:/n"); printf("1、手动添加列车信息/n"); printf("2、从文件中添加列车信息/n"); printf("3、删除列车信息/n"); printf("4、查看所有火车车次/n"); printf("5、查看某一火车的详细信息/n"); printf("6、更改密码/n"); printf("0、注销/n"); printf("请输入你的选择:"); scanf("%d%*c", &iChoose); switch (iChoose) { case 1: InsTrainList(AllData); break; case 2: ReadFromFile(AllData); break; case 3: DelTrainList(AllData); break; case 4: ShowTrainNum(AllData); break; case 5: ShowOneTrain(AllData); break; case 6: ChangePassword(CurrentUser); break; default: WriteTrainFile(AllData); DestroyTrainList(&AllData); return; } } } Time.h #ifndef TIME_H #define TIME_H typedef struct Time { int day; int hour; int min; }Time; void OutputTime(Time T); //输出时间 //Time TimeLast(Time TSrc, Time TDest); int TimeCmp(Time TSrc, Time TDest); //比较两个时间 #endif Time.c #include <stdio.h> #include "Time.h" //char szDay[][3] = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}; void OutputTime(Time T) {/* if (T.day == 0) printf("当天"); else printf("第%s天", szDay[T.day]); */ printf("%d:%d", T.hour, T.min); } int TimeCmp(Time TSrc, Time TDest) { if (TSrc.day != TDest.day) return (TSrc.day - TDest.day); else if (TSrc.hour != TDest.hour) return (TSrc.hour - TDest.hour); else return (TSrc.min - TDest.min); } Train.h #ifndef TRAIN_H #define TRAIN_H #include "time.h" #define TRAINNUMSIZE 21 #define CITYNAMESIZE 21 typedef struct Train { char city[CITYNAMESIZE]; int money; Time time; struct Train *next; }Train; typedef struct TrainNode { char num[TRAINNUMSIZE]; int money; Time timeSrc; Time timeDest; Train data; struct TrainNode *next; }TrainNode, *TrainList; void InitTrainList(TrainList *L); //初始化列车链表 void DestroyTrainList(TrainList *L); //销毁列车链表 void InsTrainList(TrainList L); //增加列车信息 void DelTrainList(TrainList L); //删除列车信息 void ShowTrainList(TrainList L); //显示用户的查询结果 void SortTrainListNum(TrainList L); //将链表按车次排序 void SortTrainListMoney(TrainList L); //将链表按价格排序 void SortTrainListTime(TrainList L); //将链表按时间排序 void InitTrainFile(); //初始化列车文件 void WriteTrainFile(TrainList L); //将内存中信息写入Train.dat void ReadTrainFile(TrainList L); //从Train.dat中读取信息 void ReadFromFile(TrainList L); //从文件中输入信息 void ShowTrainNum(TrainList L); //显示已有的列车车次 void ShowOneTrain(TrainList L); //显示一列列车的详细信息 TrainList SrcToDest(TrainList L); //建立用户查询结果的链表 #endif Train.c #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Train.h" void InitTrainList(TrainList *L) { (*L) = (TrainNode *)malloc(sizeof(TrainNode)); (*L)->next = NULL; } void DestroyTrainList(TrainList *L) { TrainNode *lpNodeDel; lpNodeDel = (*L)->next; while (lpNodeDel != NULL) { (*L)->next = lpNodeDel->next; free(lpNodeDel); lpNodeDel = (*L)->next; } lpNodeDel = (*L); free(lpNodeDel); (*L) = NULL; } void InsTrainList(TrainList L) { char chTemp; TrainNode *lpNodeIns; Train *lpTrainIns; lpNodeIns = (TrainNode *)malloc(sizeof(TrainNode)); lpNodeIns->next = L->next; L->next = lpNodeIns; lpTrainIns = &(lpNodeIns->data); printf("/n"); printf("请输入列车号:"); scanf("%s", lpNodeIns->num); lpTrainIns->time.hour = 0; lpTrainIns->time.min = 0; while (1) { lpTrainIns->next = (Train *)malloc(sizeof(Train)); printf("请输入城市名:"); scanf("%s", lpTrainIns->next->city); lpTrainIns->next->time.day = 0; printf("请输入发车时间(到达时间)(时:分):"); scanf("%d:%d%*c", &(lpTrainIns->next->time.hour), &(lpTrainIns->next->time.min)); if (lpTrainIns->time.hour > lpTrainIns->next->time.hour) lpTrainIns->next->time.day++; printf("请输入起始站到该站的金额:"); scanf("%d%*c", &lpTrainIns->next->money); lpTrainIns = lpTrainIns->next; printf("是否继续输入(y/n):"); scanf("%c%*c", &chTemp); if (chTemp == 'n') break; } lpTrainIns->next = NULL; printf("/n"); } void DelTrainList(TrainList L) { char szNum[TRAINNUMSIZE] = ""; TrainNode *lpNodePoint; TrainNode *lpNodeDel; Train *lpTrainPoint; Train *lpTrainDel; printf("/n"); printf("请输入列车号:"); scanf("%s", szNum); lpNodePoint = L; while (lpNodePoint->next != NULL && strcmp(lpNodePoint->next->num, szNum) != 0) lpNodePoint = lpNodePoint->next; if (lpNodePoint->next == NULL) { printf("无该列车!/n/n"); return; } lpNodeDel = lpNodePoint->next; lpNodePoint->next = lpNodeDel->next; lpTrainPoint = &(lpNodeDel->data); lpTrainDel = lpTrainPoint->next; while (lpTrainDel != NULL) { lpTrainPoint->next = lpTrainDel->next; free(lpTrainDel); lpTrainDel = lpTrainPoint->next; } free(lpNodeDel); printf("删除成功!/n/n"); } void SortTrainListNum(TrainList L) { TrainNode *lpNodeFront; TrainNode *lpNodeMin; TrainNode *lpNodePoint; TrainNode *lpNodePre; lpNodeFront = L; while (lpNodeFront->next != NULL) { lpNodeMin = lpNodeFront->next; lpNodePoint = lpNodeFront; lpNodePre = lpNodeFront; while (lpNodePoint->next != NULL) { if (strcmp(lpNodePoint->next->num, lpNodeMin->num) < 0) { lpNodePre = lpNodePoint; lpNodeMin = lpNodePoint->next; } lpNodePoint = lpNodePoint->next; } if (lpNodePre == lpNodeFront) { lpNodeFront = lpNodeFront->next; continue; } lpNodePoint = lpNodeFront->next; lpNodeFront->next = lpNodeMin; lpNodePre->next = lpNodeMin->next; lpNodeMin->next = lpNodePoint; } } void SortTrainListMoney(TrainList L) { TrainNode *lpNodeFront; TrainNode *lpNodeMin; TrainNode *lpNodePoint; TrainNode *lpNodePre; lpNodeFront = L; while (lpNodeFront->next != NULL) { lpNodeMin = lpNodeFront->next; lpNodePoint = lpNodeFront; lpNodePre = lpNodeFront; while (lpNodePoint->next != NULL) { if (lpNodePoint->next->data.money < lpNodeMin->data.money) { lpNodePre = lpNodePoint; lpNodeMin = lpNodePoint->next; } lpNodePoint = lpNodePoint->next; } if (lpNodePre == lpNodeFront) { lpNodeFront = lpNodeFront->next; continue; } lpNodePoint = lpNodeFront->next; lpNodeFront->next = lpNodeMin; lpNodePre->next = lpNodeMin->next; lpNodeMin->next = lpNodePoint; } } void SortTrainListTime(TrainList L) { TrainNode *lpNodeFront; TrainNode *lpNodeMin; TrainNode *lpNodePoint; TrainNode *lpNodePre; lpNodeFront = L; while (lpNodeFront->next != NULL) { lpNodeMin = lpNodeFront->next; lpNodePoint = lpNodeFront; lpNodePre = lpNodeFront; while (lpNodePoint->next != NULL) { if (TimeCmp(lpNodePoint->next->timeSrc, lpNodeMin->timeSrc) < 0) { lpNodePre = lpNodePoint; lpNodeMin = lpNodePoint->next; } lpNodePoint = lpNodePoint->next; } if (lpNodePre == lpNodeFront) { lpNodeFront = lpNodeFront->next; continue; } lpNodePoint = lpNodeFront->next; lpNodeFront->next = lpNodeMin; lpNodePre->next = lpNodeMin->next; lpNodeMin->next = lpNodePoint; } } void ShowTrainList(TrainList L) { TrainNode *lpNodePoint; lpNodePoint = L->next; if (lpNodePoint == NULL) { printf("/n暂无信息!/n/n"); return; } printf("/n"); while (lpNodePoint != NULL) { printf("列车%s:花费%d元,", lpNodePoint->num, lpNodePoint->money); OutputTime(lpNodePoint->timeSrc); printf("出发,"); OutputTime(lpNodePoint->timeDest); printf("到达。/n"); lpNodePoint = lpNodePoint->next; } printf("/n"); } void InitTrainFile() { FILE *fpWrite; fpWrite = fopen("Train.dat", "wb"); fprintf(fpWrite, "%d", 0); fclose(fpWrite); } void WriteTrainFile(TrainList L) { FILE *fpWrite; TrainNode *lpNodePoint; Train *lpTrainPoint; int iTrainNum; int iCityNum; int cI, cJ; lpNodePoint = L->next; iTrainNum = 0; while (lpNodePoint != NULL) { iTrainNum++; lpNodePoint = lpNodePoint->next; } fpWrite = fopen("Train.dat", "wb"); lpNodePoint = L->next; fprintf(fpWrite, "%d/n", iTrainNum); for (cI = 0; cI < iTrainNum; cI++) { lpTrainPoint = &(lpNodePoint->data); lpTrainPoint = lpTrainPoint->next; iCityNum = 0; while (lpTrainPoint != NULL) { iCityNum++; lpTrainPoint = lpTrainPoint->next; } fprintf(fpWrite, "%s/n%d/n", lpNodePoint->num, iCityNum); lpTrainPoint = &(lpNodePoint->data); lpTrainPoint = lpTrainPoint->next; for (cJ = 0; cJ < iCityNum; cJ++) { /* fprintf(fpWrite, "%s/n%d/n%d/n%d/n%d/n", lpTrainPoint->city, lpTrainPoint->money, / lpTrainPoint->time.day, lpTrainPoint->time.hour, lpTrainPoint->time.min); */ fwrite(lpTrainPoint, sizeof(*lpTrainPoint), 1, fpWrite); lpTrainPoint = lpTrainPoint->next; } lpNodePoint = lpNodePoint->next; } fclose(fpWrite); } void ReadTrainFile(TrainList L) { FILE *fpRead; TrainNode *lpNodeIns; Train *lpTrainIns; int iTrainNum; int iCityNum; int cI, cJ; lpNodeIns = L; fpRead = fopen("Train.dat", "rb"); fscanf(fpRead, "%d/n", &iTrainNum); for (cI = 0; cI < iTrainNum; cI++) { lpNodeIns->next = (TrainNode *)malloc(sizeof(TrainNode)); lpNodeIns = lpNodeIns->next; lpTrainIns = &(lpNodeIns->data); fscanf(fpRead, "%s/n", lpNodeIns->num); fscanf(fpRead, "%d/n", &iCityNum); for (cJ = 0; cJ < iCityNum; cJ++) { lpTrainIns->next = (Train *)malloc(sizeof(Train)); /* fscanf(fpRead, "%s/n%d/n%d/n%d/n%d/n", lpTrainIns->next->city, &(lpTrainIns->next->money), / &(lpTrainIns->next->time.day), &(lpTrainIns->next->time.hour), &(lpTrainIns->next->time.min)); */ fread(lpTrainIns->next, sizeof(*lpTrainIns), 1, fpRead); lpTrainIns = lpTrainIns->next; } lpTrainIns->next = NULL; } lpNodeIns->next = NULL; fclose(fpRead); } void ShowTrainNum(TrainList L) { TrainNode *lpNodePoint = L->next; printf("/n"); if (lpNodePoint == NULL) { printf("无列车!/n/n"); return; } printf("现在所有的列车为:/n"); while (lpNodePoint != NULL) { printf("%s/n", lpNodePoint->num); lpNodePoint = lpNodePoint->next; } printf("/n"); } void ShowOneTrain(TrainList L) { char szNum[TRAINNUMSIZE]; TrainNode *lpNodePoint; Train *lpTrainPoint; printf("/n"); printf("请输入要显示的列车号:"); scanf("%s", szNum); lpNodePoint = L->next; while (lpNodePoint != NULL) { if (strcmp(lpNodePoint->num, szNum) == 0) break; lpNodePoint = lpNodePoint->next; } if (lpNodePoint == NULL) { printf("无该列车!/n/n"); return; } lpTrainPoint = &(lpNodePoint->data); lpTrainPoint = lpTrainPoint->next; while (lpTrainPoint != NULL) { if (strlen(lpTrainPoint->city) < 8) { printf("%s/t/t%d:%d/t/t%d元/n", lpTrainPoint->city, lpTrainPoint->time.hour, lpTrainPoint->time.min, lpTrainPoint->money); } else { printf("%s/t%d:%d/t/t%d元/n", lpTrainPoint->city, lpTrainPoint->time.hour, lpTrainPoint->time.min, lpTrainPoint->money); } lpTrainPoint = lpTrainPoint->next; } printf("/n"); } TrainList SrcToDest(TrainList L) { char szSrc[CITYNAMESIZE] = ""; char szDest[CITYNAMESIZE] = ""; TrainNode *lpNodePoint; TrainNode *lpNodeInsHead; TrainNode *lpNodeIns; Train *lpTrainPoint; int bFlag = 0; lpNodeIns = lpNodeInsHead = (TrainNode *)malloc(sizeof(TrainNode)); printf("请输入起始地:"); scanf("%s", szSrc); printf("请输入目的地:"); scanf("%s", szDest); lpNodePoint = L->next; while (lpNodePoint != NULL) { bFlag = 0; lpTrainPoint = &(lpNodePoint->data); lpTrainPoint = lpTrainPoint->next; while (lpTrainPoint != NULL) { if (strcmp(lpTrainPoint->city, szSrc) == 0) { lpNodeIns->next = (TrainNode *)malloc(sizeof(TrainNode)); strcpy(lpNodeIns->next->num, lpNodePoint->num); // lpNodeIns->next->timeSrc.day = lpTrainPoint->time.day; lpNodeIns->next->timeSrc.hour = lpTrainPoint->time.hour; lpNodeIns->next->timeSrc.min = lpTrainPoint->time.min; lpNodeIns->next->money = lpTrainPoint->money; bFlag = 1; } else if (bFlag) { if (strcmp(lpTrainPoint->city, szDest) == 0) { lpNodeIns->next->money = lpTrainPoint->money - lpNodeIns->next->money; // lpNodeIns->next->timeDest.day = lpTrainPoint->time.day; lpNodeIns->next->timeDest.hour = lpTrainPoint->time.hour; lpNodeIns->next->timeDest.min = lpTrainPoint->time.min; lpNodeIns = lpNodeIns->next; break; } } lpTrainPoint = lpTrainPoint->next; } if (bFlag && lpTrainPoint == NULL) free(lpNodeIns->next); lpNodePoint = lpNodePoint->next; } lpNodeIns->next = NULL; return lpNodeInsHead; } void ReadFromFile(TrainList L) { FILE *fpRead; char szFilePath[81]; TrainNode *lpNodeIns; Train *lpTrainIns; int iSize; int nI; printf("/n请输入要录用的文件名:"); scanf("%s", szFilePath); if ((fpRead = fopen(szFilePath, "r")) == NULL) { printf("无法打开该文件,请检查文件路径是否正确!/n"); getch(); exit(1); } lpNodeIns = (TrainNode *)malloc(sizeof(TrainNode)); lpTrainIns = &(lpNodeIns->data); fscanf(fpRead, "%s/n%d/n", lpNodeIns->num, &iSize); for (nI = 0; nI < iSize; nI++) { lpTrainIns->next = (Train *)malloc(sizeof(Train)); fscanf(fpRead, "%s %d:%d %d/n", lpTrainIns->next->city, &(lpTrainIns->next->time.hour), &(lpTrainIns->next->time.min), &(lpTrainIns->next->money)); lpTrainIns = lpTrainIns->next; } lpTrainIns->next = NULL; lpNodeIns->next = L->next; L->next = lpNodeIns; printf("文件录用成功!/n/n"); fclose(fpRead); } TrainSystem.c #include <conio.h> #include <stdio.h> #include <stdlib.h> #include "Admin.h" #include "Train.h" #include "Menu.h" int main() { FILE *fpReadTrain; FILE *fpReadAdmin; int bBool = 0; if ((fpReadTrain = fopen("Train.dat", "rb")) == NULL) { bBool = 1; InitTrainFile(); } if ((fpReadAdmin = fopen("Admin.dat", "rb")) == NULL) { bBool = 1; InitAdminFile(); } if (bBool) { printf("有文件尚未初始化,请重启程序后继续!/n"); printf("初始管理员账号为:admin,密码为:123456/n"); printf("按任意键结束..."); getch(); exit(1); } fclose(fpReadTrain); fclose(fpReadAdmin); MainMenu(); return 0; }