C语言编程火车订票系统源代码.docx
《C语言编程火车订票系统源代码.docx》由会员分享,可在线阅读,更多相关《C语言编程火车订票系统源代码.docx(17页珍藏版)》请在冰豆网上搜索。
C语言编程火车订票系统源代码
火车订票系统源码
#inelude#include#inelude#include
intshoudsave=0;
intcountl=0/COunt2=0,mark=0,mark:
l=0;
/*定义存储火车信息的结构体-7
structtrain
{
charnum[10];/*列车号*7
charcity[10];/*0的城市*/
chartakeoffTime[10];/*发车时间*/
charreceiveTime[10];/*到达时间*/
intprice;/*票价*/
intbookNum;广票数*/
};
厂订票人的信息*/
structman
{
charnum[10];/*ID*/
charname[10];/*姓名*7
intbookNum;/*需求的票数*/
};
/*定义火车信息链表的结点结构
typedefstructnode
{
structtraindata;
structnode*next;
}Node,*Link;
厂定义订票人链表的结点结构*/
typedefstructpeople
{
structmandata;
structpeople*next;
JbookMan/bookManLink;
/*初始界面*7
voidprintlnterface()
{puts(H********************************************************uputs(H*Welcometousethesystemofbookingtickets*n);
puts(H********************************************************Hputs(H*Youcanchoosetheoperation:
*");
puts("*
l:
lnsertatraininformation
*u);
puts("*
2:
lnquireatraininformation
*H);
puts("*
3:
Bookatrainticket
*H);
puts("*
4:
Updatethetraininformation
*H);
puts("*
5:
Advicetoyouaboutthetrain
*n);
puts("*
6:
saveinformationtofile
*n);
puts("*
7:
quitthesystem
*H);
}
厂添加一个火车信息-7
voidlnsertTraininfo(Linklinkhead){
structnode*p,*rz*s;
charnum[10];
r=linkhead;
s=linkhead->next;
while(r->next!
=NULL)
r=r->next;
while(l)
{
printf(Hpleaseinputthenumberofthetrain(O-return)”);scanf「%s舄num);
讦(strcmp(num,,lO,,)==O)
break;
厂判断是否已经存在-7
while(s)
{
if(strcmp(s->data.num,num)==O)
{
printff'thetrain%s'hasbeenborn!
\n舄num);
return;
}s=s・>next;
}
p=(structnode*)malloc(sizeof(structnode));strcpy(p->data.num,num);
printf(Hlnputthecitywherethetrainwillreach:
11);scanf("%s:
p・>data.city);
printf(nlnputthetimewhichthetraintakeoff:
");scanf(,,%s,,,p->data.takeoffTime);
printf(Mlnputthetimewhichthetrainreceive:
11);scanfC^s'^&p^data.receiveTime);
printf(Mlnputthepriceofticket:
11);
scanf(,,%d,,,&p->data.price);
printf(Mlnputthenumberofbookedtickets:
");scanf(,,%d,,,&p->data.bookNum);p->next=NULL;
r->next=p;
r=P;shoudsave=1;
}
厂打印火车票信息>7
voidprintTrainlnfo(structnode*p)
{
puts(H\nThefollowingistherecordyouwant:
");
printf(H»numberoftrain:
%s\nH,p->data.num);
printf(H»citythetrainwillreach:
%s\n'^p^data.city);printf(u»thetimethetraintakeoff:
%s\nthetimethetrainreach:
%s\nI,,p->data.takeoffTime,p->data.receiveTime);
printf(H»thepriceoftheticket:
%d\n,,/p->data.price);
printf(u»thenumberofbookedtickets:
%d\n舄podata.bookNum);
structnode*LocatelfLinkl,charfindmess[],charnumorcity[])
Node*r;
if(strcmp(numorcity;,,num,,)==0)
r=l->next;
while(r)
{
if(strcmp(r->data.num,findmess)==O)returnr;
r=r->next;
}
else讦(strcmp(numorcityrcity”)==0)
r=l->next;
while(r)
{
if(strcmp(r->data.city,findmess)==O)returnr;
r=r->next;
}
return0;
厂查询火车信息沐/
voidQueryTrain(LinkI)
{
Node*p;
intsei;
charstrl[5],str2[10];
if(!
l->next)
{printf(HThereisnotanyrecord!
H);return;
}
printf("Choosetheway:
\n>>l:
accordingtothenumberoftrain;\n»2:
accordingtothecity:
\n");
scanf(“%d”,&sel);
if(sel==l)
{
printff'lnputthethenumberoftrain:
");
scanf("%s",strl);
p=Locatel(l,strl/,num");
if(P)
{
printTrainlnfo(p);
}
else
{
markl=l;printf("\nthefilecan'tbefound!
");
}
}
else讦(sel==2)
{
printf("lnputthecity:
");
scanf("%s",str2);
p=Locatel(l,str2/"city");
if(P)
{
printTrainlnfo(p);
}
else
{
markl=l;
printf(u\nthefilecan'tbefound!
11);
}
/*订票子模块*/
voidBookTicket(Linkl,bookManLinkk)
Node*r[10],*p;
charch,dem;bookMan*v,*h;
inti=O,t=O;
charstr[10],strl[10],str2[10];
v=k;
while(v->next!
=NULL)
v=v->next;
printf(nlnputthecityyouwanttogo:
H);scanf("%s",&str);
p=l->next;while(p!
=NULL)
if(strcmp(p->data.city,str)==O)
r[H=P;i++;
}
p=p->next;
printf(H\n\nthenumberofrecordhave%d\n,,,i);for(t=0;t
printTrainlnfo(r[t]);
if(i==O)
printf("\n\t\t\tSorry!
Can11findthetrainforyou!
\n");
else
printf(u\ndoyouwanttobookit?
\n");scanf("%d”,&ch);
讦(ch==1)
h=(bookMan*)malloc⑸zeof(booklVlan));printf(Hlnputyourname:
");
scanf("%s",&strl);strcpy(h->data.name,strl);printf(Hlnputyourid:
");
scanf("%s",&str2);strcpy(h->data.num,str2);printf「InputyourbookNum:
");scanf("%cT,&dem);
h・>data.bookNum二dem;h->next=NULL;
v・>next=h;
v=h;
printf(H\nLucky!
youhavebookedaticket!
");getch();
shoudsave=l;
}
}
}
bookMan*Locate2(bookManLinkk,charfindmess[])
{
bookMan*r;
r=k->next;
while(r)
{
if(strcmp(r->data.num,finclmess)==O)
{
mark=l;
returnr;
}
r=r->next;
}
return0;
}
厂修改火车信息沐/
voidUpdatelnfofLinkI)
{
Node*p;
charfindmess[20],ch;
if(!
l->next)
{
printf(u\nthereisn'trecordforyoutomodify!
\nu);return;
}
else
QueryTrain(l);if(markl==O)
printf(H\nDoyouwanttomodifyit?
\nH);getchar();
scanf「%c舄&ch);
if(ch=='y');
{
printf(H\nlnputthenumberofthetrain:
11);scanf(,,%s"/findmess);
p=Locatel(l,findmess/1num11);
讦(P)
ticket:
");
printf「Inputnewnumberoftrain:
");scanf(,,%s,,,&p->data.num);
printf(Hlnputnewcitythetrainwillreach:
11);scanf(,,%s,,,&p->data.city);
printf(nlnputnewtimethetraintakeoff11);scanf(,,%s,,,&p->data.takeoffTime);
printf(Hlnputnewtimethetrainreach:
11);
seanf「%s—&p->data.receiveTime);
printf(Mlnputnewpriceoftheticket:
:
11);scanf(,,%d,,/&p->data.price);
printf(Hlnputnewnumberofpeoplewhohavebooked
}
scanf(,,%d,,/&p->data.bookNum);printf(H\nmodifyingrecordissucessful!
\nJ;shoudsave=l;
else
printff'XtXtXtcan'tfindtherecord!
");
}
}
else
markl=O;
}
}
/*系统给用户的提示信息*/
voidAdvicedTrains(LinkI)
Node*r;
charstr[10];
intmar=O;
r=l->next;
printf("luputthecityyouwanttogo:
n);scanf「%s舄str);
while(r)
讦(strcmp(r->data.city,str)==0&&r->data.bookNum<200)
{
mar=l;
printf(H\nyoucanselectthefollowingtrain!
\nM);
printf("\n\npleaseselectthefourthoperationtobooktheticket!
\n");printTrainlnfo(r);
}
r=r->next;
}
if(mar==O)
printf(H\n\t\t\tyoucan'tbookanyticketnow!
\n");
}
/*保存火车信息
voidSaveTrainInfo(LinkI)
{
FILE*fp;
Node*p;
intcount=O,flag二1;
fp=fopen(,,c:
\\train.txt,,/,,wb");
if(fp==NULL)
{
printf(uthefilecan'tbeopened!
11);
return;
}
p=l->next;
while(p)
{
if(fwrite(p,sizeof(Node)/l,fp)==l)
{
p=p->next;
count++;
}
else
{
flag=O;
break;
}
}
if(flag)
printf("thenumberoftherecordwhichhavebeensavedis%d\nHzcount);shoudsave=0;
}fclose(fp);
}
厂保存订票人的信息*/
voidSaveBookmanInfo(bookManLinkk)
{
FILE*fp;
bookMan*p;
intcount=O,fbg二1;
fp=fopen「c:
\\man・txtTwb“);
if(fp==NULL)
{
printf(Hthefilecan'tbeopened!
11);
return;
}
p=k->next;
while(p)
{
if(fwrite(p,sizeof(bookMan),l,fp)==l)
{
p=p->next;
count++;
}
else
{
flag=O;
break;
}
}
if(flag)
{
printf(uthenumberoftherecordwhichhavebeensavedis%d\n'^count);shoudsave=0;
}
fclose(fp);
}
intmain()
FILE*fpl,*fp2;
Node*p,*r;
charchl,ch2;
LinkI;
bookManLinkk;
bookMan*t,*h;
intsei;
l=(Node*)malloc(sizeof(Node));
l・>next=NULL;
r=l;
k=(bookMan*)malloc(sizeof(bookl\/lan));
konext=NULL;
h=k;
fpl=fopen(,,c:
\\train.txt,,z,,ab+");
if((fpl==NULL))
{
printff'can'topenthefile!
");return0;
}
while(!
feof(fpl))
{
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node)/l,fpl)==l)
{
p->next=NULL;
r->next=p;
r=P;
countl++;
}
}
fclose(fpl);
fp2=fopen("c:
\\man.txt,,/"ab+");
if((fp2==NULL))
{
printff'can'topenthefile!
11);return0;
}
while(!
feof(fp2))
{
t=(bookMan*)malloc⑸zeof(bookMan));if(fread(t/sizeof(bookMan),l/fp2)==l){
t・>next=NULL;
h->next=t;
h=t;
count2++;
fclose(fp2);
while(l)
{
systemC'cls");
printlnterface();
printf(Hpleasechoosetheoperation:
n);
scanf("%d“,&sel);
systemC'cls");
if(sel==8)
{
讦(shoudsave==l)
{
getchar();
printf(u\nthefilehavebeenchangedidoyouwanttosaveit(y/n)?
\n");
scanf("%比&chl);
if(chl=='y'||chl=='Y')
{
SaveBookmanlnfo(k);
SaveTrainlnfo(l);
}
}
printf(H\nThankyou!
!
Youarewelcometoo\n”);break;
}
switch(sel)
{
case1:
InsertTraininfo(l);break;
case2:
QueryTrain(l);break;
case3:
BookTicket(I,k);break;
case4:
Updatelnfo(l);break;
case5:
AdvicedTrains(l);break;
case6:
SaveTrainlnfo(l);SaveBookmanlnfo(k);break;
case7:
return0;
}
printf(H\npleasepressanykeytocontinue");
getch();}
return0;
}