C语言课程设计火车票系统源代码.docx
《C语言课程设计火车票系统源代码.docx》由会员分享,可在线阅读,更多相关《C语言课程设计火车票系统源代码.docx(17页珍藏版)》请在冰豆网上搜索。
C语言课程设计火车票系统源代码
#include
#include
#include
//火车票结构体类型//
typedefstructNode
{intnum;//编号//
charname[20];//起点和终点//
chartime[5];//出发时间//
intprice;//车票价格//
intamount;//剩余数量//
structNode*next;
}Node;
//创建链表并输入数据//
structNode*creat()
{
structNode*head,*r,*s;
inti=0;
charchoice;
head=(structNode*)malloc(sizeof(structNode));
head->next=NULL;
r=head;
do
{
s=(structNode*)malloc(sizeof(structNode));
s->next=NULL;
printf("请输入第%d种火车票的信息:
\n",++i);
printf("请输入火车的编号:
");
scanf("%d",&s->num);
printf("起点和终点:
");
scanf("%s",s->name);
printf("出发时间:
");
scanf("%s",s->time);
printf("车票价格:
");
scanf("%d",&s->price);
printf("剩余数量:
");
scanf("%d",&s->amount);
r->next=s;
r=s;
printf("Continue?
(Y/N)");
scanf("%s",&choice);
}while(choice=='Y'||choice=='y');
r->next=NULL;
return(head);
}
//将单链表中的信息保存到文件1.txt中//
voidsave(structNode*h)
{
structNode*s;
FILE*fp;
char[10]="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->num,s->name,s->time,s->price,s->amount);
getchar();
fclose(fp);
}
//从文件1.txt中读取信息并存入单链表中//
structNode*read()
{
structNode*head,*r,*s;
FILE*fp;
char[10]="zl.txt";
fp=fopen("1.txt","rt");
if(fp==NULL)
{
printf("读文件错误,按任意键退出!
");
getchar();
exit
(1);
}
head=(structNode*)malloc(sizeof(structNode));
head->next=NULL;
r=head;
while(!
feof(fp))
{
s=(structNode*)malloc(sizeof(structNode));
fscanf(fp,"%d%s%s%d%d",&s->num,s->name,s->time,&s->price,&s->amount);
r->next=s;
r=s;
}
r->next=NULL;
fclose(fp);
returnhead;
}
//将链表中的数据输出//
voidprint(structNode*h)
{
structNode*s;
printf("\n火车票信息如下:
\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("编号起点和终点出发时间车票价格剩余票数:
\n");
for(s=h->next;s->next!
=NULL;s=s->next)
{
printf("%d%10s%5s%10d%6d\n",s->num,s->name,s->time,s->price,s->amount);
}
}
//链表查询//
structNode*find(structNode*h)
{
inti,j;
chars[20];
printf("\t\t查询方法有以下几种:
\n");
printf("\t\t1.火车票编号\n");
printf("\t\t2.起点和终点\n");
printf("\t\t3.出发时间\n");
printf("\t\t4.车票价格\n");
printf("\t\t5.剩余票数\n");
printf("请输入您要查询的方法的序号:
");
scanf("%d",&i);
switch(i)
{
case1:
printf("请输入你要查询火车票的编号:
");
scanf("%d",&j);
while(h->next!
=NULL)
{
h=h->next;
if(h->num==j)
returnh;
}
returnNULL;
break;
case2:
printf("请输入您要查询火车票的起点和终点:
");
scanf("%s",s);
while(h->next!
=NULL)
{
h=h->next;
if(strcmp(h->name,s)==0)
returnh;
}
returnNULL;break;
case3:
printf("请输入您要查询火车票的时间:
");
scanf("%s",s);
while(h->next!
=NULL)
{
h=h->next;
if(strcmp(h->time,s)==0)
returnh;
}
returnNULL;
break;
case4:
printf("请输入你要查询火车票的价格:
");
scanf("%d",&j);
while(h->next!
=NULL)
{
h=h->next;
if(h->price==j)
returnh;
}
returnNULL;
break;
case5:
printf("请输入你要查询火车票的剩余票数:
");
scanf("%d",&j);
while(h->next!
=NULL)
{
h=h->next;
if(h->amount==j)
returnh;
}
returnNULL;
break;
}
}
//修改信息//
change(structNode*h,intk)
{
intj;
structNode*p;
p=find(h);
printf("-------------------------------------------\n");
printf("\t您要修改哪一项?
\n");
printf("\t1.火车编号\n");
printf("\t2.起点和终点\n");
printf("\t3.出发时间\n");
printf("\t4.车票价格\n");
printf("\t5.剩余票数\n");
printf("\t0.退出系统\n");
printf("-------------------------------------------\n");
printf("请输入您要修改项的编号:
");
scanf("%d",&j);
switch(j)
{
case1:
printf("修改后的火车编号:
");
scanf("%d",&p->num);
break;
case2:
printf("修改后的起点和终点:
");
scanf("%s",p->name);
break;
case3:
printf("修改后的出发时间:
");
scanf("%s",p->time);
break;
case4:
printf("修改后的车票价格:
");
scanf("%d",&p->price);
break;
case5:
printf("修改后的剩余票数:
");
scanf("%d",&p->amount);
break;
case0:
break;
}
}
//删除信息//
delete(structNode*h)
{
structNode*p;
intj;
printf("请输入您要删除的火车票的编号:
");
scanf("%d",&j);
p=h->next;
if(p==NULL)
return0;
while(p!
=NULL)
{
if(p->num==j)
{
h->next=p->next;
free(p);
return1;
}
h=p;
p=p->next;
}
return0;
}
//添加信息//
voidappend()
{
structNode*p;
FILE*fp;
fp=fopen("1.txt","at+");
if(fp==NULL)
{
printf("写文件出错,按任意键返回.\n");
getchar();
exit
(1);
}
printf("请输入要添加的火车票的信息:
火车编号,起点和终点,出发时间,车票价格,剩余票数:
\n");
scanf("%d%s%s%d%d",&p->num,p->name,p->time,&p->price,&p->amount);
fprintf(fp,"%d%s%s%d%d\n",p->num,p->name,p->time,p->price,p->amount);
getchar();
fclose(fp);
}
//数据的统计//
voidcount(structNode*h)
{
structNode*s;
s=h;
inti,j,k,n=0;
printf("*****************************************************************************\n");
printf("\t\t请选择您要统计项目的序号:
\n");
printf("\t\t1.车票价格\n");
printf("\t\t2.剩余票数\n");
printf("\t\t0.退出界面\n");
scanf("%d",&i);
switch(i)
{
case1:
printf("请输入您要统计车票的价格的标准:
");
scanf("%d",&j);
printf("\t\t请选择低于或高于标准:
\n");
printf("\t\t1.价格低于%d的个数\n",j);
printf("\t\t2.价格高于%d的个数\n",j);
scanf("%d",&k);
if(k==1)
{
for(s=h->next;s->next!
=NULL;s=s->next)
if(s->pricen++;
printf("车票价格低于%d的个数有%d个.\n",j,n);
}
else
{
for(s=h->next;s->next!
=NULL;s=s->next)
if(s->price>j)
n++;
printf("车票价格低于%d的个数有%d个.\n",j,n);
}
break;
case2:
printf("请输入您要统计剩余票数的数量:
");
scanf("%d",&j);
printf("\t\t请选择低于或高于所输票数:
\n");
printf("\t\t1.票数低于%d的个数\n",j);
printf("\t\t2.票数高于%d的个数\n",j);
scanf("%d",&k);
if(k==1)
{
for(s=h->next;s->next!
=NULL;s=s->next)
if(s->amountn++;
printf("剩余票数低于%d的个数有%d个.\n",j,n);
}
else
{
for(s=h->next;s->next!
=NULL;s=s->next)
if(s->amount>j)
n++;
printf("剩余票数高于%d的个数有%d个.\n",j,n);
}
break;
case0:
break;
}
}
//保存用户和密码到文件2.txt中//
voidsave_user()
{
char]="2.txt";
FILE*fp;
charname[20];
charpwd[10];
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%s\n",name,pwd);
getchar();
fclose(fp);
printf("用户注册成功!
\n");
}
//检验用户和密码是否匹配//
intcheck(char*name,char*pwd)
{
charname1[20];
charpwd1[10];
FILE*fp;
char]="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)==0&&strcmp(pwd1,pwd)==0)
return1;
}
return0;
}
//数据排序//
voidsort(structNode*h)
{
structNode*s,*p,*m,*n;
intt,t1,t2,t3;
chars1[20];
chars2[10];
printf("车票价格由小到大排序如下:
\n");
for(s=h->next;s->next!
=NULL;s=s->next)
for(p=s->next;p->next!
=NULL;p=p->next)
if(s->price>p->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);strcpy(s->time,p->time);strcpy(p->time,s2);
}
print(h);
printf("\n\n剩余车票数量由多到少排序如下:
\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->name,p->name);strcpy(p->name,s1);
strcpy(s2,s->time);strcpy(s->time,p->time);strcpy(p->time,s2);
}
print(h);
}
voidmain()
{
structNode*head,*p;
inti,j,k;
head=(structNode*)malloc(sizeof(structNode));
head->next=NULL;
charname[20];
charpwd[10];
printf("\n***************欢迎进入火车票管理系统******************\n");
printf("\t\t1.用户登录\n");
printf("\t\t2.用户注册\n");
printf("\t\t0.退出系统
\n");
printf("请输入所选序号:
");
scanf("%d",&k);
switch(k)
{
case1:
printf("请输入用户名:
");
scanf("%s",name);
printf("请输入密码:
");
scanf("%s",pwd);
if(check(name,pwd))
{
printf("密码正确.\n");
do
{
printf("\n\n\t\t*********************欢迎进入火车票管理系统***********************\n");
printf("\t\t1.录入火车票信息\t\t2.添加火车票信息\n");
printf("\t\t3.修改火车票信息\t\t4.删除火车票信息\n");
printf("\t\t5.打印火车票信息\t\t6.查询火车票信息\n");
printf("\t\t7.统计火车票信息\t\t8.火车票销售排行\n");
printf("\t\t0.退出系统\n");
printf("请输入您要进入菜单的序号(0-8):
");
scanf("%d",&i);
switch(i)
{
case1:
printf("请录入火车票信息\n\n");
head=creat();
save(head);
head=read();
break;
case2:
append();
break;
case3:
printf("请输入您要修改的火车票的编号:
");
scanf("%d",&j);
change(head,j);
save(head);
break;
case4:
head=read();
if(delete(head))
{
printf("已正确删除!
\n");
save(head);
}
else
printf("要删除的结点不存在!
\n");
break;
case5:
head=read();
print(head);
break;
case6:
printf("请输入您要查询火车票的编号(以0结束):
");
scanf("%d",&j);
{
p=find(head);
printf("编号起点和终点出发时间车票价格剩余票数:
\n");
printf("%d%10s%5s%10d%6d\n",p->num,p->name,p->time,p->price,p->amount);
printf("请继续输入序号(以0结束):
");
scanf("%d",&j);
}
break;
case7:
head=read();count(head);break;
case8:
sort(head);break;
case0:
printf("************************谢谢使用!
*****************************\n");break;
}
}while(i!
=0);
}
else
printf("密码错误或用户名不存在.\n");
break;
case2:
save_user();break;
case0:
break;
}