可以用c++类和文件写个车辆停车计费系统吗

时间: 2023-11-22 19:54:36 浏览: 32
当然可以。以下是一个简单的车辆停车计费系统的C++类和文件实现: ```cpp #include <iostream> #include <fstream> #include <string> #include <vector> #include <ctime> using namespace std; class ParkingRecord { public: ParkingRecord(int id, time_t start_time) : id_(id), start_time_(start_time) {} int get_id() const { return id_; } time_t get_start_time() const { return start_time_; } double get_duration() const { time_t now = time(NULL); return difftime(now, start_time_) / 3600.0; } void print() const { cout << "车辆编号:" << id_ << endl; cout << "停车开始时间:" << ctime(&start_time_); cout << "停车时长:" << get_duration() << "小时" << endl; cout << "停车费用:" << get_fee() << "元" << endl; } double get_fee() const { double duration = get_duration(); if (duration <= 1.0) { return 5.0; } else { return 5.0 + (duration - 1.0) * 3.0; } } private: int id_; time_t start_time_; }; class ParkingLot { public: ParkingLot(const string& file_name) : file_name_(file_name) { load_records(); } ~ParkingLot() { save_records(); } void park(int id) { time_t now = time(NULL); records_.push_back(ParkingRecord(id, now)); cout << "车辆编号为 " << id << " 的车辆停车成功" << endl; } void leave(int id) { bool found = false; for (auto iter = records_.begin(); iter != records_.end(); ++iter) { if (iter->get_id() == id) { iter->print(); records_.erase(iter); found = true; break; } } if (!found) { cout << "没有找到车辆编号为 " << id << " 的车辆" << endl; } } private: void load_records() { ifstream fin(file_name_); if (fin.is_open()) { while (!fin.eof()) { int id; time_t start_time; fin >> id >> start_time; if (id > 0 && start_time > 0) { records_.push_back(ParkingRecord(id, start_time)); } } fin.close(); } } void save_records() { ofstream fout(file_name_); if (fout.is_open()) { for (auto record : records_) { fout << record.get_id() << " " << record.get_start_time() << endl; } fout.close(); } } string file_name_; vector<ParkingRecord> records_; }; int main() { ParkingLot lot("records.txt"); while (true) { cout << "请选择操作:" << endl; cout << "1. 停车" << endl; cout << "2. 离开" << endl; cout << "3. 退出" << endl; int choice; cin >> choice; if (choice == 1) { cout << "请输入车辆编号:"; int id; cin >> id; lot.park(id); } else if (choice == 2) { cout << "请输入车辆编号:"; int id; cin >> id; lot.leave(id); } else if (choice == 3) { break; } else { cout << "无效选择,请重新输入" << endl; } } return 0; } ``` 在该实现中,我们使用了两个类:`ParkingRecord` 和 `ParkingLot`。前者表示停车记录,后者表示停车场。`ParkingRecord` 类中包含了车辆的编号、停车开始时间等信息,以及计算停车费用的方法。`ParkingLot` 类中包含了所有的停车记录,并提供了停车和离开的方法。在程序运行时,用户可以选择停车或离开车场,程序会根据用户的选择调用相应的方法。 停车记录保存在一个文本文件中,程序启动时会从该文件中加载之前的停车记录,程序结束时会将新的停车记录保存到该文件中。这样即使程序关闭,之前的停车记录也不会丢失。 以上就是一个简单的车辆停车计费系统的C++类和文件实现。

相关推荐

最新推荐

recommend-type

linux系统中c++写日志文件功能分享

主要介绍了linux系统中c++写日志文件功能,简化了glog,只保留了写日志文件的功能,只是改写了linux版本,需要的朋友可以参考下
recommend-type

C++ 学生成绩管理系统设计报告书

内含4个类,各自具有其属性和行为。 基本实现了: (1)学生基本信息、考试科目及成绩的信息录入。 (2)已有学生信息的显示。 (3)学生基本信息的读取和保存输入数据等功能 (4)学生基本信息的查询与修改。可以对已有的...
recommend-type

C++中进行txt文件读入和写入的方法示例

主要给大家介绍了C++中进行txt文件读入和写入的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

C++将CBitmap类中的图像保存到文件的方法

主要介绍了C++将CBitmap类中的图像保存到文件的方法,涉及C++导出资源文件的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
recommend-type

c++读取和写入TXT文件的整理方法

今天小编就为大家分享一篇c++读取和写入TXT文件的整理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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