当前位置: 首页 > news >正文

​7.1 项目1 学生通讯录管理:文本文件增删改查(C++版本)(自顶向下设计+断点调试) (A)​

C++自学精简教程 目录(必读)

作业目标:

这个作业中,你需要综合运用之前文章中的知识,来解决一个相对完整的应用程序。

作业描述:

1 在这个作业中你需要在文本文件中存储学生通讯录的信息,并在程序启动的时候加载这些数据到内存中。

2 在程序运行过程中允许用户用键盘输入信息来完成对通讯录数的增删改查。

交互示例:

开始代码

开始代码不是完整的代码,需要你填写一部分代码,使之完整。

答案在本文最后

当你填写完整之后,运行程序和示例的交互输出一致,就算完成了这个作业

开始代码:

#include <iostream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
using namespace std;class Person
{
public:friend ostream& operator<<(ostream& os, const Person& _person);friend istream& operator>>(istream& is, Person& _person);public:string m_id;string m_name;string m_tel;
};ostream& operator<<(ostream& os, const Person& person)
{os << left << setw(5) << person.m_id << setw(15) << person.m_name << setw(20) << person.m_tel;return os;
}istream& operator>>(istream& is, Person& person)
{//(1) your code // 使用输入操作符重载,将流中的数据,提取赋值给person对象的成员变量中//see https://zhuanlan.zhihu.com/p/412724745return is;
}class PersonManager
{
public:void InputOnePerson(void);bool LoadAllPersonFromFile(const string& fileName);bool DeletePerson(void);bool QueryPersonByName() const;bool QueryPersonByTel() const;void ShowAllPerson(void) const;bool SaveAllPersonToFile(void) const;private:vector<Person> m_allPerson;
};bool PersonManager::DeletePerson(void)
{cout << "Please input person id for delete:";string id;cin >> id;for (auto itr = m_allPerson.begin(); itr != m_allPerson.cend(); ++itr){if (itr->m_id == id){//(2) your code// 容器的erase方法支持删除容器的元素时,传入指向元素的迭代器//see https://zhuanlan.zhihu.com/p/441293600}}return false;
}
bool PersonManager::QueryPersonByName() const
{//注意该函数需要返回bool值cout << "Please input name for query:";string name;cin >> name;for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr){if (itr->m_name == name){cout << "Find:" << endl;//(3) your code//see https://zhuanlan.zhihu.com/p/376440190//see https://zhuanlan.zhihu.com/p/376446724}}cout << "not found " << name << endl;return false;
}
bool PersonManager::QueryPersonByTel() const
{cout << "Please input tel for query:";string tel;cin >> tel;for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr){if (itr->m_tel == tel){cout << "Find:" << endl;//(4) your code//see https://zhuanlan.zhihu.com/p/376440190//see https://zhuanlan.zhihu.com/p/376446724}}cout << "not found " << tel << endl;return false;
}void PersonManager::ShowAllPerson(void) const
{cout << "All Person:" << endl;cout << left << setw(5) << "id" << setw(15) << "name" << setw(20) << "tel" << endl;for (auto& item : m_allPerson){cout << item << endl;}
}
bool PersonManager::SaveAllPersonToFile(void) const
{ofstream fout("data_saved.txt");//下面的常量迭代器 cbegin cend 中的 c 指的是 const的意思,表示不可以修改容器的元素for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr){//(5) your code //see https://zhuanlan.zhihu.com/p/262508774}return true;
}bool PersonManager::LoadAllPersonFromFile(const string& fileName)
{ifstream fin(fileName);if (!fin){cout << "load data failed . file " << fileName << " not exits." << endl;return false;}Person person;while (fin >> person){m_allPerson.push_back(person);}cout << "load data from file success." << endl;return true;
}void PersonManager::InputOnePerson(void)
{cout << "Please input one person:" << endl;cout << "Please input id:";string id;cin >> id;Person person;person.m_id = id;for (auto itr = m_allPerson.cbegin(); itr != m_allPerson.cend(); ++itr){if (itr->m_id == id){cout << id << " already existed! Save failed." << endl;return;}}cout << "Please input name:";string name;cin >> name;person.m_name = name;cout << "Please input tel:";string tel;cin >> tel;person.m_tel = tel;cout << "Input finished, save successed." << endl;m_allPerson.push_back(person);
}int main(int argv, char* argc[])
{PersonManager personMgr;personMgr.LoadAllPersonFromFile("input_data.txt");personMgr.ShowAllPerson();while(true){cout<<"input a commond : "<<endl;cout<<"1 [AddPerson]"<<endl;cout<<"2 [ShowAllPerson]"<<endl;cout<<"3 [QueryPerson by name]"<<endl;cout<<"4 [QueryPerson by tel]"<<endl;cout<<"5 [SaveAllPersonToFile]"<<endl;cout<<"6 [DeletePerson]"<<endl;cout<<"0 [ExitAndSaveChange]"<<endl;int commond;cin>>commond;switch(commond){case 1: { personMgr.InputOnePerson(); break;}case 2: { personMgr.ShowAllPerson(); break;}case 3: { personMgr.QueryPersonByName(); break;}case 4: { personMgr.QueryPersonByTel(); break;}case 5: { personMgr.SaveAllPersonToFile(); break;}case 6: { personMgr.DeletePerson(); break;}case 0: { personMgr.SaveAllPersonToFile(); return 0;}default:{ cout<<"System Exit."<<endl; return 0;}}}return 0;
}

输入文件

input_data.txt

文件内容:

2    zhangsan2      13788889992         
3    zhangsan3      13788889993         
4    zhangsan4      13788889994         
5    wanger         13333333333      

运行与输出

load data from file success.
All Person:
id   name           tel
2    zhangsan2      13788889992
3    zhangsan3      13788889993
4    zhangsan4      13788889994
5    wanger         13333333333
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
2
All Person:
id   name           tel
2    zhangsan2      13788889992
3    zhangsan3      13788889993
4    zhangsan4      13788889994
5    wanger         13333333333
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
1
Please input one person:
Please input id:1
Please input name:zhangsan
Please input tel:13344445555
Input finished, save successed.
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
2
All Person:
id   name           tel
2    zhangsan2      13788889992
3    zhangsan3      13788889993
4    zhangsan4      13788889994
5    wanger         13333333333
1    zhangsan       13344445555
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
3
Please input name for query:zhangsan
Find:
1    zhangsan       13344445555
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
3
Please input name for query:zhang
not found zhang
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
4
Please input tel for query:13344445555
Find:
1    zhangsan       13344445555
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
4
Please input tel for query:1334444
not found 1334444
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
6
Please input person id for delete:4
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
2
All Person:
id   name           tel
2    zhangsan2      13788889992
3    zhangsan3      13788889993
5    wanger         13333333333
1    zhangsan       13344445555
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
5
input a commond :
1 [AddPerson]
2 [ShowAllPerson]
3 [QueryPerson by name]
4 [QueryPerson by tel]
5 [SaveAllPersonToFile]
6 [DeletePerson]
0 [ExitAndSaveChange]
0

最终保存数据到文件 data_saved.txt

文件 data_saved.txt 的内容为:

2    zhangsan2      13788889992         
3    zhangsan3      13788889993         
5    wanger         13333333333         
1    zhangsan       13344445555       

你的结果也是这样吗?

答案在此

C++自学精简教程 全部答案

学生完成该作业展示

另一个学生实现的效果

相关文章:

​7.1 项目1 学生通讯录管理:文本文件增删改查(C++版本)(自顶向下设计+断点调试) (A)​

C自学精简教程 目录(必读) 作业目标&#xff1a; 这个作业中&#xff0c;你需要综合运用之前文章中的知识&#xff0c;来解决一个相对完整的应用程序。 作业描述&#xff1a; 1 在这个作业中你需要在文本文件中存储学生通讯录的信息&#xff0c;并在程序启动的时候加载这些…...

学习使用php判断阿里云oss图片单图或批量上传、查询图片文件是否存在

学习使用php判断阿里云oss图片单图或批量上传、查询图片文件是否存在 doesObjectExist doesObjectExist 主要函数doesObjectExist /*** Base64上传文件* param string|array $images* param string $model_path* param string $model_type* param string $upload_path* param…...

重磅| Falcon 180B 正式在 Hugging Face Hub 上发布!

引言 我们很高兴地宣布由 Technology Innovation Institute (TII) 训练的开源大模型 Falcon 180B 登陆 Hugging Face&#xff01; Falcon 180B 为开源大模型树立了全新的标杆。作为当前最大的开源大模型&#xff0c;有180B 参数并且是在在 3.5 万亿 token 的 TII RefinedWeb 数…...

Linux命令行

目录 CLI GUI 命令行界面 图形界面 命令行提示符 # $ ​编辑 命令一般由三个部分组成 历史命令&#xff0c;使用上下键&#xff0c;或者使用history&#xff0c;ctrlr搜索历史命令 通配符 *,? 切换用户 su 作业管理 &&#xff0c;jobs,bg,fg CLI GUI 命令行界面 …...

[持续更新]计算机经典面试题基础篇Day1

[通用]计算机经典面试题基础篇Day1 1、jvm的组成 类加载器&#xff08;Class Loader&#xff09;&#xff1a;负责将编译后的Java类加载到JVM中&#xff0c;并在运行时动态加载所需的类。运行时数据区&#xff08;Runtime Data Area&#xff09;&#xff1a;是JVM的内存管理区…...

ProcessWindowFunction 结合自定义触发器的陷阱

背景&#xff1a; flink中常见的需求如下&#xff1a;统计某个页面一天内的点击率,每10秒输出一次&#xff0c;我们如果采用ProcessWindowFunction 结合自定义触发器如何实现呢&#xff1f;如果这样实现问题是什么呢&#xff1f; ProcessWindowFunction 结合自定义触发器实现…...

什么是jvm

一、初识JVM&#xff08;虚拟机&#xff09; JVM是Java Virtual Machine&#xff08;Java虚拟机&#xff09;的缩写&#xff0c;JVM是一种用于计算设备的规范&#xff0c;它是一个虚构出来的计算机&#xff0c;是通过在实际的计算机上仿真模拟各种计算机功能来实现的。 引入Jav…...

kettle通过java步骤获取汉字首拼

kettle通过java步骤获取汉字首拼 用途描述 一组数据&#xff0c;需要获取汉字首拼后&#xff0c;输出&#xff1b; 实现效果 添加jar包 pinyin4j-2.5.0.jar 自定义常量数据 Java代码 完整代码&#xff1a; import net.sourceforge.pinyin4j.PinyinHelper; import net.sou…...

Conformer: Local Features Coupling Global Representationsfor Visual Recognition

论文链接&#xff1a;https://arxiv.org/abs/2105.03889 代码链接&#xff1a;https://github.com/pengzhiliang/Conformer 参考博文&#xff1a;Conformer论文以及代码解析&#xff08;上&#xff09;_conformer代码_从现在开始壹并超的博客-CSDN博客 摘要 在卷积神经网络…...

java8-Stream流常用API

什么是 Stream Stream&#xff08;流&#xff09;是 Java 8 引入的一个新的抽象概念&#xff0c;它代表着一种处理数据的序列。简单来说&#xff0c;Stream 是一系列元素的集合&#xff0c;这些元素可以是集合、数组、I/O 资源或者其他数据源。 Stream API 提供了丰富的操作方…...

React 任务调度

React 任务池 不同的fiber任务有不同的优先级&#xff0c;为了用户体验&#xff0c;React需要先处理优先级高的任务。 为了存储这些任务&#xff0c;React中有两个任务池&#xff1a; // Tasks are stored on a min heap var taskQueue []; // 存储立即要执行的任务 var tim…...

小白开始学习C++

​​​​第一节&#xff1a;控制台输出hello word&#xff01; #include<iostream> //引入库文件 int main() { //控制台输出 hello word! 之后回车 std::cout << "hello word!\n"; #include<iostream> //引入库文件int main() {//控制…...

SpringMVC入门的注解、参数传递、返回值和页面跳转---超详细教学

前言&#xff1a; 欢迎阅读Spring MVC入门必读&#xff01;在这篇文章中&#xff0c;我们将探索这个令人兴奋的框架&#xff0c;它为您提供了一种高效、灵活且易于维护的方式来构建Web应用程序。通过使用Spring MVC&#xff0c;您将享受到以下好处&#xff1a;简洁的代码、强大…...

【复习socket】每天40min,我们一起用70天稳扎稳打学完《JavaEE初阶》——28/70 第二十八天

专注 效率 记忆 预习 笔记 复习 做题 欢迎观看我的博客,如有问题交流,欢迎评论区留言,一定尽快回复!(大家可以去看我的专栏,是所有文章的目录)   文章字体风格: 红色文字表示:重难点★✔ 蓝色文字表示:思路以及想法★✔   如果大家觉得有帮助的话,感谢大家帮忙 点…...

vue2踩坑之项目:生成二维码使用vue-print-nb打印二维码

1. vue2安装 npm install vue-print-nb --save vue3安装 npm install vue3-print-nb --save 2. //vue2 引入方式 全局 main.js import Print from vue-print-nb Vue.use(Print) ------------------------------------------------------------------------------------ //vue2 …...

【iVX】十五分钟制作一款小游戏,iVX真有怎么神?

个人主页&#xff1a;【&#x1f60a;个人主页】 新人博主&#xff0c;喜欢就关注一下呗~ 文章目录 前言iVX介绍初上手布置背景制作可移动物体总结&#xff08;完善步骤&#xff09; 前言 在上篇文章中&#xff0c;我向大家介绍了一种打破常规的编程方式——iVX&#xff0c;可…...

SpringMVC常用注解、参数传递、返回值

目录 前言 一、常用注解 二、参数传递 ​编辑 1. 基础类型String类型 2. 复杂类型 3. RequestParam 4. PathVariable 5.RequestBody 6. RequestHeader 三、方法返回值 一&#xff1a;void 二&#xff1a;String 三&#xff1a;Stringmodel 四&#xff1a;ModelAndVi…...

新公司第一次上架新APP需要提前准备哪些材料?

目录 前言一、需要上架的应用市场二、需要准备的资料总结 前言 前不久&#xff0c;使用一家新公司刚刚上架了一款新的APP项目。特此记录一下&#xff0c;现在第一次上架一款APP需要提前准备的各项材料。 一、需要上架的应用市场 现在&#xff0c;上架一款新的APP主流的应用市…...

『C语言进阶』指针进阶(一)

&#x1f525;博客主页&#xff1a; 小羊失眠啦 &#x1f516;系列专栏&#xff1a; C语言 &#x1f325;️每日语录&#xff1a;无论你怎么选&#xff0c;都难免会有遗憾。 ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 前言 在C语言初阶中&#xff0c;我们对指针有了一定的…...

2605. 从两个数字数组里生成最小数字(Java)

给你两个只包含 1 到 9 之间数字的数组 nums1 和 nums2 &#xff0c;每个数组中的元素 互不相同 &#xff0c;请你返回 最小 的数字&#xff0c;两个数组都 至少 包含这个数字的某个数位。 示例 1&#xff1a; 输入&#xff1a;nums1 [4,1,3], nums2 [5,7] 输出&#xff1a;1…...

51c自动驾驶~合集58

我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留&#xff0c;CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制&#xff08;CCA-Attention&#xff09;&#xff0c;…...

Debian系统简介

目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版&#xff…...

python如何将word的doc另存为docx

将 DOCX 文件另存为 DOCX 格式&#xff08;Python 实现&#xff09; 在 Python 中&#xff0c;你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是&#xff0c;.doc 是旧的 Word 格式&#xff0c;而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

C++ 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

SiFli 52把Imagie图片,Font字体资源放在指定位置,编译成指定img.bin和font.bin的问题

分区配置 (ptab.json) img 属性介绍&#xff1a; img 属性指定分区存放的 image 名称&#xff0c;指定的 image 名称必须是当前工程生成的 binary 。 如果 binary 有多个文件&#xff0c;则以 proj_name:binary_name 格式指定文件名&#xff0c; proj_name 为工程 名&…...

基于TurtleBot3在Gazebo地图实现机器人远程控制

1. TurtleBot3环境配置 # 下载TurtleBot3核心包 mkdir -p ~/catkin_ws/src cd ~/catkin_ws/src git clone -b noetic-devel https://github.com/ROBOTIS-GIT/turtlebot3.git git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_msgs.git git clone -b noetic-dev…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...

【深度学习新浪潮】什么是credit assignment problem?

Credit Assignment Problem(信用分配问题) 是机器学习,尤其是强化学习(RL)中的核心挑战之一,指的是如何将最终的奖励或惩罚准确地分配给导致该结果的各个中间动作或决策。在序列决策任务中,智能体执行一系列动作后获得一个最终奖励,但每个动作对最终结果的贡献程度往往…...

C# WPF 左右布局实现学习笔记(1)

开发流程视频&#xff1a; https://www.youtube.com/watch?vCkHyDYeImjY&ab_channelC%23DesignPro Git源码&#xff1a; GitHub - CSharpDesignPro/Page-Navigation-using-MVVM: WPF - Page Navigation using MVVM 1. 新建工程 新建WPF应用&#xff08;.NET Framework) 2.…...

Ray框架:分布式AI训练与调参实践

Ray框架&#xff1a;分布式AI训练与调参实践 系统化学习人工智能网站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目录 Ray框架&#xff1a;分布式AI训练与调参实践摘要引言框架架构解析1. 核心组件设计2. 关键技术实现2.1 动态资源调度2.2 …...