| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1483 人关注过本帖
标题:求助!火车订票系统,运行出来之后输入相应的序号,为什么没有反应,一直都 ...
只看楼主 加入收藏
花花花花花
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2018-12-11
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
求助!火车订票系统,运行出来之后输入相应的序号,为什么没有反应,一直都是初始界面,希望各位帮帮忙!
#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);
    }
}
搜索更多相关主题的帖子: 输入 DATA next the printf 
2018-12-14 10:47
花花花花花
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2018-12-11
收藏
得分:0 
该问题已解决,正确代码如下。但又出现了其他问题,序号1的添加火车票信息模块陷入了死循环,如果不一直输入0的话,它是不会跳出来的,一直都是车次、出发城市、终点、时间等循环输入。希望大家帮忙看看,谢谢大家了。
#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:退出系统                                |");
    puts("tt|        1:添加火车票信息                          |");
    puts("tt|        2:查询火车票信息                          |");
    puts("tt|        3:订票模块                                |");
    puts("tt|        4:修改火车票信息                          |");
    puts("tt|        5:显示火车票信息                          |");
    puts("tt|        6:保存火车票信息和订票信息到磁盘文件      |");
    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:n");
        scanf("%d",&p->data.price);//输入火车票价
        printf("Input the number of booked tickets:n");
        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 l)
 {
    Node *s[10],*r;
    int sel,k,i=0;
    char str1[5],str2[10];
    if(!l->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=l->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=l->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 l,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=l->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 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 if?(y/n)n");
        getchar();
        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 l) { //自定义函数显示列车信息
    Node *p;
    p=l->next;
    printheader();//输出列车表头
    if(l->next==NULL)//判断有无可显示的信息
        printf("no records!");
    else
        while(p!=NULL) { //遍历p*
            printdata(p);//输出所有火车数据
            p=p->next;//*p指针后移一位
        }
}



//保存火车信息
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) { //遍历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 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)
        {
            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(l);
                        }
                    }
                    printf("n Thank you!!You are welcoome toon");
                    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();
            }
        }

2018-12-15 17:36
Mr_doge
Rank: 5Rank: 5
等 级:贵宾
威 望:10
帖 子:159
专家分:385
注 册:2018-6-28
收藏
得分:20 
你的程序问题有点多...恕我无法快速给出肯定的答案,谁叫我又懒又菜呢...
编译环境 win10 MinGW ide为code blocks
已知的问题:

1.编译不通过,报错原因,文件打开函数的输入字符串存在问题,例如:
fp1=fopen("f:\train.txt","ab+");//打开存储车票信息的文件
fp2=fopen("f:\man.txt","ab+");
字符串内,部分特殊字符应使用转义字符,例如\应当为\\,修改后可正常编译通过,运行后未发现你所说问题

2.字符串输出看着很糟心啊,前后多出的n,恐怕又是忘了转义的\n吧

3.文件的写入和读取的方式似乎有问题,无法正常显示希望保存的数据

其余的问题暂时没啥心思一个个找出来,这个程序不小,得自己水磨工夫一点一点的修缮

似乎没说清楚,你所说的死循环,本身就是你设置输入票信息的操作在一个while的循环体内,此循环嵌套在外层循环内,只要不是0,那么这个循环就会一直循环,输入0能跳出是因为外层循环你设置了字符串比对,为0则跳出循环,从外层跳出自然不会进入循环内下一步,想在输入完毕后就自动退回,可以定义该函数有int返回值然后用return 0;跳出函数等方式(话说输入0退出的判断不就是为了退出么)


[此贴子已经被作者于2018-12-20 01:15编辑过]

