2021-10-19 01:05:11 +00:00
|
|
|
|
# 基于C++的通讯录系统
|
2021-10-18 22:47:03 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
# 一、实验的内容
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
通讯录系统可帮助使用者管理归纳通讯录名单,达到添加,删除,修改,保存等需求。要求使用学习过的C/C++程序设计的知识完成通讯录系统的设计与实现。
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
# 二、实验课题分析
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
## 2.1 通讯录系统的主要功能
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
通讯录主要功能为:添加通讯录成员,修改成员,删除成员,按需求搜索查看成员,保存为文档。
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/873a50e1f3ff8e550170e434abd6d65d.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
系统各模块的功能具体描述为:
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**添加成员模块**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
提供界面让使用者输入希望加入的通讯录成员的各类信息(姓名,电话,住址,QQ,邮箱等),并检查格式是否有误。若格式无误,则将该通讯录信息通过二进制文件方式储存在./contact文件目录下。
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**修改成员模块**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
使用者可以重写已有的通讯录成员,增加或删除除姓名以外的各个信息。一条通讯录成员可以拥有多个电话号码或QQ。
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**删除成员模块**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
使用者可以选择某个不希望继续使用的通讯录成员并删除他们。
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**搜索查看成员模块**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
使用者通过各种方式查询已添加的通讯录成员,并决定是否修改或删除它们。提供的方法有:精准查询,模糊查询,按分类查询等。
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
## 2.2 系统分析及设计
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
系统开发使用Template Method设计模式和Strategy Patten 两种设计模式,较好的封装所需函数,使得主程序入口开发环节只需关注Contact.h头文件即可实现。具体类之间的耦合关系见下图:
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/ee90b3d45e439cf0e2a07be7ba0666a6.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
## 2.3 系统的实现
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
### 2.3.1 类的编写
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
系统工程名为:contacts。包含类:Person, Contact, ContactInterface, ContactInit等
|
|
|
|
|
具体类结构声明如下:
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**Person类**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
```C
|
|
|
|
|
class Person
|
2021-10-18 22:45:58 +00:00
|
|
|
|
{
|
|
|
|
|
Public:
|
|
|
|
|
char name[MAXNAME];
|
|
|
|
|
char sex;
|
|
|
|
|
char tel[MAXTEL];
|
|
|
|
|
char addr[MAXADDR];
|
2021-10-19 01:05:11 +00:00
|
|
|
|
char zip[MAXZIP];
|
2021-10-18 22:45:58 +00:00
|
|
|
|
char mail[MAXMAIL];
|
|
|
|
|
char qq[MAXQQ];
|
|
|
|
|
char category[MAXCTGY];
|
|
|
|
|
|
|
|
|
|
Person();
|
|
|
|
|
~Person();
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**ContactInterface类**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
```C
|
|
|
|
|
class CheckInterface
|
2021-10-18 22:45:58 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
bool check(Person&, const bool _check_repe) const;
|
2021-10-19 01:05:11 +00:00
|
|
|
|
bool check_exact(const Person&, const string) const;
|
2021-10-18 22:45:58 +00:00
|
|
|
|
virtual ~CheckInterface(){};
|
|
|
|
|
private:
|
|
|
|
|
vector<string> part_tq(const Person&, const char* const) const;
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**ContactInit类**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
```C
|
|
|
|
|
class ContactInit
|
2021-10-18 22:45:58 +00:00
|
|
|
|
{
|
|
|
|
|
public:
|
2021-10-19 01:05:11 +00:00
|
|
|
|
virtual int refresh() const = 0;
|
2021-10-18 22:45:58 +00:00
|
|
|
|
virtual void welcome() const= 0;
|
|
|
|
|
virtual ~ContactInit(){};
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**Contact类**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
```C
|
2021-10-18 22:45:58 +00:00
|
|
|
|
class Contact : public ContactInterface, public ContactInit
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
MainStrategy* setMainStrategy(int);
|
|
|
|
|
public:
|
|
|
|
|
Contact();
|
|
|
|
|
~Contact();
|
2021-10-19 01:05:11 +00:00
|
|
|
|
int refresh() const;
|
2021-10-18 22:45:58 +00:00
|
|
|
|
void welcome() const;
|
2021-10-19 01:05:11 +00:00
|
|
|
|
};
|
2021-10-18 22:45:58 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**MainStrategy类**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
```C
|
2021-10-18 22:45:58 +00:00
|
|
|
|
class MainStrategy : public CheckInterface
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
MainStrategy();
|
|
|
|
|
virtual ~MainStrategy();
|
|
|
|
|
virtual int doMainStrategy() = 0;
|
|
|
|
|
protected:
|
2021-10-19 01:05:11 +00:00
|
|
|
|
void printAll() const;
|
2021-10-18 22:45:58 +00:00
|
|
|
|
void print_prsn(const Person&, const string, bool) const;
|
|
|
|
|
|
|
|
|
|
bool delete_prsn(Person&) const;
|
|
|
|
|
int modify_prsn(Person&) const; //Way to modify a spefic Person member, with 0->success, -1->fail
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**MainViewMenuInterface类**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
```C
|
2021-10-18 22:45:58 +00:00
|
|
|
|
class MainVewMenuInterface : public MainStrategy
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
private:
|
|
|
|
|
ViewStrategy* viewStrategy;
|
|
|
|
|
virtual ViewStrategy* setViewStrategy(int) = 0;
|
|
|
|
|
virtual int view(Person* v_Person) const;
|
|
|
|
|
public:
|
|
|
|
|
MainVewMenuInterface();
|
|
|
|
|
virtual ~MainVewMenuInterface();
|
|
|
|
|
virtual int doMainStrategy();
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**类的关系设计**
|
|
|
|
|
|
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/61b902f86f8018f6d63ce4f23de85d51.writebug)
|
|
|
|
|
|
|
|
|
|
各类的具体功能和说明如下:
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
>class Person; 提供基本的数据存储结构。
|
|
|
|
|
>class ContactInterface; 提供主函数菜单,策略选择方法。是Contact类的一个接口,MainStrategy的调用者。
|
|
|
|
|
>class ContactInit; 提供初始化程序所需函数。同样是Contact类的一个接口。
|
|
|
|
|
>class Contact; 具体实现了两个接口的方法。MainStrategy的决策者。同时面向调用者(main.cpp)。但注意Contact不提供任何public方法。需要通过两个接口调用。
|
|
|
|
|
>class CheckInterface; 提供检查函数。
|
|
|
|
|
>class MainStrategy; Strategy Patten设计模式。同时包含子类公用的方法。
|
|
|
|
|
>class MainNewMenu; class MainDelMenu; class MainMdfMenu; 分别override doMainStrategy()函数,实现新建,删除,修改功能。
|
|
|
|
|
>class MainVewMenuInterface; override doMainStrategy()函数,ViewStrategy的调用者。
|
|
|
|
|
>class MainVewMenu; ViewStrategy的决策者。
|
|
|
|
|
>class ViewStrategy; Strategy Patten。
|
|
|
|
|
>class ViewAllMenu; class ViewExactMenu;
|
|
|
|
|
>class ViewFuzzyMenu; class ViewCategoryMenu; 分别override doViewStrategy()函数,实现所有查找,精确查找,模糊查找,按类查找功能。
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
**通讯录实现流程图**
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/54d05a5c2d598c0d46c71672bebeac9c.writebug)
|
|
|
|
|
|
|
|
|
|
### 2.3.2 交互界面以及登录菜单的实现
|
|
|
|
|
|
|
|
|
|
**系统运行开始的界面如图所示**
|
|
|
|
|
|
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/9f96d4bea111dda62495eaa8decb32e5.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
主要通过选择结构和循环结构实现界面的前进和后退。例如,第一个登录界面出现5个选择:1.新建,2.删除,3.修改,4.浏览,5.退出
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
# 三、实验调试、测试、运行记录及分析
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
系统在调试测试过程中遇到若干问题,不过经过仔细反复的检查已经消除各种漏洞。
|
|
|
|
|
|
|
|
|
|
主要的测试经过如下:
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/6f53cb0dcbdf55fd703f64b50ae0d4ad.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
在开始界面输入“1”即添加新的成员:
|
|
|
|
|
|
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/e4f50dc22f11d26e457578f085303a97.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
若显示 Information Entry Success! 则录入数据成功。若显示Information Error! 则录入数据失败。如图则因为在电话(TEL)中出现了中文字符。随后将返回主界面。
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/1b0716f1e6442130639bcf7a30b3aa7b.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
在主界面输入2可删除成员:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/3f1abeb90240457332dbf08175bcc6f1.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
如我们希望删除(2)数据,则键入2:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/7a60eec0d254cd7973199011b7910bf9.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
就可以得到(2)的详细数据。输入y/n即可选择是否删除该成员。随后程序将返回主界面。
|
|
|
|
|
|
|
|
|
|
在主界面下输入3可以修改已有的成员:我们希望修改刚刚加入的成员(1)的电话号码,同时加入他所有常用的QQ号码:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/bf0e411a2580cbe6aa625dcde26bcbe8.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
键入1, 并按照需求修改信息:
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/00e606fa87b5e462913c538b9ce023f9.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
确认无误后即可修改信息。随后返回主界面
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
输入4即可按需求分类查看搜索成员:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/56e53730ac01df68a0ec48c85e1fa4b1.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
键入1进行精准匹配,该模式下只匹配名字:如输入“严恩伟”后匹配信息如图:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/38cfbb157c7990d9b1acbafeb9219a89.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
随后可以根据自身需求选择1-3选项。此处不再演示。
|
|
|
|
|
|
|
|
|
|
在view模式下键入2进行模糊匹配,该模式匹配名字,电话,地址。只要出现匹配字符即给与显示。如输入“181”后显示:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/d9d9afac07e79866fdd0702b3c333394.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
选择1-3即可进入其详细页面。此处不再演示。
|
|
|
|
|
|
|
|
|
|
在view模式下键入3进行分类(category)匹配。该模式会列出所有的分类,并可根据用户选择的分类进行罗列(其中未设置的分类被标记为Unset):
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/fdd143f5d10c987122a2a8c5b4d0f91c.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
根据提示选择1-2即可进入相应页面。选择0即可退出。
|
|
|
|
|
|
|
|
|
|
在view模式下键入4进行全局匹配。该模式会列出所有的成员:
|
|
|
|
|
|
2021-10-19 01:05:11 +00:00
|
|
|
|
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/b6faf525d0fc0376c25d87bae4c494e4.writebug)
|
2021-10-18 22:45:58 +00:00
|
|
|
|
|
|
|
|
|
在view模式下键入5退出view模式。
|
|
|
|
|
|
|
|
|
|
在主菜单中键入5退出程序。
|