火车票订票系统(DEVC++)

1.开发背景

火车票订票系统主要满足了用户对火车票信息的查询和订购需求,同时可以对火车车次信息和订票信息进行保存。

如何实现菜单的选择功能

如何将新输入的信息添加到存放火车票信息的链表中

如何输出满足条件的信息

如何将信息保存到指定的磁盘文件中

2.需求分析

火车订票系统为用户提供预定火车票服务,将火车车次详细信息进行显示,保存;同时提供火车的剩余票数,以供用户查询,决定是否预订;当预定成功后,提供保存用户订票信息的功能。

3.系统设计

3.1系统目标

显示火车车次信息及可供订票数

对输入车次或要到达的城市提供查询

输入要到达城市的车次信息,确定是否订票

可对输入的火车车次信息进行修改

显示火车车票信息

对火车车票信息及订票人的信息进行保存

3.2系统功能结构

3.3系统操作

在主界面上输入“1”,进入输入火车票信息界面,输入火车的车次,起点,终点,出发时间,到达时间,票价和可以订购的票数。

在界面上输入“2”,可以查询火车票信息。查询方法:一是按照车次查询;二是按照想要到达的地方查询。

在界面上输入3,进入订票界面,按照提示输入想要到达的城市,会自动显示出终点站为输入城市的信息,根据提示决定是否订票,以及输入用户的个人信息。

在界面上输入4,进入修改界面,根据提示输入要修改的内容。

在界面上输入5,显示所有的火车票信息。

在界面输入6,进入保存界面,将输入火车票信息及订票人的信息存储在指定的磁盘文件中。

4.预处理模块

4.1预处理模块概述

为了提高程序的可读性,在预处理模块中做了充足的准备工作。在该模块中宏定义了输入、输出语句中的字符串,也使用了自定义结构体类型封装了其中存在的不同类型的零散数据。预处理模块使整个程序的结构简洁清晰,更容易理解。

4.2预处理模块实现

#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 | take off time | 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

/*定义存储火车票信息的结构体*/
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结构体类型的变量*/
	struct node*next;  
}Node,*Link;

/*订票人链表的节点结构*/
typedef struct Man
{
	struct man data;
	struct Man *next;
}book,*bookLink;

 typedef声明了新的类型名Node为node结构体类型,Link为node指针类型,同样也声明了book为Man结构体类型,bookLink为Man结构体的指针类型。

5.主函数设计

5.1主函数概述

在火车订票系统中main()函数中调用menu()函数实现了功能选择菜单的显示

main()函数同时完成了选择菜单的选择功能,即输入菜单中的提示数字,完成相应的功能

5.2技术分析

在main()函数中打开文件是为了将火车票信息和订票人信息保存到该文件,因此需先判断文件中是否有内容。

fopen()函数以读写的方式打开一个二进制文件,如果能够成功打开文件,则测试文件流是否到结尾,即文件中是否有数据。若文件中没有任何数据,则关闭文件;若文件中有数据,则构造链表,读取该磁盘文件中的数据。

保存火车票信息的train.txt文件

保存订票人信息的man.txt文件

5.3主函数实现 

在显示功能选择菜单的函数menu()中主要使用了puts()函数在控制台上输出文字或特殊字符。

main()函数主要应用switch多分支选择结构来实现对菜单中的功能进行调用。

6.输入模块设计

6.1输入模块概述

对火车车次,始发站,终点站,始发时间,到站时间,票价及所剩票数等信息进行输入及保存

