C-火车订票系统

it2023-10-20  64

C-火车订票系统

VS2017

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #pragma warning(disable:4996)

//定位坐标 void gotoxy(int x, int y) //goto语句 {     COORD pos;     pos.X = x - 1;     pos.Y = y - 1;     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }

//车票信息 typedef struct Ticket {     char T_num[10]; //车次     char T_origin[10]; //起点     char T_destination[10]; //终点     double T_fares; //票价     int T_tnum; //票数     struct Ticket *next; }ticket;

//乘客信息 typedef struct Passager {     int P_id; //ID     char P_name[10]; //姓名     char T_num[10]; //车次     char T_origin[10]; //起点     char T_destination[10]; //终点     int P_num; //订票数量     struct Passager *next;

}passager;

void T_destroy(ticket **T_p); //销毁链表 void T_destroyP(passager **T_p); //销毁链表passager void T_show(void); //主界面 void T_insert(void); //添加车票信息 void T_tshow(void); //显示车票信息 void T_bookt(void); //订车票 void T_showbookt(void); //查询订票信息 void T_mod(void); //修改车票信息

int main(void) {     T_show();     system("pause");     return 0; }

//销毁链表 void T_destroy(ticket **T_p) {     ticket *p = NULL;     ticket *T_temp = NULL;     p = *T_p;     while (NULL != p->next)     {         T_temp = p->next;         p->next = T_temp->next;         free(T_temp);         T_temp = NULL;     }     free(p);     p = NULL; }

//销毁链表 void T_destroyP(passager **T_p) {     passager *p = NULL;     passager *T_temp = NULL;     p = *T_p;     while (NULL != p->next)     {         T_temp = p->next;         p->next = T_temp->next;         free(T_temp);         T_temp = NULL;     }     free(p);     p = NULL; }

//主界面 void T_show(void) {     int i;     int num;     for (i = 0; i < 72; i++)     {         printf("+");     }     printf("\n");     for (i = 0; i < 30; i++)     {         printf(" ");     }     printf("火车订票系统\n");

    for (i = 0; i < 72; i++)     {         printf("+");     }     printf("\n");     printf("\n");     printf("\t\t\t  1 > 输入车票信息\n");     printf("\t\t\t  2 > 查询订票信息\n");     printf("\t\t\t  3 > 订票\n");     printf("\t\t\t  4 > 修改车票信息\n");     printf("\t\t\t  5 > 显示车票信息\n");     printf("\t\t\t  0 > 退出\n");     printf("\n");     for (i = 0; i < 72; i++)     {         printf("+");     }     printf("\n");     printf("请输入选项:");     scanf("%d", &num);     getchar(); //获取回车     switch (num)     {     case 1:         T_insert();         break;     case 2:         T_showbookt();         break;     case 3:         T_bookt();         break;     case 4:         T_mod();         break;     case 5:         T_tshow();         break;     case 0:         exit(1);         break;     default:         printf("请输入正确标签!\n");         break;     } }

//添加车票信息 void T_insert(void) {     ticket *T_p = NULL; //临时变量     FILE *pfile = NULL;     T_p = (ticket *)malloc(sizeof(ticket));     pfile = fopen("Ticket.txt", "a");     if (!pfile)     {         printf("打开文件失败!");         getchar();         exit(1);     }     printf("请输入车次:\n");     scanf("%s", T_p->T_num);     getchar();     printf("请输入起点:\n");     scanf("%s", T_p->T_origin);     getchar();     printf("请输入终点:\n");     scanf("%s", T_p->T_destination);     getchar();     printf("请输入票价:\n");     scanf("%12lf", &T_p->T_fares);     getchar();     printf("请输入票数:\n");     scanf("%12d", &T_p->T_tnum);     getchar();     fprintf_s(pfile, "%s%12s%12s%12.2lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, T_p->T_tnum);     printf("添加车票成功!\n");     fclose(pfile);     pfile = NULL;     free(T_p);     T_p = NULL; }

