iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C语言实现火车订票系统
  • 434
分享到

C语言实现火车订票系统

C语言火车订票系统C语言火车订票C语言订票系统 2022-11-13 14:11:49 434人浏览 薄情痞子
摘要

本文实例为大家分享了C语言实现火车订票系统的具体代码,供大家参考,具体内容如下 程序介绍 1.运行程序时,首先进入到菜单部分,菜单部分提供了菜单显示和输入功能部分。其运行效果如图所示

本文实例为大家分享了C语言实现火车订票系统的具体代码,供大家参考,具体内容如下

程序介绍

1.运行程序时,首先进入到菜单部分,菜单部分提供了菜单显示和输入功能部分。其运行效果如图所示。在主界面上输入数字0——6,实现相应的功能。

2.主界面输入“1”,进入添加火车信息界面,如图所示。根据屏幕上给出的提示输入火车的车次,起点,终点,出发时间,到达时间,票价和可以订购的票数。

3.主界面输入“2”,可以查询火车信息,可以选择查询的方法有两种,一种是按照车次查询,一种是按照你想要到达的地方查询,运行效果如图所示。

4.当在主界面输入“3”时,进入订票界面,按照提示输入你想要到达的城市,会自动显示出你终点站为你输入城市的信息,根据提示输入你是否决定订票以及你的个人信息,运行效果如图所示。

5.当在主界面输入“4”时,进入修改界面,根据提示输入你要修改的内容,修改模块的运行效果如图所示。

6.当在主界面输入“5”时,可以显示出所有的火车信息,显示模块效果如图所示。

7.当在主界面输入“6”时,进入到保存模块,将录入的火车信息进行保存,并且将订票人的信息也进行保存,存储在指定的磁盘文件中。运行效果如图所示。

代码