为避免添加的车次重复,采用比较函数判断车次是否已经存在,若不存在,则将插入的信息根据提示输入,并插入链表中

  • 25
    点赞
  • 210
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
好的,我们来逐个实现这些功能。 1. 班次录入 我们可以定义一个结构体来存储每个班次的信息,包括班次号、起点站、终点站、出发时间、到达时间、票价等。然后在程序运行时,让用户输入每个班次的信息,并将其存储到一个数组中。 具体实现代码如下: ```c++ #include <iostream> #include <string> using namespace std; // 定义结构体存储班次信息 struct Train { string id; // 班次号 string start; // 起点站 string end; // 终点站 string departTime; // 出发时间 string arriveTime; // 到达时间 int price; // 票价 }; int main() { const int MAX = 100; // 最大班次数目 Train trains[MAX]; // 存储班次信息的数组 int count = 0; // 班次数目 // 输入班次信息 while (count < MAX) { cout << "请输入第 " << count+1 << " 个班次信息:" << endl; cout << "班次号:"; cin >> trains[count].id; if (trains[count].id == "0") { // 输入 0 表示班次信息录入结束 break; } cout << "起点站:"; cin >> trains[count].start; cout << "终点站:"; cin >> trains[count].end; cout << "出发时间:"; cin >> trains[count].departTime; cout << "到达时间:"; cin >> trains[count].arriveTime; cout << "票价:"; cin >> trains[count].price; count++; } // 输出所有班次信息 cout << "所有班次信息如下:" << endl; for (int i = 0; i < count; i++) { cout << "班次号:" << trains[i].id << endl; cout << "起点站:" << trains[i].start << endl; cout << "终点站:" << trains[i].end << endl; cout << "出发时间:" << trains[i].departTime << endl; cout << "到达时间:" << trains[i].arriveTime << endl; cout << "票价:" << trains[i].price << endl; cout << endl; } return 0; } ``` 2. 浏览班次信息 我们可以在程序运行时,输出所有班次的信息,供用户浏览。 具体实现代码如下: ```c++ // 输出所有班次信息 cout << "所有班次信息如下:" << endl; for (int i = 0; i < count; i++) { cout << "班次号:" << trains[i].id << endl; cout << "起点站:" << trains[i].start << endl; cout << "终点站:" << trains[i].end << endl; cout << "出发时间:" << trains[i].departTime << endl; cout << "到达时间:" << trains[i].arriveTime << endl; cout << "票价:" << trains[i].price << endl; cout << endl; } ``` 3. 查询火车信息 我们可以让用户输入起点站和终点站,然后在所有班次中查找符合条件的班次,并输出它们的信息。 具体实现代码如下: ```c++ // 查询火车信息 string start, end; cout << "请输入起点站和终点站,以空格分隔:" << endl; cin >> start >> end; bool flag = false; // 是否有符合条件的班次 for (int i = 0; i < count; i++) { if (trains[i].start == start && trains[i].end == end) { flag = true; cout << "班次号:" << trains[i].id << endl; cout << "起点站:" << trains[i].start << endl; cout << "终点站:" << trains[i].end << endl; cout << "出发时间:" << trains[i].departTime << endl; cout << "到达时间:" << trains[i].arriveTime << endl; cout << "票价:" << trains[i].price << endl; cout << endl; } } if (!flag) { cout << "没有符合条件的班次。" << endl; } ``` 4. 购买火车票 我们可以让用户输入班次号、座位号、乘客姓名等信息,然后将其存储到一个文件中。购票时,还需要检查该班次是否有足够的座位,如果座位已满,则不能购买。 具体实现代码如下: ```c++ // 购买火车票 string id, name; int seat; cout << "请输入班次号、座位号和乘客姓名,以空格分隔:" << endl; cin >> id >> seat >> name; bool found = false; // 是否找到该班次 for (int i = 0; i < count; i++) { if (trains[i].id == id) { found = true; if (seat <= 0 || seat > 100) { // 座位号不合法 cout << "座位号不合法。" << endl; } else if (trains[i].price == 0) { // 该班次已停运 cout << "该班次已停运。" << endl; } else if (seat <= 50 && trains[i].seats[seat-1] != "") { // 座位已被购买 cout << "该座位已被购买,请选择其他座位。" << endl; } else if (seat > 50 && trains[i].seats[seat-51] != "") { // 座位已被购买 cout << "该座位已被购买,请选择其他座位。" << endl; } else { // 购票成功 if (seat <= 50) { trains[i].seats[seat-1] = name; } else { trains[i].seats[seat-51] = name; } cout << "购票成功!" << endl; ofstream outfile(id + ".txt", ios::app); // 打开班次号对应的文件 outfile << seat << " " << name << endl; // 写入购票信息 outfile.close(); } break; } } if (!found) { cout << "没有找到该班次。" << endl; } ``` 5. 退订火车票 我们可以让用户输入班次号、座位号等信息,然后我们从该班次对应的文件中删除对应的购票信息。 具体实现代码如下: ```c++ // 退订火车票 string id; int seat; cout << "请输入班次号和座位号,以空格分隔:" << endl; cin >> id >> seat; bool found = false; // 是否找到该班次 for (int i = 0; i < count; i++) { if (trains[i].id == id) { found = true; if (seat <= 0 || seat > 100) { // 座位号不合法 cout << "座位号不合法。" << endl; } else if (trains[i].price == 0) { // 该班次已停运 cout << "该班次已停运。" << endl; } else if (seat <= 50 && trains[i].seats[seat-1] == "") { // 座位未被购买 cout << "该座位未被购买,无法退订。" << endl; } else if (seat > 50 && trains[i].seats[seat-51] == "") { // 座位未被购买 cout << "该座位未被购买,无法退订。" << endl; } else { // 退票成功 if (seat <= 50) { trains[i].seats[seat-1] = ""; } else { trains[i].seats[seat-51] = ""; } cout << "退票成功!" << endl; ifstream infile(id + ".txt"); // 打开班次号对应的文件 ofstream outfile(id + "_temp.txt"); // 创建临时文件 string line; while (getline(infile, line)) { int s; string n; stringstream ss(line); ss >> s >> n; if (s != seat) { outfile << line << endl; // 将非该座位的购票信息写入临时文件 } } infile.close(); outfile.close(); remove((id + ".txt").c_str()); // 删除原文件 rename((id + "_temp.txt").c_str(), (id + ".txt").c_str()); // 将临时文件重命名为原文件 } break; } } if (!found) { cout << "没有找到该班次。" << endl; } ``` 6. 退出系统 我们可以在程序运行时,提供一个菜单供用户选择操作,当用户选择退出系统时,程序结束运行。 具体实现代码如下: ```c++ // 提供菜单供用户选择操作 while (true) { cout << "请选择要进行的操作:" << endl; cout << "1. 班次录入" << endl; cout << "2. 浏览班次信息" << endl; cout << "3. 查询火车信息" << endl; cout << "4. 购买火车票" << endl; cout << "5. 退订火车票" << endl; cout << "6. 退出系统" << endl; int choice; cin >> choice; switch (choice) { case 1: // 班次录入 // ... break; case 2: // 浏览班次信息 // ... break; case 3: // 查询火车信息 // ... break; case 4: // 购买火车票 // ... break; case 5: // 退订火车票 // ... break; case 6: // 退出系统 return 0; default: cout << "输入不合法,请重新输入。" << endl; break; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值