C++编写汽车站车票管理系统,功能包含增加车次信息,查看所有车次信息,车辆信息查询,注销车次,车辆信息查询,购票,退票案例

时间: 2023-08-12 14:48:13 浏览: 67
好的,下面是一个简单的汽车站车票管理系统的实现示例: ```c++ #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; // 车次信息结构体 struct Train { string id; // 车次号 string start; // 起始站点 string end; // 终点站点 string time; // 发车时间 double price; // 车票价格 int seats; // 座位数量 }; // 全局变量,存储所有车次信息 vector<Train> trains; // 从文件中读取车次信息 void loadTrainsFromFile() { ifstream fin("trains.txt"); if (!fin.is_open()) { cout << "无法打开文件 trains.txt" << endl; return; } trains.clear(); string line; while (getline(fin, line)) { Train train; sscanf(line.c_str(), "%s %s %s %s %lf %d", &train.id, &train.start, &train.end, &train.time, &train.price, &train.seats); trains.push_back(train); } fin.close(); } // 将车次信息写入文件 void saveTrainsToFile() { ofstream fout("trains.txt"); for (auto train : trains) { fout << train.id << " " << train.start << " " << train.end << " " << train.time << " " << train.price << " " << train.seats << endl; } fout.close(); } // 显示所有车次信息 void showAllTrains() { cout << "所有车次信息:" << endl; for (auto train : trains) { cout << train.id << " " << train.start << " -> " << train.end << " " << train.time << " " << train.price << " " << train.seats << endl; } } // 查询车次信息 void searchTrains() { string query; cout << "请输入查询关键字:"; cin >> query; bool found = false; for (auto train : trains) { if (train.id == query || train.start == query || train.end == query || train.time == query) { cout << train.id << " " << train.start << " -> " << train.end << " " << train.time << " " << train.price << " " << train.seats << endl; found = true; } } if (!found) { cout << "没有找到匹配的车次信息" << endl; } } // 增加车次信息 void addTrain() { Train train; cout << "请输入车次号:"; cin >> train.id; cout << "请输入起始站点:"; cin >> train.start; cout << "请输入终点站点:"; cin >> train.end; cout << "请输入发车时间:"; cin >> train.time; cout << "请输入车票价格:"; cin >> train.price; cout << "请输入座位数量:"; cin >> train.seats; trains.push_back(train); saveTrainsToFile(); cout << "车次信息已添加" << endl; } // 注销车次信息 void removeTrain() { string id; cout << "请输入要注销的车次号:"; cin >> id; bool found = false; for (auto it = trains.begin(); it != trains.end(); ++it) { if (it->id == id) { trains.erase(it); found = true; break; } } if (found) { saveTrainsToFile(); cout << "车次信息已注销" << endl; } else { cout << "没有找到匹配的车次信息" << endl; } } // 购票 void buyTicket() { string id; cout << "请输入要购买的车次号:"; cin >> id; bool found = false; for (auto it = trains.begin(); it != trains.end(); ++it) { if (it->id == id) { if (it->seats > 0) { // 有余票,可以购买 --it->seats; saveTrainsToFile(); cout << "车票已购买" << endl; } else { // 没有余票 cout << "该车次已无余票" << endl; } found = true; break; } } if (!found) { cout << "没有找到匹配的车次信息" << endl; } } // 退票 void returnTicket() { string id; cout << "请输入要退票的车次号:"; cin >> id; bool found = false; for (auto it = trains.begin(); it != trains.end(); ++it) { if (it->id == id) { ++it->seats; saveTrainsToFile(); cout << "车票已退回" << endl; found = true; break; } } if (!found) { cout << "没有找到匹配的车次信息" << endl; } } int main() { loadTrainsFromFile(); // 从文件中读取车次信息 while (true) { cout << "请选择操作:" << endl; cout << "1. 查看所有车次信息" << endl; cout << "2. 查询车次信息" << endl; cout << "3. 增加车次信息" << endl; cout << "4. 注销车次信息" << endl; cout << "5. 购票" << endl; cout << "6. 退票" << endl; cout << "0. 退出" << endl; int choice; cin >> choice; switch (choice) { case 1: showAllTrains(); break; case 2: searchTrains(); break; case 3: addTrain(); break; case 4: removeTrain(); break; case 5: buyTicket(); break; case 6: returnTicket(); break; case 0: return 0; default: cout << "无效的选择" << endl; break; } } return 0; } ``` 这个程序使用了结构体 `Train` 来表示每个车次的信息,使用了容器 `vector` 来存储所有车次信息,并使用了文件读写来实现数据的持久化。用户可以通过菜单选择不同的操作来实现对车票信息的管理,包括查看所有车次信息、查询车次信息、增加车次信息、注销车次信息、购票和退票等。当用户进行某个操作时,程序会调用相应的函数来实现该功能,例如在购票功能中,程序会查找匹配的车次信息并更新座位数量,然后将更新后的信息写入文件中。

最新推荐

recommend-type

汽车租赁信息管理系统源代码 c++.docx

一、为了方便公司管理各种型号的车辆,并实现以下功能: (1)对车辆进行租赁:先输入车牌号,然后输入车辆类别、品牌型号,并在库存中查找该车辆的相关信息,并进行租车。 (2)添加新的车辆信息:主要完成车辆信息...
recommend-type

信息学奥赛一本通(C++版)在线评测系统-答案-1000-1009.docx

信息学奥赛一本通(C++版)在线评测系统,答案,1000-1009,前10道题。http://ybt.ssoier.cn:8088/problem_list.php?page=
recommend-type

C++学校人员信息管理系统课程设计.doc

一个用C++写的学校人员管理系统,适合大一大二的课程设计,里面运用到了链表、文件读写等一些C++基本语法操作
recommend-type

C++使用MFC获取PC硬件配置信息

主要为大家详细介绍了C++使用MFC获取PC硬件配置信息,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

c++获取进程信息列表和进程所调用的dll列表

主要介绍了c++获取进程信息列表和进程所调用的dll列表,大家参考使用吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。