c语言问题 火车票售卖问题

c语言程序运行不了但是没有报错
(1)程序改成录入班次信息(信息用文件保存),可不定时地增加班次数据
(2)浏览班次信息,可显示出所有班次当前状总(如果当前系统时间超过了某班次的发车时间,则显示“此班已发出”的提示信息)。
(3)查询路线:可按班次号查询 ,可按终点站查询
(4)售票和退票功能

#include<stdio.h>
#include<string.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
void show();
void showone();
void function1();
void function2();
void function3();
void function4();

struct Huoche
{
    const char* TrainNumber;             //车次
    double StartTime;               //开车时间
    const char* StartingStation;         //起点站
    const char* EndingStation;           //终点站
};
struct Huoche Huo[100] = { {"一 ",    8.00  ,   "郫县"    , "广汉"},
    {"二"  ,    6.30 ,      "郫县" ,     "成都"},
    {"三"  ,     7.00   ,    "郫县"  ,    "成都"},
    {"四"  ,      10.00 ,     "郫县"  ,   "成都"}
};
struct chenke
{
    float ArriveTime[20];              //到达时间
    float erding;                        //额定乘客
    float yiding;                        //已定乘客
};
struct chenke ke[100] = { 2,45,30,0.5,40,40,0.5,40,20,0.5,40,2 };
int i, n;

void main()
{
    do
    {
        printf("************************************\n");
        printf("**     欢迎使用火车票管理系统     **\n");
        printf("*         1.添加车次信息           *\n");
        printf("*         2.查询车次信息           *\n");
        printf("*         3.统计火车车次           *\n");
        printf("*         4.浏览火车时刻           *\n");
        printf("*         0.退出                   *\n");
        printf("************************************\n");
        int a;
        printf("请输入执行操作代码:");
        scanf_s("%d", &a);
        switch (a)
        {
        case 1:function1();break;
        case 2:function2();break;
        case 3:function3();break;
        case 4:function4();break;
        case 0:printf("程序结束!!!!!\n");exit(-1);
        }
    } while (1);
}


//循环显示
void show()
{
    for (int i = 0;i < 10;i++)
        printf("%s\t\t%s\t\t%s\t\t%s\t\t%f\t\t%f\t\t%f\n", Huo[i].TrainNumber, Huo[i].StartTime, Huo[i].StartingStation, Huo[i].EndingStation, ke[i].ArriveTime, ke[i].erding, ke[i].yiding);
    printf("\n\n");
}

//显示匹配项
void showone()
{
    printf("%s\t\t%s\t\t%s\t\t%s\t\t%f\t\t%f\t\t%f\n", Huo[i].TrainNumber, Huo[i].StartTime, Huo[i].StartingStation, Huo[i].EndingStation, ke[i].ArriveTime, ke[i].erding, ke[i].yiding);
    printf("\n\n");
}


void function1() {
    printf("请输入录入个数:");
    scanf_s("%d", &n);
    printf("请输入:\n");
    printf("车次\t发车时间\t起点\t终点\t行车时间\t额定乘客\t已定乘客\n");
    for (i = 0;i < n;i++)
        scanf_s("%s%s%s%s%f%f%f", &Huo[i].TrainNumber, &Huo[i].StartTime, &Huo[i].StartingStation, &Huo[i].EndingStation, &ke[i].ArriveTime, &ke[i].erding, &ke[i].yiding);
    printf("!!!!!录入完成!!!!!\n\n");
}

