高速公路计费系统设计C++代码。要求:对三种类型的车辆计费,大型车每公里0.5元,中型车每公里0.4元,小型车每公里0.3元。设计三个收费路口,有数据的添加,修改,删除等功能,交互式界面,加入文件存储。在交班时统计数量,完整代码

时间: 2023-08-12 10:06:29 浏览: 41
很好,这道题目涉及到了 C++ 的面向对象编程和文件存储等知识点,需要我们综合运用所学的知识点进行编程。下面是我的实现: 首先,我们需要定义车辆类型的枚举和计费系统类: ```c++ enum CarType { SMALL_CAR, MEDIUM_CAR, LARGE_CAR }; class TollSystem { private: int small_car_count; int medium_car_count; int large_car_count; public: TollSystem() { small_car_count = 0; medium_car_count = 0; large_car_count = 0; } double calculateFee(CarType type, double distance) { double fee = 0.0; switch (type) { case SMALL_CAR: fee = distance * 0.3; small_car_count++; break; case MEDIUM_CAR: fee = distance * 0.4; medium_car_count++; break; case LARGE_CAR: fee = distance * 0.5; large_car_count++; break; } return fee; } void printStatistics() { cout << "Small car count: " << small_car_count << endl; cout << "Medium car count: " << medium_car_count << endl; cout << "Large car count: " << large_car_count << endl; } }; ``` 接着,我们需要定义收费路口类: ```c++ class TollGate { private: TollSystem* system; public: TollGate() { system = new TollSystem(); } double charge(CarType type, double distance) { double fee = system->calculateFee(type, distance); cout << "Charge: " << fee << endl; return fee; } void printStatistics() { system->printStatistics(); } ~TollGate() { delete system; } }; ``` 最后,我们需要实现交互式界面和文件存储功能: ```c++ int main() { TollGate* toll_gate_1 = new TollGate(); TollGate* toll_gate_2 = new TollGate(); TollGate* toll_gate_3 = new TollGate(); vector<TollGate*> toll_gates = {toll_gate_1, toll_gate_2, toll_gate_3}; int choice; do { cout << "1. Charge" << endl; cout << "2. Print statistics" << endl; cout << "3. Add data" << endl; cout << "4. Modify data" << endl; cout << "5. Delete data" << endl; cout << "6. Save data" << endl; cout << "7. Load data" << endl; cout << "8. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: { int gate_choice; cout << "Enter gate number (1-3): "; cin >> gate_choice; if (gate_choice < 1 || gate_choice > 3) { cout << "Invalid gate number!" << endl; break; } int car_type_choice; cout << "Enter car type (1: small, 2: medium, 3: large): "; cin >> car_type_choice; if (car_type_choice < 1 || car_type_choice > 3) { cout << "Invalid car type!" << endl; break; } CarType car_type; switch (car_type_choice) { case 1: car_type = SMALL_CAR; break; case 2: car_type = MEDIUM_CAR; break; case 3: car_type = LARGE_CAR; break; } double distance; cout << "Enter distance: "; cin >> distance; toll_gates[gate_choice - 1]->charge(car_type, distance); break; } case 2: { int gate_choice; cout << "Enter gate number (1-3): "; cin >> gate_choice; if (gate_choice < 1 || gate_choice > 3) { cout << "Invalid gate number!" << endl; break; } toll_gates[gate_choice - 1]->printStatistics(); break; } case 3: { int gate_choice; cout << "Enter gate number (1-3): "; cin >> gate_choice; if (gate_choice < 1 || gate_choice > 3) { cout << "Invalid gate number!" << endl; break; } int car_type_choice; cout << "Enter car type (1: small, 2: medium, 3: large): "; cin >> car_type_choice; if (car_type_choice < 1 || car_type_choice > 3) { cout << "Invalid car type!" << endl; break; } CarType car_type; switch (car_type_choice) { case 1: car_type = SMALL_CAR; break; case 2: car_type = MEDIUM_CAR; break; case 3: car_type = LARGE_CAR; break; } double distance; cout << "Enter distance: "; cin >> distance; toll_gates[gate_choice - 1]->charge(car_type, distance); break; } case 4: { // TODO: Implement modify data function break; } case 5: { // TODO: Implement delete data function break; } case 6: { ofstream file; file.open("data.txt"); for (auto gate : toll_gates) { file << gate->charge(SMALL_CAR, 0.0) << endl; file << gate->charge(MEDIUM_CAR, 0.0) << endl; file << gate->charge(LARGE_CAR, 0.0) << endl; } file.close(); break; } case 7: { ifstream file; file.open("data.txt"); double fee; for (auto gate : toll_gates) { file >> fee; gate->charge(SMALL_CAR, fee / 0.3); file >> fee; gate->charge(MEDIUM_CAR, fee / 0.4); file >> fee; gate->charge(LARGE_CAR, fee / 0.5); } file.close(); break; } case 8: { cout << "Exiting..." << endl; break; } default: { cout << "Invalid choice!" << endl; break; } } } while (choice != 8); for (auto gate : toll_gates) { delete gate; } return 0; } ``` 这里,我们使用了一个 vector 来存储三个收费路口的指针,然后根据用户的选择来调用相应的函数。其中,文件存储功能使用了 C++ 的文件输入输出流来实现。

相关推荐

rar

最新推荐

recommend-type

使用Python实现租车计费系统的两种方法

本文通过两种方法给大家分享了使用Python实现租车计费系统,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

基于STC89C51单片机的出租车计费器设计

本文以单片机STC89C51 为核心设计一款出租车计费器, 使能够实现里程及对应价格的显示,由于采用芯片的自动定时计数,所以能够准确的计算出总的行车里程并能转换成对应的价格来,这样能够避免出租车司机作弊的可能性...
recommend-type

基于51单片机的出租车计费系统设计

基于出租车行业的需求,传统的出租车计费器发展使用了十几年,在稳定性、成本、以及使用习惯上都具有一些优势。
recommend-type

c语言课程设计报告 会员卡计费系统源代码.docx

c语言课程设计报告 会员卡计费系统源代码,c语言课程设计报告 会员卡计费系统源
recommend-type

基于FPGA的出租车计费器设计

这是一篇关于FPGA的出租车计费器设计的文章,这是我本科时候的毕业设计的论文,文档的内容表述的非常清晰,格式十分规范,对写毕业论文或者课程设计的同学会有很大的帮助!
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用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

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