//显示车票信息 void T_tshow(void) {     int i;     ticket *T_head = NULL;     ticket *T_end = NULL;     ticket *T_p = NULL;     FILE *pfile = NULL;     int T_empty;     T_end = (ticket *)malloc(sizeof(ticket));     T_end->next = NULL;     T_head = (ticket *)malloc(sizeof(ticket));     T_head->next = T_end;     pfile = fopen("Ticket.txt", "r");     if (!pfile)     {         printf("读取文件失败!");         getchar();         exit(1);     }     T_p = (ticket *)malloc(sizeof(ticket));     //判断文件是否为空     T_empty = fscanf(pfile, "%s%12s%12s%12lf%12d", &T_p->T_num, &T_p->T_origin, &T_p->T_destination, &T_p->T_fares, &T_p->T_tnum);     if (EOF == T_empty)     {         printf("没有可查询的车票信息\n");         getchar();         exit(1);     }     while (EOF != T_empty)     {         T_p->next = T_end;         T_end = T_p;         T_head->next = T_end;         T_p = (ticket *)malloc(sizeof(ticket));         T_empty = fscanf(pfile, "%s%12s%12s%12lf%12d", &T_p->T_num, &T_p->T_origin, &T_p->T_destination, &T_p->T_fares, &T_p->T_tnum);     }     free(T_p);     T_p = NULL;     T_p = T_head;     printf("%s%12s%12s%12s%12s\n","车次","起点","终点","票价","票数");     while (T_p->next->next != NULL)     {         printf("%s%12s%12s%12.2lf%12d\n", T_p->next->T_num, T_p->next->T_origin, T_p->next->T_destination, T_p->next->T_fares, T_p->next->T_tnum);         T_p = T_p->next;     }     T_destroy(&T_head);     fclose(pfile);     pfile = NULL;     for (i = 0; i < 72; i++)     {         printf("+");     }     printf("\n");     printf("按任意键返回!\n");     getchar();     system("cls");     main(); }

//订车票 void T_bookt(void) {     int P_empty; //查空     int P_id = 0; //ID记录     ticket *T_p = NULL;     passager *P_p = NULL;     FILE *pfile = NULL;     FILE *T_pfile = NULL; //临时文件用     if (NULL == (pfile=fopen("Passenger.txt", "r")))     {         printf("读取文件失败!\n");         getchar();         exit(1);     }     P_p = (passager *)malloc(sizeof(passager));     P_empty = fscanf(pfile, "%d%12s%12s%12s%12s%12d", &P_p->P_id, &P_p->T_num, &P_p->T_origin, &P_p->T_destination, &P_p->P_name, &P_p->P_num);     //查空     if (EOF == P_empty)     {         P_p->P_id = ++P_id;     }     else     {         while (EOF != P_empty)         {             P_empty = fscanf(pfile, "%d%12s%12s%12s%12s%12d", &P_p->P_id, &P_p->T_num, &P_p->T_origin, &P_p->T_destination, &P_p->P_name, &P_p->P_num);         }         P_p->P_id++;     }     fclose(pfile);     pfile = NULL;     //写入文件订票信息     if (NULL == (pfile = fopen("Passenger.txt", "a")))     {         printf("订票失败!\n");         getchar();         exit(1);     }     printf("请输入订票信息:\n");     printf("请输入车次:\n");     scanf("%s", &P_p->T_num);     getchar();     printf("请输入起点:\n");     scanf("%s", &P_p->T_origin);     getchar();     printf("请输入终点:\n");     scanf("%s", &P_p->T_destination);     getchar();     printf("请输入姓名:\n");     scanf("%s", &P_p->P_name);     getchar();     printf("请输入票数:\n");     scanf("%12d", &P_p->P_num);     getchar();     fprintf(pfile, "%d%12s%12s%12s%12s%12d\n", P_p->P_id, P_p->T_num, P_p->T_origin, P_p->T_destination, P_p->P_name, P_p->P_num);     fclose(pfile);     pfile = NULL;     T_p = (ticket *)malloc(sizeof(ticket));     //读取车票信息,以做比对客户买的是什么票     if (NULL == (pfile = fopen("Ticket.txt", "r")))     {         printf("读取文件失败!\n");         getchar();         exit(1);     }     //打开临时文件     if (NULL == (T_pfile = fopen("T-ticket.txt", "w")))     {         printf("写入文件失败!\n");         getchar();         exit(1);     }     while (EOF != fscanf(pfile, "%s%12s%12s%12lf%12d", &T_p->T_num, &T_p->T_origin, &T_p->T_destination, &T_p->T_fares, &T_p->T_tnum))     {         //判断找出乘客输入的车票         if ((strcmp(T_p->T_num, P_p->T_num) == 0 && strcmp(T_p->T_origin, P_p->T_origin) == 0 && strcmp(T_p->T_destination, P_p->T_destination) == 0))         {             //将读取的车票信息写入临时文件,并且减掉乘客买的车票数             fprintf_s(T_pfile, "%s%12s%12s%12.2lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, (T_p->T_tnum) - (P_p->P_num));             printf("%s%12s%12s%12s%12s%12s\n", "ID","车次", "起点", "终点", "姓名", "票数");             printf("%d%12s%12s%12s%12s%12d\n", P_p->P_id, P_p->T_num, P_p->T_origin, P_p->T_destination, P_p->P_name, P_p->P_num);             printf("订票成功!\n");             printf("总金额为:%.2lf元\n",T_p->T_fares*P_p->P_num);             continue;             getchar();         }         fprintf_s(T_pfile, "%s%12s%12s%12.2lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, T_p->T_tnum);     }     free(P_p);     P_p = NULL;     free(T_p);     T_p = NULL;     fclose(pfile);     pfile = NULL;     fclose(T_pfile);     T_pfile = NULL;     remove("Ticket.txt");     rename("C:\\CODE\\火车订票系统\\火车订票系统\\火车订票系统\\T-ticket.txt", "C:\\CODE\\火车订票系统\\火车订票系统\\火车订票系统\\ticket.txt");     getchar();     getchar();     system("cls");     main(); }

