当前位置: 首页 > 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…...

《从零掌握MIPI CSI-2: 协议精解与FPGA摄像头开发实战》-- CSI-2 协议详细解析 (一)

CSI-2 协议详细解析 (一&#xff09; 1. CSI-2层定义&#xff08;CSI-2 Layer Definitions&#xff09; 分层结构 &#xff1a;CSI-2协议分为6层&#xff1a; 物理层&#xff08;PHY Layer&#xff09; &#xff1a; 定义电气特性、时钟机制和传输介质&#xff08;导线&#…...

大模型多显卡多服务器并行计算方法与实践指南

一、分布式训练概述 大规模语言模型的训练通常需要分布式计算技术,以解决单机资源不足的问题。分布式训练主要分为两种模式: 数据并行:将数据分片到不同设备,每个设备拥有完整的模型副本 模型并行:将模型分割到不同设备,每个设备处理部分模型计算 现代大模型训练通常结合…...

#Uniapp篇:chrome调试unapp适配

chrome调试设备----使用Android模拟机开发调试移动端页面 Chrome://inspect/#devices MuMu模拟器Edge浏览器&#xff1a;Android原生APP嵌入的H5页面元素定位 chrome://inspect/#devices uniapp单位适配 根路径下 postcss.config.js 需要装这些插件 “postcss”: “^8.5.…...

Fabric V2.5 通用溯源系统——增加图片上传与下载功能

fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...

Mysql中select查询语句的执行过程

目录 1、介绍 1.1、组件介绍 1.2、Sql执行顺序 2、执行流程 2.1. 连接与认证 2.2. 查询缓存 2.3. 语法解析&#xff08;Parser&#xff09; 2.4、执行sql 1. 预处理&#xff08;Preprocessor&#xff09; 2. 查询优化器&#xff08;Optimizer&#xff09; 3. 执行器…...

虚拟电厂发展三大趋势:市场化、技术主导、车网互联

市场化&#xff1a;从政策驱动到多元盈利 政策全面赋能 2025年4月&#xff0c;国家发改委、能源局发布《关于加快推进虚拟电厂发展的指导意见》&#xff0c;首次明确虚拟电厂为“独立市场主体”&#xff0c;提出硬性目标&#xff1a;2027年全国调节能力≥2000万千瓦&#xff0…...

代码规范和架构【立芯理论一】(2025.06.08)

1、代码规范的目标 代码简洁精炼、美观&#xff0c;可持续性好高效率高复用&#xff0c;可移植性好高内聚&#xff0c;低耦合没有冗余规范性&#xff0c;代码有规可循&#xff0c;可以看出自己当时的思考过程特殊排版&#xff0c;特殊语法&#xff0c;特殊指令&#xff0c;必须…...

什么是VR全景技术

VR全景技术&#xff0c;全称为虚拟现实全景技术&#xff0c;是通过计算机图像模拟生成三维空间中的虚拟世界&#xff0c;使用户能够在该虚拟世界中进行全方位、无死角的观察和交互的技术。VR全景技术模拟人在真实空间中的视觉体验&#xff0c;结合图文、3D、音视频等多媒体元素…...

HTML前端开发:JavaScript 获取元素方法详解

作为前端开发者&#xff0c;高效获取 DOM 元素是必备技能。以下是 JS 中核心的获取元素方法&#xff0c;分为两大系列&#xff1a; 一、getElementBy... 系列 传统方法&#xff0c;直接通过 DOM 接口访问&#xff0c;返回动态集合&#xff08;元素变化会实时更新&#xff09;。…...

车载诊断架构 --- ZEVonUDS(J1979-3)简介第一篇

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 做到欲望极简,了解自己的真实欲望,不受外在潮流的影响,不盲从,不跟风。把自己的精力全部用在自己。一是去掉多余,凡事找规律,基础是诚信;二是…...