2018-12-20 00:52
花花花花花
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2018-12-11
收藏
得分:0 
回复 3楼 Mr_doge
感谢你的回复,其实我也很菜(是个新手),这个程序的运行环境是windows7 Dev c++
我已经知道问题出在哪里了,在修改模块部分:输入新票数和输入新票价哪里的scanf里面的"%s"改为"%d",下面是正确代码,我做了一些修改:
#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 "|    车次    |出发城市  |到达城市  |出发时间   |到达时间   |票价 |可预订票数  |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|               欢迎来到火车订票系统               |");
    puts("tt|--------------------------------------------------|");
    puts("tt|        0:退出系统                                |");
    puts("tt|        1:添加火车票信息                          |");
    puts("tt|        2:查询火车票信息                          |");
    puts("tt|        3:订票模块                                |");
    puts("tt|        4:修改火车票信息                          |");
    puts("tt|        5:显示火车票信息                          |");
    puts("tt|        6:保存火车票信息和订票信息到磁盘文件      |");
    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("请输入车次(按0-返回)");
        scanf("%s",num);
        if(strcmp(num,"0")==0)                    //比较字符
            break;
        //判断是否已经存在
        while(s)
         {
            if(strcmp(s->data.num,num)==0)
            {
                printf("火车 '%s'已经存在!n",num);
                return;
            }
            s = s->next;
        }
        p = (struct node*)malloc(sizeof(struct node));
        strcpy(p->data.num,num);                                        //复制车号
        printf("请输入出发城市:");
        scanf("%s",p->data.startcity);                            //输入出发城市
        printf("请输入到站城市:");
        scanf("%s",p->data.reachcity);//输入到站城市
        printf("请输入出发时间:");
        scanf("%s",p->data.takeofftime);//输入出发时间
        printf("请输入到站时间:");
        scanf("%s",&p->data.receivetime);//输入到站时间
        printf("请输入火车票价:");
        scanf("%s",&p->data.price);//输入火车票价
        printf("请输入预定票数:");
        scanf("%s",&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 l)
 {
    Node *s[10],*r;
    int sel,k,i=0;
    char str1[5],str2[10];
    if(!l->next)
    {
        printf("对不起,没有记录!");
        return;
    }
    printf("选择查询方式:n1:根据车次查询;n2:根据城市查询:n");
    scanf("%d",&sel);//输入选择的序号
    if(sel==1)
    { //若输入的序号等于1,则根据车次查询
        printf("请输入火车车次:");
        scanf("%s",str1);//输入火车车次
        r=l->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("请输入要到达的城市:");
        scanf("%s",str2);//输入查询的城市
        r=l->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("对不起,没有找到!");
    else
     {
        printheader();//输出表头
        for(k=0; k<i; k++)
            printdata(s[k]);//输入火车xinx
    }
}


//订票模块(p18)
void Bookticket(Link l,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("请输入要到达的城市:");
    scanf("%s",&str);                         //输入要到达的城市
    p=l->next;                                //*p指向传入的参数指针I的下一位
    while(p!=NULL)
     {
        //遍历指针票p*
        if(strcmp(p->data.reachcity,str)==0)
         { //比较输入的城市与输入的火车终点是否匹配
            r[i]=p;                                 //将满足条件的记录存到数组r中
            i++;
        }
        p=p->next;
    }
    printf("nn总共有 %d 条记录n",i);
    printheader();            //输出表头
    for(t==0; t<i; t++)
        printdata(r[t]);             //循环输出数组中的火车信息
    if(i==0)
        printf ("n对不起!没有你要找的火车信息n");
    else
    {
        printf("n你想预定吗?<y/n>n");
        scanf("%s",ch);
        if(strcmp(ch,"Y")==0||strcmp(ch,"y")==0) { //判断是否订票
            h=(book*)malloc(sizeof(book));
            printf("请输入你的姓名:");
            scanf("%s",&str1);                          //输入订票人的姓名
            strcpy(h->data.name,str1);                 //与存储的信息进行比较,看是否有重复的
            printf("请输入你的身份证号:");
            scanf("%s",&str2);                         //输入身份证号
            strcpy(h->data.num,str2);                    // 与存储的信息进行比较
            printf("请输入你要预定的车次:");
            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("对不起!没有剩余车票了");
                        Sleep(2);
                        return;
                    }
                    printf("还有 %d 张车票n",r[t]->data.ticketnum);
                    flag=1;
                    break;
                }
            if(flag==0) {
                printf("输入错误!");
                Sleep(2);
                return;
            }
            printf("请输入你要预定的票数:");
            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("n非常幸运!你已经订票成功了!");
            getch();
            saveflag=1;
        }
    }
}