//修改车票信息 void T_mod(void) {     FILE *T_pfile = NULL;     FILE *pfile = NULL;     ticket *T_p = NULL;     char T_modnum[10];     T_p = (ticket *)malloc(sizeof(ticket));     if (NULL == (pfile = fopen("Ticket.txt", "r")))     {         printf("读取文件失败!\n");         getchar();         exit(1);     }     if (NULL == (T_pfile = fopen("T-ticket.txt", "w")))     {         printf("写入文件失败!\n");         getchar();         exit(1);     }     printf("请输入需要修改的车次:");     scanf("%s", &T_modnum);     while (EOF != fscanf(pfile, "%s%12s%12s%12lf%12d", &T_p->T_num, &T_p->T_origin, &T_p->T_destination, &T_p->T_fares, &T_p->T_tnum))     {         if (strcmp(T_modnum, T_p->T_num) == 0)         {             printf("%s%12s%12s%12s%12s\n", "车次", "起点", "终点", "票价", "票数");             printf("%s%12s%12s%12.2lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, T_p->T_tnum);             printf("以下需要修改为:\n");             printf("请输入车次:\n");             scanf("%s", T_p->T_num);             getchar();             printf("请输入起点:\n");             scanf("%s", T_p->T_origin);             getchar();             printf("请输入终点:\n");             scanf("%s", T_p->T_destination);             getchar();             printf("请输入票价:\n");             scanf("%12lf", &T_p->T_fares);             getchar();             printf("请输入票数:\n");             scanf("%12d", &T_p->T_tnum);             getchar();             printf("%s%12s%12s%12s%12s\n", "车次", "起点", "终点", "票价", "票数");              fprintf(T_pfile, "%s%12s%12s%12lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, T_p->T_tnum);             printf("%s%12s%12s%12.2lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, T_p->T_tnum);             printf("修改成功!\n");             printf("按任意键返回菜单!\n");             getch();             continue;         }         fprintf(T_pfile, "%s%12s%12s%12lf%12d\n", T_p->T_num, T_p->T_origin, T_p->T_destination, T_p->T_fares, T_p->T_tnum);     }     free(T_p);     T_p = NULL;     fclose(pfile);     pfile = NULL;     fclose(T_pfile);     T_pfile = NULL;     remove("Ticket.txt");     rename("C:\\CODE\\火车订票系统\\火车订票系统\\火车订票系统\\T-ticket.txt", "C:\\CODE\\火车订票系统\\火车订票系统\\火车订票系统\\Ticket.txt");     system("cls");     main(); }

//显示订票信息 void T_showbookt(void) {     int i;     passager *T_head = NULL;     passager *T_end = NULL;     passager *T_p = NULL;     passager *H_p = NULL;     FILE *pfile = NULL;     int T_empty;     T_end = (passager *)malloc(sizeof(passager));     T_end->next = NULL;     T_head = (passager *)malloc(sizeof(passager));     T_head->next = T_end;     H_p = T_head;     pfile = fopen("Passenger.txt", "r");     if (!pfile)     {         printf("读取文件失败!");         getchar();         exit(1);     }     T_p = (passager *)malloc(sizeof(passager));     //判断文件是否为空     T_empty = fscanf(pfile, "%d%12s%12s%12s%12s%12d", &T_p->P_id, &T_p->T_num, &T_p->T_origin, &T_p->T_destination, &T_p->P_name, &T_p->P_num);     if (EOF == T_empty)     {         printf("没有可查询的车票信息\n");         getchar();         exit(1);     }     while (EOF != T_empty)     {         H_p->next = T_p;         T_p->next = T_end;         H_p = H_p->next;         T_p = (passager *)malloc(sizeof(passager));         T_empty = fscanf(pfile, "%d%12s%12s%12s%12s%12d", &T_p->P_id, &T_p->T_num, &T_p->T_origin, &T_p->T_destination, &T_p->P_name, &T_p->P_num);     }     free(T_p);     T_p = NULL;     T_p = T_head;     printf("%s%12s%12s%12s%12s%12s\n", "ID", "车 次", "起 点", "终 点", "票 价", "票 数");     while (T_p->next->next != NULL)     {         printf("%2d%12s%12s%12s%12s%12d\n", T_p->next->P_id, T_p->next->T_num, T_p->next->T_origin, T_p->next->T_destination, T_p->next->P_name, T_p->next->P_num);         T_p = T_p->next;     }     T_destroyP(&T_head);     fclose(pfile);     pfile = NULL;     for (i = 0; i < 72; i++)     {         printf("+");     }     printf("\n");     printf("按任意键返回!\n");     getchar();     system("cls");     main(); }

最新回复(0)