收藏 分享(赏)

C语言课程设计火车票系统源代码.doc

上传人:精品资料 文档编号:8589730 上传时间:2019-07-04 格式:DOC 页数:11 大小:22.56KB
下载 相关 举报
C语言课程设计火车票系统源代码.doc_第1页
第1页 / 共11页
C语言课程设计火车票系统源代码.doc_第2页
第2页 / 共11页
C语言课程设计火车票系统源代码.doc_第3页
第3页 / 共11页
C语言课程设计火车票系统源代码.doc_第4页
第4页 / 共11页
C语言课程设计火车票系统源代码.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、#include#include#include/火车票结构体类型/typedef struct Nodeint num; /编号 /char name20; /起点和终点/char time5; /出发时间/int price; /车票价格/int amount; /剩余数量/struct Node *next;Node;/创建链表并输入数据/struct Node *creat()struct Node *head,*r,*s; int i=0; char choice;head=(struct Node *)malloc(sizeof(struct Node);head-next=NUL

2、L;r=head;do s=(struct Node *)malloc(sizeof(struct Node);s-next=NULL;printf(“请输入第%d 种火车票的信息 :n“,+i);printf(“请输入火车的编号:“);scanf(“%d“, printf(“起点和终点:“);scanf(“%s“,s-name);printf(“出发时间:“);scanf(“%s“,s-time);printf(“车票价格:“);scanf(“%d“,printf(“剩余数量:“);scanf(“%d“, r-next=s;r=s; printf(“Continue?(Y/N)“);scan

3、f(“%s“,while(choice=Y|choice=y); r-next=NULL;return(head);/将单链表中的信息保存到文件 1.txt 中/void save(struct Node *h) struct Node *s;FILE *fp; char filename10=“1.txt“;fp=fopen(“1.txt“,“wt“);if(fp=NULL) printf(“n 写文件出错,按任意键退出! “);getchar();exit(1); for(s=h-next;s!=NULL;s=s-next)fprintf(fp,“%d %s %s %d %d n“,s-n

4、um,s-name,s-time,s-price,s-amount);getchar();fclose(fp);/ 从文件 1.txt 中读取信息并存入单链表中 /struct Node *read()struct Node *head,*r,*s;FILE *fp;char filename10=“zl.txt“;fp=fopen(“1.txt“,“rt“);if(fp=NULL)printf(“读文件错误,按任意键退出!“);getchar();exit(1);head=(struct Node *)malloc(sizeof(struct Node);head-next=NULL;r=h

5、ead;while(!feof(fp)s=(struct Node *)malloc(sizeof(struct Node);fscanf(fp,“%d %s %s %d %d“,r-next=s;r=s; r-next=NULL;fclose(fp);return head;/将链表中的数据输出/void print(struct Node *h)struct Node *s;printf(“n 火车票信息如下:n“);printf(“n“);printf(“编号 起点和终点 出发时间 车票价格 剩余票数: n“);for(s=h-next;s-next!=NULL;s=s-next)pri

6、ntf(“ %d %10s %5s %10d %6dn“,s-num,s-name,s-time,s-price,s-amount);/链表查询/struct Node * find(struct Node *h)int i,j;char s20;printf(“tt 查询方法有以下几种:n“);printf(“tt 1.火车票编号 n“);printf(“tt 2.起点和终点 n“);printf(“tt 3.出发时间n“);printf(“tt 4.车票价格n“);printf(“tt 5.剩余票数n“);printf(“请输入您要查询的方法的序号:“);scanf(“%d“,switch

7、(i)case 1:printf(“请输入你要查询火车票的编号:“);scanf(“%d“,while(h-next!=NULL) h=h-next;if(h-num=j)return h;return NULL;break;case 2:printf(“请输入您要查询火车票的起点和终点:“);scanf(“%s“,s);while(h-next!=NULL)h=h-next;if(strcmp(h-name,s)=0)return h;return NULL; break;case 3:printf(“请输入您要查询火车票的时间:“);scanf(“%s“,s);while(h-next!=

8、NULL)h=h-next;if(strcmp(h-time,s)=0)return h;return NULL;break;case 4:printf(“请输入你要查询火车票的价格 :“);scanf(“%d“,while(h-next!=NULL) h=h-next;if(h-price=j)return h; return NULL;break;case 5:printf(“请输入你要查询火车票的剩余票数:“);scanf(“%d“,while(h-next!=NULL) h=h-next;if(h-amount=j)return h;return NULL;break;/修改信息/ch

9、ange(struct Node *h,int k)int j;struct Node *p;p=find(h);printf(“-n“);printf(“t 您要修改哪一项?n“);printf(“t 1.火车编号n“);printf(“t 2.起点和终点n“);printf(“t 3.出发时间n“);printf(“t 4.车票价格n“);printf(“t 5.剩余票数n“);printf(“t 0.退出系统n“);printf(“-n“);printf(“请输入您要修改项的编号:“);scanf(“%d“,switch(j)case 1: printf(“修改后的火车编号:“);sca

10、nf(“%d“,break;case 2:printf(“修改后的起点和终点:“);scanf(“%s“,p-name);break;case 3:printf(“修改后的出发时间:“);scanf(“%s“,p-time);break;case 4:printf(“修改后的车票价格 :“);scanf(“%d“,break;case 5:printf(“修改后的剩余票数 :“);scanf(“%d“,break;case 0:break;/删除信息/delete(struct Node *h)struct Node *p;int j;printf(“请输入您要删除的火车票的编号:“);sca

11、nf(“%d“,p=h-next;if(p=NULL)return 0;while(p!=NULL)if(p-num=j)h-next=p-next;free(p);return 1;h=p;p=p-next;return 0;/添加信息/void append()struct Node *p;FILE *fp;fp=fopen(“1.txt“,“at+“);if(fp=NULL)printf(“写文件出错,按任意键返回.n“);getchar();exit(1); printf(“请输入要添加的火车票的信息:火车编号,起点和终点,出发时间,车票价格,剩余票数:n“);scanf(“%d%s%

12、s%d%d“,fprintf(fp,“%d %s %s %d %dn“,p-num,p-name,p-time,p-price,p-amount);getchar();fclose(fp);/数据的统计/void count(struct Node *h)struct Node *s;s=h;int i,j,k,n=0;printf(“*n“);printf(“tt 请选择您要统计项目的序号:n“);printf(“tt 1.车票价格n“);printf(“tt 2.剩余票数n“);printf(“tt 0.退出界面n“);scanf(“%d“,switch(i)case 1:printf(“

13、请输入您要统计车票的价格的标准 :“);scanf(“%d“,printf(“tt 请选择低于或高于标准:n“);printf(“tt 1.价格低于%d 的个数n“,j);printf(“tt 2.价格高于%d 的个数n“,j);scanf(“%d“,if(k=1)for(s=h-next;s-next!=NULL;s=s-next)if(s-pricenext;s-next!=NULL;s=s-next)if(s-pricej)n+;printf(“车票价格低于%d 的个数有%d 个.n“,j,n);break;case 2:printf(“请输入您要统计剩余票数的数量 :“);scanf(

14、“%d“,printf(“tt 请选择低于或高于所输票数:n“);printf(“tt 1.票数低于%d 的个数n“,j);printf(“tt 2.票数高于%d 的个数n“,j);scanf(“%d“,if(k=1)for(s=h-next;s-next!=NULL;s=s-next)if(s-amountnext;s-next!=NULL;s=s-next)if(s-amountj)n+;printf(“剩余票数高于%d 的个数有%d 个.n“,j,n);break;case 0:break;/保存用户和密码到文件 2.txt 中/void save_user() char file10=

15、“2.txt“;FILE *fp;char name20;char pwd10;fp=fopen(“2.txt“,“at+“);if(fp=NULL)printf(“n 写文件出错,按任意键退出 .n“);getchar();exit(1);printf(“请输入用户名:“);scanf(“%s“,name);printf(“请输入密码:“);scanf(“%s“,pwd);fprintf(fp,“%s %sn“,name,pwd);getchar();fclose(fp);printf(“用户注册成功!n“);/检验用户和密码是否匹配/int check(char *name,char *p

16、wd)char name120;char pwd110;FILE *fp;char file10=“2.txt“;if(fp=fopen(“2.txt“,“rt“)=NULL)printf(“读文件出错,按任意键退出!n“);getchar();exit(1);while(!feof(fp)fscanf(fp,“%s %s“,name1,pwd1);if(strcmp(name1,name)=0return 0;/数据排序/void sort(struct Node *h)struct Node *s,*p,*m,*n;int t,t1,t2,t3;char s120;char s210;pr

17、intf(“车票价格由小到大排序如下:n“);for(s=h-next;s-next!=NULL;s=s-next)for(p=s-next;p-next!=NULL;p=p-next)if(s-pricep-price)t1=s-num;s-num=p-num;p-num=t1;t2=s-price;s-price=p-price;p-price=t2;t3=s-amount;s-amount=p-amount;p-amount=t3;strcpy(s1,s-name);strcpy(s-name,p-name);strcpy(p-name,s1);strcpy(s2,s-time);str

18、cpy(s-time,p-time);strcpy(p-time,s2);print(h);printf(“nn 剩余车票数量由多到少排序如下:n“);for(s=h-next;s-next!=NULL;s=s-next)for(p=s-next;p-next!=NULL;p=p-next)if(s-amountamount)t1=s-num;s-num=p-num;p-num=t1;t2=s-price;s-price=p-price;p-price=t2;t3=s-amount;s-amount=p-amount;p-amount=t3;strcpy(s1,s-name);strcpy(s

19、-name,p-name);strcpy(p-name,s1);strcpy(s2,s-time);strcpy(s-time,p-time);strcpy(p-time,s2);print(h);void main()struct Node *head,*p;int i,j,k;head=(struct Node *)malloc(sizeof(struct Node);head-next=NULL;char name20;char pwd10;printf(“n*欢迎进入火车票管理系统*n“);printf(“tt 1.用户登录n“);printf(“tt 2.用户注册n“);printf

20、(“tt 0.退出系统n“);printf(“请输入所选序号:“);scanf(“%d“,switch(k)case 1: printf(“请输入用户名:“);scanf(“%s“,name);printf(“请输入密码:“);scanf(“%s“,pwd);if(check(name,pwd)printf(“密码正确.n“);doprintf(“nntt*欢迎进入火车票管理系统*n“);printf(“tt 1.录入火车票信息tt 2.添加火车票信息n“);printf(“tt 3.修改火车票信息tt 4.删除火车票信息n“);printf(“tt 5.打印火车票信息tt 6.查询火车票信息

21、n“);printf(“tt 7.统计火车票信息tt 8.火车票销售排行n“);printf(“tt 0.退出系统n“);printf(“请输入您要进入菜单的序号(0-8):“); scanf(“%d“,switch(i)case 1:printf(“请录入火车票信息nn“);head=creat();save(head);head=read();break;case 2:append();break;case 3:printf(“请输入您要修改的火车票的编号 :“);scanf(“%d“,change(head,j);save(head);break;case 4:head=read();i

22、f(delete(head)printf(“已正确删除!n“);save(head); elseprintf(“要删除的结点不存在! n“);break;case 5:head=read();print(head);break;case 6:printf(“请输入您要查询火车票的编号 (以 0 结束):“);scanf(“%d“, p=find(head);printf(“编号 起点和终点 出发时间 车票价格 剩余票数:n“);printf(“%d %10s %5s %10d %6dn“,p-num,p-name,p-time,p-price,p-amount);printf(“请继续输入序号( 以 0 结束):“);scanf(“%d“,break; case 7: head=read();count(head);break;case 8: sort(head);break;case 0: printf(“*谢谢使用!*n“);break; while(i!=0);else printf(“密码错误或用户名不存在.n“);break;case 2:save_user();break;case 0:break;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报