求助!火车订票系统,运行出来之后输入相应的序号,为什么没有反应,一直都是初始界面,希望各位帮帮忙!
#include <conio.h>#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#define HEADER1 " -----------------------------------BOOK TICKET----------------------------------n"
#define HEADER2 "| number |start city|reach city|takeofftime|recervetime|price|ticketnumber|n"
#define HEADER3 "|------------|----------|----------|-----------|-----------|-----|------------|n"
#define FORMAT "|%-10S|%-10S|%-10S|%-10S|%-10S|%5d| %5d |n"
#define DATA p->data.num,p->data.startcity,p->data.reachcity,p->data.takeofftime,p->data.receivetime,p->data.price,p->data.ticketnum
int saveflag=0 ;
//定义存储火车信息的结构体
struct train {
char num[10]; //列车号
char startcity[10]; //出发城市
char reachcity[10]; //目的城市
char takeofftime[10]; // 发车时间
char receivetime[10]; //到达时间
int price; //票价
int ticketnum; //票数
} ;
//订票人的信息
struct man {
char num[10]; //ID
char name[10]; //姓名
int bookNum; //订的票数
} ;
//定义火车票信息链表的节点结构
typedef struct node {
struct train data;//声明train结构体类型的变量data
struct node * next;
} Node,*Link;
//定义订票人链表的节点结构
typedef struct Man {
struct man data;
struct Man *next;
} book,*bookLink;
//初始界面
void menu() {
puts("nn");
puts("tt|---------------------------------------------|");//输出终端
puts("tt| Booking Tickets |");
puts("tt|----------------------------------------------|");
puts("tt| 0:quit the system |");
puts("tt| 1:lnsert a train information |");
puts("tt| 2:Search a train information |");
puts("tt| 3:Book a train ticket |");
puts("tt| 4:Modify the train information |");
puts("tt| 5:Show the train information |");
puts("tt| 6:save information to file |");
puts("tt|----------------------------------------------|");
}
//添加火车票信息
void Traininfo(Link linkhead) {
struct node *p,*r,*s;
char num[10];
r=linkhead;
s=linkhead->next;
while(r->next!=NULL)
r=r->next;
while(1) { //进入死循环
printf("please input the number of the train(0-return)");
scanf("%s",num);
if(strcmp(num,"0")==0) //比较字符
break;
//判断是否已经存在
while(s) {
if(strcmp(s->data.num,num)==0) {
printf("the train '%s'is existing!n",num);
return;
}
s=s->next;
}
p=(struct node*)malloc(sizeof(struct node));
strcpy(p->data.num,num); //复制车号
printf("input the city where the train will start:");
scanf("%s",p->data.startcity); //输入出发城市
printf("input the city where the train will reach:");
scanf("%s",p->data.reachcity);//输入到站城市
printf("Input the time which the train take off:");
scanf("%s",p->data.takeofftime);//输入出发时间
printf("Input the time which the train recerve:");
scanf("%d",&p->data.receivetime);//输入到站时间
printf("Input the price of ticket:");
scanf("%d",&p->data.price);//输入火车票价
printf("Input the number of booked tickets:");
scanf("%d",&p->data.ticketnum);//输入预定票数
p->next=NULL;
r->next=p;//插入链表中
r=p;
saveflag= 1;
}
}
//打印火车票信息
void printheader() { //格式化输出头表
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
void printdata(Node *q) { //格式化输出表中数据
Node* p;
p=q;
printf( FORMAT,DATA);
}
//查询火车信息
void searchtrain(Link I) {
Node *s[10],*r;
int sel,k,i=0;
char str1[5],str2[10];
if(!I->next) {
printf("There is not any record!");
return;
}
printf("Choose the way:n1:according to the number of train;n2:according to the city:n");
scanf("%d",&sel);//输入选择的序号
if(sel==1) { //若输入的序号等于1,则根据车次查询
printf("Input the the number of train:");
scanf("%s",str1);//输入火车车次
r=I->next;
while(r!=NULL)//遍历指针r,若为空则跳出循环
if(strcmp(r->data.num,str1)==0) { //检索是否有与输入的车次相匹配的火车
s[i]=r;
i++;
break;
} else
r=r->next;//没有查找到火车车次则指针r后移一位
} else if(sel==2) { //选择2则根据城市查询
printf("Input the city you want to go:");
scanf("%s",str2);//输入查询的城市
r=I->next;
while(r!=NULL)//遍历指针r
if(strcmp(r->data.reachcity,str2)==0) { //检索是否有与输入的城市相匹配的火车
s[i]=r;
i++;//检索到有匹配的火车票信息,执行i++
r=r->next;
} else
r=r->next;
}
if(i==0)
printf("can not find!");
else {
printheader();//输出表头
for(k=0; k<i; k++)
printdata(s[k]);//输入火车xinx
}
}
//订票模块(p18)
void Bookticket(Link I,bookLink k) {
Node *r[10],*p;
char ch[2],tnum[10],str[10],str1[10],str2[10];
book *q,*h;
int i=0,t=0,flag=0,dnum;
q=k;
while(q->next!=NULL)
q=q->next;
printf("Input the city you want to go:");
scanf("%s",&str); //输入要到达的城市
p=I->next; //*p指向传入的参数指针I的下一位
while(p!=NULL) {
//遍历指针票p*
if(strcmp(p->data.reachcity,str)==0) { //比较输入的城市与输入的火车终点是否匹配
r[i]=p; //将满足条件的记录存到数组r中
i++;
}
p=p->next;
}
printf("nnthe number of record have %dn",i);
printheader(); //输出表头
for(t==0; t<i; t++)
printdata(r[t]); //循环输出数组中的火车信息
if(i==0)
printf ("nSorry!Can't find the train for you!n");
else {
printf("ndo you want to book it?<y/n>n");
scanf("%s",ch);
if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) { //判断是否订票
h=(book*)malloc(sizeof(book));
printf("Input you name:");
scanf("%s",&str1); //输入订票人的姓名
strcpy(h->data.name,str1); //与存储的信息进行比较,看是否有重复的
printf("Input your id:");
scanf("%s",&str2); //输入身份证号
strcpy(h->data.num,str2); // 与存储的信息进行比较
printf("please input the number of the train:");
scanf("%s",tnum); //输入要预定的车次
for(t=0; t<i; t++)
if(strcmp(r[t]->data.num,tnum)==0) { //比较车次,看是否存在该车次
if(r[t]->data.ticketnum<1) { //判断剩余的可供预定的票数是否为0
printf("sorry,no ticket!");
Sleep(2);
return;
}
printf("remanin %d ticketsn",r[t]->data.ticketnum);
flag=1;
break;
}
if(flag==0) {
printf("input error");
Sleep(2);
return;
}
printf("Input your bookNum:");
scanf("%d",&dnum);//输入要预定的票数
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;
}
}
}
//修改模块(p20)
void Modify(Link I) {
Node *p;
char tnum[10],ch;
p=I->next;
if(!p) {
printf("nthere isn't record for you to modify!n");
return;
} else {
printf("nDo you want to modify if?(y/n)n");
scanf("%c",&ch);//输入是否想要修改的字符
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;
else
p=p->next;
if(p) { //遍历p,如果p不指向空则执行if语句
printf("Input new number of train:");
scanf("%s",&p->data.num); //输入新车次
printf("Input new city the train will start:");
scanf("%s",&p->data.startcity); //输入新始发站
printf("Input new city the train will reach:");
scanf("%s",&p->data.startcity); //输入新终点站
printf("Input new city the train take off:");
scanf("%s",&p->data.takeofftime); //输入新出发时间
printf("Input new city the train reach:");
scanf("%s",&p->data.receivetime); //输入新到站时间
printf("Input new price of the ticket::");
scanf("%s",&p->data.price); //输入新票价
printf("Input new number of people who have booked ticket:");
scanf("%s",&p->data.ticketnum); // 输入新票数
printf("nmodifying record is sucessful!n");
saveflag=1; //保存标志
} else
printf("tcan't find the record!");
}
}
}
//显示模块(p22)
void showtrain(Link I) { //自定义函数显示列车信息
Node *p;
p=I->next;
printheader();//输出列车表头
if(I->next==NULL)//判断有无可显示的信息
printf("no records!");
else
while(p!=NULL) { //遍历p*
printdata(p);//输出所有火车数据
p=p->next;//*p指针后移一位
}
}
//保存火车信息
void SaveTrainInfo(Link I) {
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=I->next;
while(p) { //遍历p指针
if(fwrite(p,sizeof(Node),1,fp)==1) { //向磁盘文件写入数据块
p=p->next;//指针指向xiayiw
count++;
} else {
flag=0;
break;
}
}
if(flag) {
printf("saved %d train recordsn",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 recordsn",count);
saveflag=0;
}
fclose(fp);
}
main() {
FILE*fp1,*fp2;
Node *p,*r;
char ch1,ch2;
Link I;
bookLink k;
book *t,*h;
int sel;
I=(Node*)malloc(sizeof(Node));
r=I;
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) {
system("CLS");
menu();
printf("tplease choose (0~6): ");
scanf("%d",&sel);
system("CLS");
if(sel==0) {
if(saveflag==1) { //当退出时判断信息是否保存
getchar();
printf("nthe file have been change!do you want to save it(yn)?n");
scanf("%c",&ch1);
{
SaveBookInfo(k);
if(ch1=='y'||ch1=='Y') {
SaveBookInfo(k);
SaveTrainInfo(I);
}
}
printf("n Thank you!!You are welcoome toon");
break;
}
switch(sel) { //根据输入不同的sel值来选择相应操作
case 1:
Traininfo(I);
break;
case 2:
searchtrain(I);
break;
case 3:
Bookticket(I,k);
break;
case 4:
Modify(I);
break;
case 5:
showtrain(I);
break;
case 6:
SaveTrainInfo(I);
SaveBookInfo(k);
break;
case 0:
return 0;
}
printf("nplease press any key to continue.......");
getch();
}
}
fp1=fopen("f:\train.txt","rb+");//打开存储车票信息的文件
if((fp1==NULL)) { //文件未成功打开
printf("can't open the file!");
return 0;
}
while(!feof(fp1)) { //测试文件流是否到结尾
p=(Node*)malloc(sizeof(Node));//为p动态辟内存
if(fread(p,sizeof(Node),1,fp1)==1) { //从指定磁盘文件读取记录
p->next=NULL;
r->next=p;//构造链表
r=p;
}
}
fclose(fp1); //关闭文件
fp2=fopen("f:\man.txt","rb+");
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);
}
}