next ;while(p!=NULL){if(strcmp(p->data.reachcity,str)==0){r[i]=p ;/*将满足条件的记录存。C语言火车订单管理源码( 二 )。" />

C语言火车订单管理源码( 二 )

next!=NULL)q=q->next ;printf("Input the city you want to go: ");scanf("%s",/*输入要到达的城市*/p=l->next ;while(p!=NULL){if(strcmp(p->data.reachcity,str)==0){r[i]=p ;/*将满足条件的记录存到数组r中*/i++;}p=p->next ;}printf("\n\nthe number of record have %d\n",i);printheader();for(t=0;t\n");scanf("%s",ch); if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0)/*判断是否订票*/{h=(book*)malloc(sizeof(book));printf("Input your name: ");scanf("%s",strcpy(h->data.name,str1);printf("Input your id: ");scanf("%s",strcpy(h->data.num,str2);printf("please input the number of the train:");scanf("%s",tnum);for(t=0;tdata.num,tnum)==0){if(r[t]->data.ticketnum<1)/*判断剩余的供订票的票数是否为0*/{printf("sorry,no ticket!");sleep(2);return;}printf("remain %d tickets\n",r[t]->data.ticketnum);flag=1;break;}if(flag==0){printf("input error");sleep(2);return;}printf("Input your bookNum: ");scanf("%d",r[t]->data.ticketnum=r[t]->data.ticketnum-dnum;/*定票成功则可供订的票数相应减少*/h->data.bookNum=dnum ;h->next=NULL ;q->next=h ;q=h ;printf("\nLucky!you have booked a ticket!");getch();saveflag=1 ;}}}/*修改火车信息*/void Modify(Link l){Node *p ;char tnum[10],ch ;p=l->next;if(!p){printf("\nthere isn't record for you to modify!\n");return ;}else{printf("\nDo you want to modify it?(y/n)\n");getchar();scanf("%c",if(ch=='y'||ch=='Y'){printf("\nInput the number of the train:");scanf("%s",tnum); while(p!=NULL) if(strcmp(p->data.num,tnum)==0)/*查找与输入的车号相匹配的记录*/break;elsep=p->next;if(p){printf("Input new number of train:");scanf("%s",printf("Input new city the train will start:");scanf("%s",printf("Input new city the train will reach:");scanf("%s",printf("Input new time the train take off");scanf("%s",printf("Input new time the train reach:");scanf("%s",printf("Input new price of the ticket::");scanf("%d",printf("Input new number of people who have booked ticket:");scanf("%d",printf("\nmodifying record is sucessful!\n");saveflag=1 ;}elseprintf("\tcan't find the record!");}}}void showtrain(Link l)/*自定义函数显示列车信息*/{Node *p;p=l->next;printheader();if(l->next==NULL)printf("no records!");else while(p!=NULL){ printdata(p); p=p->next;}}/*保存火车信息*/void SaveTrainInfo(Link l){FILE*fp ;Node*p ;int count=0,flag=1 ;fp=fopen("f:\\train.txt","wb");if(fp==NULL){printf("the file can't be opened!");return ;}p=l->next ;while(p){if(fwrite(p,sizeof(Node),1,fp)==1){p=p->next ;count++;}else{flag=0 ;break ;}}if(flag){printf(" saved %d train records\n",count);saveflag=0 ;}fclose(fp);}/*保存订票人的信息*/void SaveBookInfo(bookLink k){FILE*fp ;book *p ;int count=0,flag=1 ;fp=fopen("f:\\man.txt","wb");if(fp==NULL){printf("the file can't be opened!");return ;}p=k->next ;while(p){ if(fwrite(p,sizeof(book),1,fp)==1){p=p->next ;count++;}else{flag=0 ;break ;}}if(flag){printf(" saved %d booking records\n",count);saveflag=0 ;}fclose(fp);}main(){FILE*fp1,*fp2 ;Node *p,*r ;char ch1,ch2 ;Link l ;bookLink k ;book *t,*h ;int sel ;l=(Node*)malloc(sizeof(Node));l->next=NULL ;r=l ;k=(book*)malloc(sizeof(book));k->next=NULL ;h=k ;fp1=fopen("f:\\train.txt","ab+");/*打开存储车票信息的文件*/if((fp1==NULL)){printf("can't open the file!");return 0 ;}while(!feof(fp1)){p=(Node*)malloc(sizeof(Node));if(fread(p,sizeof(Node),1,fp1)==1)/*从指定磁盘文件读取记录*/{p->next=NULL ;r->next=p ;/*构造链表*/r=p ;}}fclose(fp1);fp2=fopen("f:\\man.txt","ab+");if((fp2==NULL)){printf("can't open the file!");return 0 ;}while(!feof(fp2)){t=(book*)malloc(sizeof(book));if(fread(t,sizeof(book),1,fp2)==1){t->next=NULL ;h->next=t ;h=t ;}}fclose(fp2);while(1){clrscr();menu();printf("\tplease choose (0~6):");scanf("%d",clrscr();if(sel==0){if(saveflag==1)/*当退出时判断信息是否保存*/{getchar();printf("\nthe file have been changed!do you want to save it(y/n)?\n");scanf("%c",if(ch1=='y'||ch1=='Y'){SaveBookInfo(k);SaveTrainInfo(l);}}printf("\nThank you!!You are welcome too\n");break ;}switch(sel)/*根据输入的sel值不同选择相应操作*/{case 1 :Traininfo(l);break ;case 2 :searchtrain(l);break ;case 3 :Bookticket(l,k);break ;case 4 :Modify(l);break ;case 5:showtrain(l);break;case 6 :SaveTrainInfo(l);SaveBookInfo(k);break ;case 0:return 0;}printf("\nplease press any key to continue.......");getch();}}