void function2() {
    printf("1——按照车次查询\n");
    printf("2——按照起点和终点查询\n");
    int a;
    printf("请输入执行操作代码:");
    scanf_s("%d", &a);
    switch (a)
    {
    case 1:
    {
        char checi[20];
        printf("请输入所要查询的车次:");
        scanf_s("%s", checi);
        /*    cin>>checi;*/
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < n;i++)
        {
            if (strcmp(checi, Huo[i].TrainNumber) == 1)
                showone();

        }


    }
    break;
    case 2:
    {
        char qidian[20], zhongdian[20];
        printf("请输入起点:");
        scanf_s("%s", &qidian);
        printf("请输入终点:");
        scanf_s("%s", &zhongdian);
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < n;i++)
        {
            if (strcmp(qidian, Huo[i].StartingStation) == 0 && strcmp(zhongdian, Huo[i].EndingStation) == 0)
            {
                showone();

            }
        }
    }
    break;
    }

};
void function3() {
    printf("1——按起点统计\n");
    printf("2——按终点统计\n");
    int a;
    printf("请输入执行操作代码:");
    scanf_s("%d", &a);
    switch (a)
    {
    case 1:
    {
        char qidian1[20];
        printf("请输入起点:");
        scanf_s("%s", &qidian1);
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < n;i++)
        {
            if (strcmp(qidian1, Huo[i].StartingStation) == 0)
            {
                showone();
            }
        }
    }
    break;
    case 2:
    {
        char zhongdian1[20];
        printf("请输入终点:");
        scanf_s("%s", &zhongdian1);
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < n;i++)
        {
            if (strcmp(zhongdian1, Huo[i].EndingStation) == 0)
            {
                showone();
            }
        }
    }
    break;
    }

};
void function4() {
    for (i = 0;i < n - 1;i++)
    {
        for (int j = 0;j < n - 1 - i;j++)
            if (Huo[j].StartTime > Huo[j + 1].StartTime)
            {
                Huoche t = Huo[j];
                Huo[j] = Huo[j + 1];
                Huo[j + 1] = t;
            }
    }
    printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
    show();
};


原代码修改版
大体的思路问题不大,但是代码能力需要提高,可以多注重一下语法基础
基础问题标记在代码的注释中了(在Vscode和dev上测试均通过),还有几处改进,望采纳

#include<stdio.h>
#include<string.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
void show();
void showone();
void function1();
void function2();
void function3();
void function4();

struct Huoche
{
    /*
        const 标记常量不应该乱用
        char 字符串可以改为定义数组不容易错
    */
    char TrainNumber[20];             //车次
    double StartTime;               //开车时间
    char StartingStation[20];         //起点站
    char EndingStation[20];           //终点站
};
struct Huoche Huo[100] = { {"一 ",    8.00  ,   "郫县"    , "广汉"},
    {"二"  ,    6.30 ,      "郫县" ,     "成都"},
    {"三"  ,     7.00   ,    "郫县"  ,    "成都"},
    {"四"  ,      10.00 ,     "郫县"  ,   "成都"}
};
struct chenke
{
    // 数组不要乱用,内外想清楚
    float ArriveTime;              //到达时间
    float erding;                        //额定乘客
    float yiding;                        //已定乘客
};
struct chenke ke[100] = { 2,45,30,0.5,40,40,0.5,40,20,0.5,40,2 };
int i;
int Huocnt = 4; // 火车列表的个数 不好意思之前没有理解n变量的作用 Huocnt和原来的n是一样的

int main()
{
    do
    {
        printf("************************************\n");
        printf("**     欢迎使用火车票管理系统     **\n");
        printf("*         1.添加车次信息           *\n");
        printf("*         2.查询车次信息           *\n");
        printf("*         3.统计火车车次           *\n");
        printf("*         4.浏览火车时刻           *\n");
        printf("*         0.退出                   *\n");
        printf("************************************\n");
        int a;
        printf("请输入执行操作代码:");
        scanf_s("%d", &a);
        switch (a)
        {
        case 1:function1();break;
        case 2:function2();break;
        case 3:function3();break;
        case 4:function4();break;
        case 0:printf("程序结束!!!!!\n");exit(-1);
        }
    } while (1);
}


//循环显示
void show()
{
    /*
        个人建议 毕竟一堆null也不好看
    */
    for (int i = 0;i < 10 && i < Huocnt;i++){
        printf("%s\t\t%lf\t\t%s\t\t%s\t\t%f\t\t%f\t\t%f\n", Huo[i].TrainNumber, Huo[i].StartTime, Huo[i].StartingStation, Huo[i].EndingStation, ke[i].ArriveTime, ke[i].erding, ke[i].yiding);
    }
    printf("\n\n");
}

//显示匹配项
void showone()
{
    // 请时刻关注变量类型double 应该用%lf
    printf("%s\t\t%lf\t\t%s\t\t%s\t\t%f\t\t%f\t\t%f\n", Huo[i].TrainNumber, Huo[i].StartTime, Huo[i].StartingStation, Huo[i].EndingStation, ke[i].ArriveTime, ke[i].erding, ke[i].yiding);
    printf("\n\n");
}


void function1() {
    printf("请输入录入个数:");
    int n;
    scanf_s("%d", &n);
    /*
        如果输入无效直接返回就好了
    */
    if (n <= 0) {
        printf("操作已取消\n\n");
        return ; 
    }
    printf("请输入:\n");
    printf("车次\t发车时间\t起点\t终点\t行车时间\t额定乘客\t已定乘客\n");
    for (i = 0;i < n;i++,Huocnt ++){
        /*
            这里用scanf就行
            另外不要用i 前面的数据会被覆盖
        */
       // 请时刻关注变量类型double 应该用%lf
        scanf("%s%lf%s%s%f%f%f", Huo[Huocnt].TrainNumber, &Huo[Huocnt].StartTime, Huo[Huocnt].StartingStation, Huo[Huocnt].EndingStation, &ke[Huocnt].ArriveTime, &ke[Huocnt].erding, &ke[Huocnt].yiding);
    }
    show();
    printf("!!!!!录入完成!!!!!\n\n");
}

void function2() {
    printf("1——按照车次查询\n");
    printf("2——按照起点和终点查询\n");
    int a;
    printf("请输入执行操作代码:");
    scanf_s("%d", &a);
    switch (a)
    {
    case 1:
    {
        char checi[20];
        printf("请输入所要查询的车次:");
        scanf_s("%s", checi);
        /*    cin>>checi;*/
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < Huocnt;i++)
        {
            // 匹配成功返回0
            if (strcmp(checi, Huo[i].TrainNumber) == 0) {
                showone();
            }
        }
        /*
            break;应该放在这
        */
        break;

    }
    case 2:
    {
        char qidian[20], zhongdian[20];
        printf("请输入起点:");
        scanf_s("%s", &qidian);
        printf("请输入终点:");
        scanf_s("%s", &zhongdian);
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < Huocnt;i++)
        {
            if (strcmp(qidian, Huo[i].StartingStation) == 0 && strcmp(zhongdian, Huo[i].EndingStation) == 0)
            {
                showone();

            }
        }
    }
    break;
    }

};
void function3() {
    printf("1——按起点统计\n");
    printf("2——按终点统计\n");
    int a;
    printf("请输入执行操作代码:");
    scanf_s("%d", &a);
    switch (a)
    {
    case 1:
    {
        char qidian1[20];
        printf("请输入起点:");
        scanf_s("%s", &qidian1);
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < Huocnt;i++)
        {
            if (strcmp(qidian1, Huo[i].StartingStation) == 0)
            {
                showone();
            }
        }
    }
    break;
    case 2:
    {
        char zhongdian1[20];
        printf("请输入终点:");
        scanf_s("%s", &zhongdian1);
        printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
        for (i = 0;i < Huocnt;i++)
        {
            if (strcmp(zhongdian1, Huo[i].EndingStation) == 0)
            {
                showone();
            }
        }
    }
    break;
    }

};
void function4() {
    for (i = 0;i < Huocnt - 1;i++)
    {
        for (int j = 0;j < Huocnt - 1 - i;j++)
            if (Huo[j].StartTime > Huo[j + 1].StartTime)
            {
                /*
                    struct 变量不能省略哦
                */
                struct Huoche t = Huo[j];
                Huo[j] = Huo[j + 1];
                Huo[j + 1] = t;
            }
    }
    printf("车次\t\t发车时间\t\t起点\t\t终点\t\t行车时间\t\t额定乘客\t\t已定乘客\n");
    show();
};

调试这种隐性错误很难,望采纳采纳采纳!感谢

可以检查程序是否存在编译错误或运行时错误,或者调用了某些未定义的函数。建议使用调试工具来查看程序的执行过程

看起来您正在尝试创建一个程序来管理火车票销售。以下是您的代码的一些潜在问题:

1.结构“Huoche”有一个数组成员“StartTime”,但它被定义为双精度。最好定义成double类型的数组,这样可以存储多个开始时间。

2.结构“chenke”有一个数组成员“ArriveTime”,但它被定义为一个浮点数组。最好将其定义为double数组,以匹配“Huoche”结构中“StartTime”成员的类型。

3.在 main 函数中,您有一个“do-while”循环,提示用户输入操作代码,然后执行相应的函数。但是,您没有包含让用户输入值 0 以退出程序的方法。结果,循环将无限期地继续下去。

4.函数“function1”到“function4”定义在主函数的末尾,这不是 C 编程中的标准做法。通常最好先定义函数,然后再在主函数中使用它们。

5.函数“function1”应该将列车信息添加到程序中,但缺少实际执行此操作的代码。因此,该函数在调用时不会产生任何影响。

6.函数“function2”应该搜索火车信息,但缺少执行此操作的代码。因此,该函数在调用时不会产生任何影响。

7.函数“function4”应该显示火车时刻表,但缺少执行此操作的代码。因此,该函数在调用时不会产生任何影响。

我希望这有帮助!如果您有任何其他问题或需要更多说明,请告诉我。

C语言如果没错误的话,需要经过编译后才能生成可执行文件,然后才能执行的。
如果“最后显示无法执行程序”,如果需要的资源超出了系统能够提供的范围,也会出现这样的问题的。这需要把具体的程序代码拿出来,才能分析原因的。

需要有人写出正确运行代码 谢谢了

给你个参考吧

借鉴下

程序设计要求用C语言写一个简单的火车售票系统,主要实现的功能为:

录入班次信息

浏览班次信息

按班次号查询

按终点站查询

按余票数量排序保存

售票

退票

更新班次信息

退出系统

所有的班次信息保存在number.dat文件中,排序过后的保存在sort.dat中(.dat是一种二进制文件)。

在编写的过程中我觉得在判断火车的状态比较值得深究。这里假设火车主要有四种状态:

1.未发车

2.已发车

3.停止检票

4.停止退票

在程序中,思路是将代表发车时间的字符串转化为整型,再和系统现在的时间进行大小比较,主要采用if判断各种情况。其中atime代表的是发车时间的整型数,btime代表的是系统时间的整型数,具体实现如下:

if(atime<=btime) //已经发车

return 1;

if(((atime-btime<=30)&&(atime-btime>5)&&(atime/100==btime/100))||(((atime%100+(60-btime%100))<=30)&&(atime%100+(60-btime%100))>5&&(atime/100-btime/100==1))) //距发车半小时以内,停止退票,%表示取余

return 2;

if(((atime-btime<=5)&&(atime/100==btime/100))||((atime%100+(60-btime%100)&&(atime/100-btime/100==1))<=5)) //距发车前五分钟内停止检票

return 3;

return 0; //可以办理购退票

在判断退票时如果两个时间的小时数是一样的,则它们的分钟数如果相差在30之内即半小时之内或者发车时间的小时和系统时间的小时相差一个1,并且发车时间的分钟数小于30,系统时间的分钟大于30,则它们之间也就相差在30之内,此时代表停止退票。

判断检票和上面思想大致相同。

为了参考研究,贴上代码:

#include

#include

#include

#include

#include "time.h"

//班次信息表

#define SIZELIMIT 10 //每班次的具体信息的字符个数限制在10以内

#define MAXNUM 1000 //设定最多只能录入1000个车次信息

typedef struct cardbase //定义一个关于班次信息的结构体,取名为CardBase

{

char C_ID[SIZELIMIT];//班次

char C_TIME[SIZELIMIT];//发车时间

char C_ANAME[SIZELIMIT];//起点站

char C_BNAME[SIZELIMIT];//终点站

char C_USETIME[SIZELIMIT];//行车时间

char C_MAXNUM[SIZELIMIT];//额定载量

char C_REMAINNUM[SIZELIMIT];//余票数量

}CardBase;

int cbNum=0;//记录班次数

CardBase cBList[MAXNUM];//班次列表

//读取班次信息

void readcardbasefile()

{

FILE *fp;

//打开文件失败则创建文件

if((fp=fopen("./number.dat","r"))==NULL)

{

//初次运行创建文件

if ((fp=fopen("./number.dat","w"))==NULL)

{

exit(0); //返回

}

else

{

fclose(fp);

}

return ;

}

/*文件位置指针移动到文件末尾*/

fseek(fp,0,2);//重定位文件内部位置指针

/* int fseek( FILE *stream, long offset, int origin );

第一个参数stream为文件指针

第二个参数offset为偏移量,正数表示正向偏移,负数表示负向偏移

第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET

SEEK_SET: 文件开头

SEEK_CUR: 当前位置

SEEK_END: 文件结尾

其中SEEK_SET,SEEK_CUR和SEEK_END依次为012.

简言之:

fseek(fp,100L,0);把stream指针移动到离文件开头100字节处;

fseek(fp,100L,1);把stream指针移动到离文件当前位置100字节处;

fseek(fp,-100L,2);把stream指针退回到离文件结尾100字节处。 */

if (ftell(fp)>0)//文件不为空

/*ftell函数用于得到文件位置指针当前位置相对于文件首的偏移字节数

也就是得到文件所包含的字节数,如果大于0,则代表文件非空*/

{

//文件位置指针移动到文件开始

rewind(fp);

char buff[10]={0};

for (cbNum=0;!feof(fp) && fread(&cBList[cbNum],sizeof(CardBase),1,fp);cbNum++)

/*对于feof函数,如果遇到文件结束,函数feof(fp)的值为非零值,否则为0。

即如果文件结束,!feof(fp)为0,跳出循环

对于fread函数,是从一个文件流中读数据,如果调用成功返回实际读取到的项个数,

如果不成功或读到文件末尾返回 0*/

fgets(buff,10,fp);

/*char *fgets(char *buf, int bufsize, FILE *stream);

从文件结构体指针stream中读取数据,每次读取一行。读取的数据保存在buf指向的字符数组中,

每次最多读取bufsize-1个字符(第bufsize个字符赋'\0'),如果文件中的该行,不足bufsize个字符,

则读完该行就结束。如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只返回一个不完整的行,

但是,缓冲区总是以NULL字符结尾,对fgets的下一次调用会继续读该行。函数成功将返回buf,失败或读到文件结尾返回NULL。

因此我们不能直接通过fgets的返回值来判断函数是否是出错而终止的,应该借助feof函数或者ferror函数来判断。

*/

fclose(fp);

}

else

{

fclose(fp);

}

return;

}

//保存班次信息

void writecardbasefile()

