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

news/2024/7/2 1:26:34

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/412724745

    return 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++自学精简教程 全部答案

学生完成该作业展示

另一个学生实现的效果


http://lihuaxi.xjx100.cn/news/1510035.html

相关文章

QT中对话框界面的实现以及事件处理机制(核心机制)

对话框 消息对话框、字体对话框、颜色对话框、文件对话框 消息对话框 消息对话框提供了一个模态的对话框&#xff0c;用来提示用户信息&#xff0c;或者询问用户问题并得到回答 基于属性版本的API 使用该类调用构造函数&#xff0c;构造一个类对象调用成员函数exec进入执行…

Unity中Shader的渲染排序Tags{“Queue“ = “Transparent“}

文章目录 前言一、在Unity中渲染排序一般是固定的几个层级&#xff0c;透明 和 半透明是以 2500 为 分界点&#xff0c;渲染层级 从 低 到 高二、渲染队列 可以 在 SubShader 或 Pass 中写 前言 Unity中Shader的渲染排序 一、在Unity中渲染排序一般是固定的几个层级&#xff0…

解决github图片及网页加载不出来

github时不时的抽风&#xff0c;即使用了代理也访问不了&#xff0c;加载不出页面上的图片和静态资源&#xff0c;下面介绍下几个常用的解决方法 kgithub kgithub 是一个公益加速项目&#xff0c;仅需在 github.com 前加上 k 即可&#xff0c;若提示访问限制请刷新任意 github…

stable diffusion实践操作-webUI教程

系列文章目录 stable diffusion实践操作 提示&#xff1a;写完文章后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 系列文章目录前言一、SD webUI是什么&#xff1f;二、详细教程1. 插件安装1.1. 提示词插件安装和使用 2. xyz 图表使用2.1…

一个 MySQL 数据库死锁的案例和解决方案

本文介绍了一个 MySQL 数据库死锁的案例和解决方案。 场景 生产环境出了一个偶现的数据库死锁问题&#xff0c;导致少部分业务处理失败。 分析特征之后&#xff0c;发现是多个线程并发执行同一个方法&#xff0c;更新关联的数据时可能会出现&#xff0c;把场景简化概括一下&…

[国产MCU]-W801开发实例-TCP客户端

TCP客户端 文章目录 TCP客户端1、TCP协议简单介绍2、W801创建TCP客户流程本文将详细介绍如何在W801中使用TCP客户端。 1、TCP协议简单介绍 传输控制协议 (TCP) 是一种标准,它定义了如何建立和维护应用程序可以用来交换数据的网络对话。 TCP 与 Internet 协议 (IP) 一起工作,…

在CentOS7上使用Docker安装和部署RabbitMQ

&#x1f680; 1 拉取RabbitMQ Docker镜像 首先&#xff0c;使用Docker命令从Docker Hub拉取RabbitMQ官方镜像。打开终端并运行以下命令&#xff1a; docker pull rabbitmq&#x1f680; 2 创建RabbitMQ容器 一旦镜像下载完成&#xff0c;使用以下命令创建RabbitMQ容器&…

ESP32在线仿真器

1. Wokwi是一个电子在线仿真平台&#xff0c;支持的芯片有ESP32,STM32,树莓派&#xff0c;Arduino 网址&#xff1a; https://wokwi.com ; 而且支持在vscode内置插件仿真 2. ESP32可以使用microPython开发&#xff0c;相关sdk说明MicroPython&#xff08;ESP32&#xff09;快…