#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];
    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 ;
            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)
           {
               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)
        {
            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();
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: C语言实现火车订票系统

本文链接: https://www.lsjlt.com/news/171918.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C语言实现火车订票系统
    本文实例为大家分享了C语言实现火车订票系统的具体代码,供大家参考,具体内容如下 程序介绍 1.运行程序时,首先进入到菜单部分,菜单部分提供了菜单显示和输入功能部分。其运行效果如图所示...
    99+
    2022-11-13
    C语言火车订票系统 C语言火车订票 C语言订票系统
  • C语言实现火车票管理系统
    本文为大家分享了C语言实现火车票管理系统课程设计的具体代码,供大家参考,具体内容如下 1、前言 这是大一刚学C语言一个学期完成的课设项目,里面的功能还可以进一步的完善,仅供分享、参考...
    99+
    2024-04-02
  • C语言实现车票管理系统
    本文实例为大家分享了C语言实现车票管理系统的具体代码,供大家参考,具体内容如下 一、项目简介 设计一个车票管理系统实现录入、查看班次信息,售票,退票等基本功能。设计中要求综合运用所学...
    99+
    2024-04-02
  • Java实战之火车票预订系统的实现
    目录 一、项目运行二、效果图三、核心代码个人中心Controller管理员和员工登陆控制用户管理操作 一、项目运行 环境配置: Jdk1.8 + Tomcat8....
    99+
    2024-04-02
  • C语言如何实现车票管理系统
    今天小编给大家分享一下C语言如何实现车票管理系统的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。一、项目简介设计一个车票管理系...
    99+
    2023-06-30
  • C语言如何实现航空订票系统
    本篇内容介绍了“C语言如何实现航空订票系统”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!利用二进制将一整个结构体中的数据存入文件,然后读取文...
    99+
    2023-06-29
  • 如何利用C++实现一个简单的火车票订购系统?
    随着人们工作和生活方式的变化,越来越多的人选择乘坐火车出行。因此,实现一个简单的火车票订购系统可以方便用户预订车票,同时也可以提高工作效率,减少人力投入。本文将介绍如何使用C++实现一个简单的火车票订购系统,以方便读者学习和实践。一、需求分...
    99+
    2023-11-03
    C++ 火车票 订购系统
  • Java实现火车票预订系统的代码怎么写
    本文小编为大家详细介绍“Java实现火车票预订系统的代码怎么写”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java实现火车票预订系统的代码怎么写”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。 一、项...
    99+
    2023-06-29
  • C语言实现航空订票系统课程设计
    本文实例为大家分享了C语言实现航空订票系统的具体代码,供大家参考,具体内容如下 大一写的时候没有写注释,后来也懒得加了。在这里说一下读写文件的思路吧。 就是利用二进制将一整个结构体中...
    99+
    2024-04-02
  • 如何使用C语言实现飞机订票系统
    这篇文章主要介绍了如何使用C语言实现飞机订票系统的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何使用C语言实现飞机订票系统文章都会有所收获,下面我们一起来看看吧。总体设计和需求分析设计目的怎样去合理的设计一个...
    99+
    2023-07-02
  • C++实现车票管理系统
    本文实例为大家分享了C++实现车票管理系统的具体代码,供大家参考,具体内容如下 一车站每天有n个发车班次,每个班次都有一班次号(1、2、3…n),固定的发车时间,固定的...
    99+
    2024-04-02
  • C语言实现飞机订票系统的完整代码
    目录题目总体设计和需求分析设计目的总体设计和功能结构体设计机票信息结构体主函数的设计各功能代码的实现前置添加机票查找机票信息修改机票信息显示机票信息推荐机票信息订票退票保存信息显示时...
    99+
    2024-04-02
  • C++实现飞机订票系统
    本文实例为大家分享了C++实现飞机订票系统的具体代码,供大家参考,具体内容如下 // 飞机订票系统.cpp : 定义控制台应用程序的入口点。 // #include "stdafx...
    99+
    2024-04-02
  • (附源码)spring boot火车订票系统 毕业设计 031012
    火车订票系统的设计与实现 摘 要 信息化社会内需要与之针对性的信息获取途径,但是途径的扩展基本上为人们所努力的方向,由于站在的角度存在偏差,人们经常能够获得不同类型信息,这也是技术最为难以攻克的课题。针对火车票预定等问题,对订票系统进行...
    99+
    2023-09-07
    spring boot java python php html Powered by 金山文档
  • java/php/node.js/python火车订票管理系统【2024年毕设】
    本系统带文档lw万字以上 文末可领取本课题的JAVA源码参考 开发环境 开发语言:Java 框架:ssm 技术:ssm+vue JDK版本:JDK1.8 服务器:tomcat7 数据库:mysql 5.7或8.0 数据库工具:Navica...
    99+
    2023-10-06
    java 课程设计 开发语言
  • C语言实现飞机售票系统
    本文实例为大家分享了C语言实现飞机售票系统的具体代码,供大家参考,具体内容如下 一、项目简介 通过“航空售票系统”C语言课程设计的实践,掌握函数、数据的读取和...
    99+
    2024-04-02
  • C++怎么实现车票管理系统
    这篇文章主要介绍“C++怎么实现车票管理系统”,在日常操作中,相信很多人在C++怎么实现车票管理系统问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么实现车票管理系统”的疑惑有所帮助!接下来,请跟着小编...
    99+
    2023-06-29
  • C语言实现停车管理系统
    本题为大家分享了C语言实现停车管理系统的具体代码,供大家参考,具体内容如下 设计题目 设停车场是一个可以停放n辆汽车的南北方向的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按...
    99+
    2024-04-02
  • Python编写车票订购系统 Python实现快递收费系统
    本文实例为大家分享了Python编写车票订购系统,Python实现快递收费系统的具体代码,供大家参考,具体内容如下 要求: 1.上网查询郑州到北京,西安,石家庄,济南,太原,武汉的距...
    99+
    2024-04-02
  • C语言实现简易订餐系统
    本文实例为大家分享了C语言实现简易订餐系统的具体代码,供大家参考,具体内容如下 主要功能: (1)菜单维护(餐厅管理人员使用)采用顺序表实现 1、添加新菜 2、删除菜品 3、修改菜品...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作