{

int i;

FILE *fp;

if ((fp=fopen("./number.dat","w"))==NULL)

{

printf("系统错误");

}

char buff[10]={0};

strcpy(buff,"\r\n");

for (i=0;i

{

if (fwrite(&cBList[i],sizeof(CardBase),1,fp)!=1)

{

printf("系统错误");

}

if (fwrite(buff,2,1,fp)!=1)

{

printf("系统错误");

}

}

fclose(fp);

}

//保存排序信息

void writesortfile()

{

int i;

FILE *fp;

if ((fp=fopen("./sort.dat","w"))==NULL)

{

printf("系统错误");

}

char buff[10]={0};

strcpy(buff,"\r\n");

for (i=0;i

{

if (fwrite(&cBList[i],sizeof(CardBase),1,fp)!=1)

{

printf("系统错误");

}

if (fwrite(buff,2,1,fp)!=1)

{

printf("系统错误");

}

}

fclose(fp);

}

//打印并输入后返回

void printReturn(char *info)

{

printf("\n\n\t %s",info);

fflush(stdin);//清空输入缓冲区,通常是为了确保不影响后面的数据读取.

getchar();

}

//输入信息

void setInfo(char pinfo[1024],char desinfo[])

{

printf("\n\t%s:",pinfo);

fflush(stdin);

scanf("%s",desinfo);

}

//系统初始化

void initsystem()

{

readcardbasefile();

};

//录入班次

void infoinput()

{

setInfo("班次",cBList[cbNum].C_ID);

setInfo("发车时间(24小时制)",cBList[cbNum].C_TIME);

setInfo("起点站",cBList[cbNum].C_ANAME);

setInfo("终点站",cBList[cbNum].C_BNAME);

setInfo("行车时间",cBList[cbNum].C_USETIME);

setInfo("额定载量",cBList[cbNum].C_MAXNUM);

setInfo("余票数量",cBList[cbNum].C_REMAINNUM);

cbNum++;

writecardbasefile();

printReturn("\n\t录入成功,回车键返回");

};

//时间比较

int timecmp(char A[10])

{

//将火车时间转成整数

char tempa[10]={0};

int ta=0;

int i;

for(i=0;i

if(A[i]!=':'&&A[i]!=':') //冒号的中文输入和英文输入

{

tempa[ta]=A[i];

ta++;

}

int atime=atoi(tempa);//把字符串变成整型

//获取系统时间

char tempb[10]={0};

time_t t = time(0);

strftime( tempb, 10, "%H%M",localtime(&t) );

int btime=atoi(tempb);

//比较

if(atime<=btime) //已经发车

return 1;

if(((atime-btime<=30)&&(atime-btime>5)&&(atime/100==btime/100))||(((atime%100+(60-btime%100))<=30)&&(atime%100+(60-btime%100))>5&&(atime/100-btime/100==1))) //距发车半小时以内,停止退票,%表示取余

return 2;

if(((atime-btime<=5)&&(atime/100==btime/100))||((atime%100+(60-btime%100)&&(atime/100-btime/100==1))<=5)) //距发车前五分钟内停止检票

return 3;

return 0; //可以办理购退票

}

//浏览所有班次

void queryallinfo()

{

printf("班次信息\n");

printf("班次 发车时间 起点站 终点站 行车时间 额定载量 余票数量 状态\n");

int i;

for(i=0;i

{

char temp[20]={0};

strcpy(temp,"未发车");

if(1==timecmp(cBList[i].C_TIME))

strcpy(temp,"已发车");

if(2==timecmp(cBList[i].C_TIME))

strcpy(temp,"停止退票");

if(3==timecmp(cBList[i].C_TIME))

strcpy(temp,"停止检票");

printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s%s\n",

cBList[i].C_ID,cBList[i].C_TIME,

cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME,

cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM,temp );

}

printReturn("\n\t回车键返回");

};

//通过班次号查路线

void queryinfobyID()

{

char ID[20]={0};

setInfo("输入班次号",ID);

int i;

for( i=0;i

{

if(strcmp(cBList[i].C_ID,ID)==0)

{

printf("班次信息\n");

printf("班次 发车时间 起点站 终点站 行车时间 额定载量 余票数量\n");

printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n",

cBList[i].C_ID,cBList[i].C_TIME,

cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME,

cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM );

printReturn("\n\t回车键返回");

return;

}

}

printReturn("\n\t指定信息不存在,回车键返回");

};

//通过终点站查路线

void queryinfobyBNAME()

{

char Name[20]={0};

setInfo("输入终点站",Name);

int i;

for(i=0;i

{

if(strcmp(cBList[i].C_BNAME,Name)==0)

{

printf("班次信息\n");

printf("班次 发车时间 起点站 终点站 行车时间 额定载量 余票数量\n");

printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n",

cBList[i].C_ID,cBList[i].C_TIME,

cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME,

cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM );

printReturn("\n\t回车键返回");

return;

}

}

printReturn("\n\t指定信息不存在,回车键返回");

};

//排序按余票数量排序保存

void sortSave()

{

//冒泡排序

int i,j;

for(i=0;i

for(j=0;j

{

if(atoi(cBList[j].C_REMAINNUM)

{

CardBase temp=cBList[j];

cBList[j]=cBList[j+1];

cBList[j+1]=temp;

}

}

//打印排序结果

queryallinfo();

//保存排序结果

writesortfile();

};

//售票

void sale()

{

char ID[20]={0};

setInfo("输入班次号",ID);

int i;

for(i=0;i

{

if(strcmp(cBList[i].C_ID,ID)==0)

{

if(cBList[i].C_REMAINNUM==0)

{

printReturn("\n\t余票不足,回车返回");

return;

}

//余票减少

int temp=atoi(cBList[i].C_REMAINNUM)-1;

if(temp<0) temp=0;//确保余票不为负数

_itoa(temp,cBList[i].C_REMAINNUM,10);

//保存到文件

writecardbasefile();

printf("班次信息\n");

printf("班次 发车时间 起点站 终点站 行车时间 额定载量 余票数量\n");

printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n",

cBList[i].C_ID,cBList[i].C_TIME,

cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME,

cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM );

printReturn("\n\t售票成功,回车返回");

return;

}

}

printReturn("\n\t指定班次不存在,回车键返回");

};

//退票

void back()

{

char ID[20]={0};

setInfo("输入班次号",ID);

int i;

for(i=0;i

{

if(strcmp(cBList[i].C_ID,ID)==0)

{

//余票增加

int temp=atoi(cBList[i].C_REMAINNUM)+1;

_itoa(temp,cBList[i].C_REMAINNUM,10);

//保存到文件

writecardbasefile();

printf("班次信息\n");

printf("班次 发车时间 起点站 终点站 行车时间 额定载量 余票数量\n");

printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n",

cBList[i].C_ID,cBList[i].C_TIME,

cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME,

cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM );

printReturn("\n\t\t退票成功,回车返回");

return;

}

}

printReturn("\n\t指定班次不存在,回车键返回");

};

//更新车次信息

void update()

{

int flag;

char ID[20]={0};

setInfo("请输入要删除或修改的车次(请确认车次输入正确)",ID);

do

{

printf("\n\t按下列提示更新车次信息(退出修改请按0):");

printf("\n\t1:删除车次;2:修改发车时间;3:修改起点站;4:修改终点站;");

printf("\n\t5:修改行车时间;6:修改额定载量;7:修改余票数量;0:退出修改;");

printf("\n\t请选择:");

scanf("%d",&flag);

int i;

CardBase temp;

for(i=0;i

{

if(strcmp(cBList[i].C_ID,ID)==0)

{

if(flag==1) //删除车次信息

{

int j;

for(j=i;j

cBList[j]=cBList[j+1];//后面的数据覆盖前面的数据

cbNum--;//车次数减一

//保存到文件

writecardbasefile();

}

if(flag==2) //修改发车时间

{

setInfo("请输入新的发车时间",cBList[i].C_TIME);

temp=cBList[i];

//保存到文件

writecardbasefile();

}

if(flag==3) //修改起点站

{

setInfo("请输入新的起点站",cBList[i].C_ANAME);

temp=cBList[i];

//保存到文件

writecardbasefile();

}

if(flag==4) //修改终点站

{

setInfo("请输入新的终点站",cBList[i].C_BNAME);

temp=cBList[i];

//保存到文件

writecardbasefile();

}

if(flag==5) //修改行车时间

{

setInfo("请输入新的行车时间",cBList[i].C_USETIME);

temp=cBList[i];

//保存到文件

writecardbasefile();

}

if(flag==6) //修改额定载量

{

setInfo("请输入新的额定载量",cBList[i].C_MAXNUM);

temp=cBList[i];

//保存到文件

writecardbasefile();

}

if(flag==7) //修改余票数量

{

setInfo("请输入新的余票数量",cBList[i].C_REMAINNUM);

temp=cBList[i];

//保存到文件

writecardbasefile();

}

}

}

//更改完成后显示更新后的班次信息,如果是删除车次则不显示,即flag不等于0的时候显示更新信息

if(flag!=1&&flag!=0)

{ printf("更新后的班次信息\n");

printf("班次 发车时间 起点站 终点站 行车时间 额定载量 余票数量\n");

printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n",

temp.C_ID,temp.C_TIME,

temp.C_ANAME,temp.C_BNAME,temp.C_USETIME,

temp.C_MAXNUM,temp.C_REMAINNUM );

}

}while(flag);

printReturn("\n\t完成车次信息更新,回车键返回");

};

void mainmenu()

{

while(1)

{

char select;

do{

system("cls");//清屏

printf("\n\t ╭═════════■□■□═══╮");

printf("\n\t│ 火车班次系统 │");

printf("\n\t╰═══■□■□══════════╯");

printf("\n\t ┌────────────────┐");

printf("\n\t │1.录入班次 │");

printf("\n\t │2.浏览所有班次 │");

printf("\n\t │3.通过班次号查路线 │");

printf("\n\t │4.通过终点站查路线 │");

printf("\n\t │5.排序保存 │");

printf("\n\t │6.售票 │");

printf("\n\t │7.退票 │");

printf("\n\t │8.更新车次信息 │");

printf("\n\t │0.退出登录 │");

printf("\n\t └────────────────┘");

printf("\n\t 请选择:");

fflush(stdin);//清空输入缓冲区,通常是为了确保不影响后面的数据读取.

select=getchar();//等待用户输入数据

}while (select'8');

switch(select)

{

case '0':exit(0);break;

case '1':infoinput();break;

case '2':queryallinfo();break;

case '3':queryinfobyID();break;

case '4':queryinfobyBNAME();break;

case '5':sortSave();break;

case '6':sale();break;

case '7':back();break;

case '8':update();break;

}

}

}

//主函数

int main()

{

initsystem(); //系统初始化

while(1)

{

mainmenu();

}

}

链表写的,参考一下

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#define HEADER1 " -------------------------------BOOK TICKET----------------------------------\n"
#define HEADER2 " |  number  |start city|reach city|takeofftime|receivetime|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 ;
    struct node * next ;
}Node,*Link ;
/*定义订票人链表的结点结构*/
typedef struct Man 
{
    struct man data ;
    struct Man *next ;
}book,*bookLink ;
/* 初始界面*/
void menu()
{
    puts("\n\n");
    puts("\t\t|------------------------------------------------------|");
    puts("\t\t|                   Booking Tickets                    |");
    puts("\t\t|------------------------------------------------------|");
    puts("\t\t|       0:quit the system                              |");
    puts("\t\t|       1:Insert a train information                   |");
    puts("\t\t|       2:Search a train information                   |");
    puts("\t\t|       3:Book a train ticket                          |");
    puts("\t\t|       4:Modify the train information                 |");
    puts("\t\t|       5:Show the train information                   |");
    puts("\t\t|       6:save information to file                     |");
    puts("\t\t|------------------------------------------------------|");
}
/*添加一个火车信息*/
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 receive:");
    scanf("%s",&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 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)
    {
        printf("Input the the number of train:");
        scanf("%s",str1);
        r=l->next;
    while(r!=NULL)
        if(strcmp(r->data.num,str1)==0)/*检索是否有与输入的车号相匹配的*/
        {
            s[i]=r;
        i++;
        break;
        }
        else 
            r=r->next;
    }
    else if(sel==2)
    {
        printf("Input the city  you want to go:");
        scanf("%s",str2);
        r=l->next;
    while(r!=NULL)
        if(strcmp(r->data.reachcity,str2)==0)/*检索是否有与输入的城市相匹配的火车*/
        {
            s[i]=r;
        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]);
    }
}

/*订票子模块*/
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 ;
    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<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 your 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("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",&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 ;
        }
    }
}
/*修改火车信息*/
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",&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)
                {
                    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.reachcity);
                    printf("Input new time the train take off");
            scanf("%s",&p->data.takeofftime);
                    printf("Input new time the train reach:");
            scanf("%s",&p->data.receivetime);
                    printf("Input new price of the ticket::");
                    scanf("%d",&p->data.price);
                    printf("Input new number of people who have booked ticket:");
                    scanf("%d",&p->data.ticketnum);
                    printf("\nmodifying record is sucessful!\n");
                    saveflag=1 ;
                }
                else
                printf("\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)
    {
        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 changed!do you want to save it(y/n)?\n");
                scanf("%c",&ch1);
                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();
    }
}