//修改模块(p20)
void Modify(Link l) {
    Node *p;
    char tnum[10],ch;
    p=l->next;
    if(!p)
    {
        printf("n对不起,没有你要修改的火车记录!n");
        return;
    }
     else
      {
        printf("n确定要修改吗?(y/n)n");
        getchar();
        scanf("%c",&ch);//输入是否想要修改的字符
        if(ch=='y'||ch=='Y') { //判断字符
            printf("n请输入你要修改的车次:");
            scanf("%s",tnum);//输入需要修改的车次
            while(p!=NULL)
                if(strcmp(p->data.num,tnum)==0)//查找与输入的车次相匹配的记录
                    break;
                else
                    p=p->next;
            if(p) {                  //遍历p,如果p不指向空则执行if语句
                printf("请输入新的火车车次:");
                scanf("%s",&p->data.num);                    //输入新车次
                printf("请输入新的出发城市:");
                scanf("%s",&p->data.startcity);              //输入新始发站
                printf("请输入新的到达城市:");
                scanf("%s",&p->data.startcity);              //输入新终点站
                printf("请输入新的出发时间:");
                scanf("%s",&p->data.takeofftime);           //输入新出发时间
                printf("请输入新的到达时间:");
                scanf("%s",&p->data.receivetime);           //输入新到站时间
                printf("请输入新票价:");
                scanf("%d",&p->data.price);                 //输入新票价
                printf("请输入新票数:");
                scanf("%d",&p->data.ticketnum);                  // 输入新票数
                printf("n修改成功!n");
                saveflag=1;                                 //保存标志
            } else
                printf("t对不起,没有记录!");
        }
    }
}



//显示模块(p22)
void showtrain(Link l) { //自定义函数显示列车信息
    Node *p;
    p=l->next;
    printheader();//输出列车表头
    if(l->next==NULL)//判断有无可显示的信息
        printf("no records!");
    else
        while(p!=NULL) { //遍历p*
            printdata(p);//输出所有火车数据
            p=p->next;//*p指针后移一位
        }
}



//保存火车信息
void SaveTrainInfo(Link l) {
    FILE*fp;
    Node*p;
    int count=0,flag=1;
    fp=fopen("c:\train.txt","wb");//打开只写的二进制
    if(fp==NULL) {
        printf("文件打开失败!");
        return;
    }
    p=l->next;
    while(p) { //遍历p指针
        if(fwrite(p,sizeof(Node),1,fp)==1) { //向磁盘文件写入数据块
            p=p->next;//指针指向xiayiw
            count++;
        } else {
            flag=0;
            break;
        }
    }
    if(flag) {
        printf("有 %d 个火车记录n",count);
        saveflag=0;//保存结束,保存标志清零
    }
    fclose(fp);//关闭文件
}
//保存订票人的信息
void SaveBookInfo(bookLink k) {
    FILE*fp;
    book *p;
    int count=0,flag=1;
    fp=fopen("c:\man.txt","wb");
    if(fp==NULL) {
        printf("文件打开失败!");
        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("有 %d 个订票记录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("c:\train.txt","ab+");//打开存储车票信息的文件
    if((fp1==NULL))
     {
        printf("文件打开失败!");
        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("文件打开失败!");
        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("t请选择序号(0~6):  ");
            scanf("%d",&sel);
            system("CLS");
            if(sel==0)
             {
                if(saveflag==1)
                 { //当退出时判断信息是否保存
                    getchar();
                    printf("n文件已经更改!你要保存吗?(yn)?n");
                    scanf("%c",&ch1);
                        if(ch1=='y'||ch1=='Y')
                        {

                            SaveBookInfo(k);
                            SaveTrainInfo(l);
                        }
                    }
                    printf("n 非常感谢,欢迎你再次来到这个系统!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("n请按任意键继续.......");
                getch();
            }
        }

2018-12-20 11:14
快速回复:求助!火车订票系统,运行出来之后输入相应的序号,为什么没有反应,一 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.065819 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved