265 lines
8.2 KiB
Markdown
265 lines
8.2 KiB
Markdown
# 基于C++的通讯录系统的设计与实现
|
||
|
||
# 一 需求分析
|
||
|
||
通讯录系统可帮助使用者管理归纳通讯录名单,达到添加,删除,修改,保存等需求。
|
||
|
||
# 二 系统设计
|
||
|
||
## 2.1 **功能模块设计**
|
||
|
||
**通讯录主要功能为:**添加通讯录成员,修改成员,删除成员,按需求搜索查看成员,保存为文档。
|
||
|
||
如下图所示:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/bc5ec8765192d2e34a411ca325319160.writebug)
|
||
|
||
**系统各模块的功能具体描述为:**
|
||
|
||
**1**、添加成员模块
|
||
|
||
提供界面让使用者输入希望加入的通讯录成员的各类信息(姓名,电话,住址, QQ,邮箱等),并检查格式是否有误。若格式无误,则将该通讯录信息通过二进制文件方式储存在./contact文件目录下。
|
||
|
||
**2**、修改成员模块
|
||
|
||
使用者可以重写已有的通讯录成员,增加或删除除姓名以外的各个信息。一条通 讯录成员可以拥有多个电话号码或QQ。
|
||
|
||
**3**、删除成员模块
|
||
|
||
使用者可以选择某个不希望继续使用的通讯录成员并删除他们。
|
||
|
||
**4**、搜索查看成员模块
|
||
|
||
使用者通过各种方式查询已添加的通讯录成员,并决定是否修改或删除它们。提供的方法有:精准查询,模糊查询,按分类查询等。
|
||
|
||
## 2.2 系统架构设计
|
||
|
||
系统开发使用Template Method设计模式和Strategy Patten 两种设计模式,较好的封装所需函数,使得主程序入口开发环节只需关注Contact.h头文件即可实现。
|
||
|
||
具体类之间的耦合关系见下图:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/a203ff3046cc160d1b971568683ffd63.writebug)
|
||
|
||
|
||
|
||
**类的关系设计如下图所示:**
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/db7ed97700697f6e106a4f72e6346ffc.writebug)
|
||
|
||
**各类的具体功能和说明如下:**
|
||
|
||
- class Person; 提供基本的数据存储结构
|
||
|
||
- classContactInterface; 提供主函数菜单,策略选择方法。是Contact类的一个接口,MainStrategy的调用者
|
||
|
||
- classContactInit; 提供初始化程序所需函数。同样是Contact类的一个接口
|
||
|
||
- classContact; 具体实现了两个接口的方法。MainStrategy的决策者。同时面向调用者(main.cpp)。但注意Contact不提供任何public方法。需要通过两个接口调用
|
||
|
||
- classCheckInterface; 提供检查函数
|
||
|
||
- classMainStrategy; Strategy Patten设计模式。同时包含子类公用的方法
|
||
|
||
- classMainNewMenu; class MainDelMenu; class MainMdfMenu; **分别override **doMainStrategy()函数,实现新建,删除,修改功能
|
||
|
||
- classMainVewMenuInterface; **override **doMainStrategy()函数,ViewStrategy的调用者
|
||
|
||
- classMainVewMenu; ViewStrategy的决策者
|
||
|
||
- class ViewStrategy; StrategyPatten
|
||
|
||
- classViewAllMenu; class ViewExactMenu;
|
||
|
||
**class ViewFuzzyMenu; class ViewCategoryMenu; **分别override **doViewStrategy()**函数,实现所有查找,精确查找,模糊查找,按类查找功能。
|
||
|
||
# 三 系统实现
|
||
|
||
## 3.1 系统实现流程
|
||
|
||
通讯录系统实现流程如下图所示:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/5bdb36bb9f84f408f3aff113738b297a.writebug)
|
||
|
||
## 3.2 类的实现
|
||
|
||
系统包含Person,Contact, ContactInterface, ContactInit等类,具体类结构声明如下:
|
||
|
||
**Person**类:
|
||
|
||
```c++
|
||
class Person
|
||
{
|
||
Public:
|
||
char name[MAXNAME];
|
||
char sex;
|
||
char tel[MAXTEL];
|
||
char addr[MAXADDR];
|
||
char zip[MAXZIP];
|
||
char mail[MAXMAIL];
|
||
char qq[MAXQQ];
|
||
char category[MAXCTGY];
|
||
|
||
Person();
|
||
~Person();
|
||
};
|
||
|
||
```
|
||
|
||
**ContactInterface**类:
|
||
|
||
```c++
|
||
class CheckInterface
|
||
{
|
||
public:
|
||
bool check(Person&, const bool _check_repe) const;
|
||
bool check_exact(const Person&, const string) const;
|
||
virtual ~CheckInterface(){};
|
||
private:
|
||
vector<string> part_tq(const Person&, const char* const) const;
|
||
};
|
||
|
||
```
|
||
|
||
**ContactInit**类:
|
||
|
||
```c++
|
||
class ContactInit
|
||
{
|
||
public:
|
||
virtual int refresh() const = 0;
|
||
virtual void welcome() const= 0;
|
||
virtual ~ContactInit(){};
|
||
};
|
||
|
||
```
|
||
|
||
**Contact**类:
|
||
|
||
```c++
|
||
class Contact : public ContactInterface, public ContactInit
|
||
{
|
||
private:
|
||
MainStrategy* setMainStrategy(int);
|
||
public:
|
||
Contact();
|
||
~Contact();
|
||
int refresh() const;
|
||
void welcome() const;
|
||
};
|
||
|
||
```
|
||
|
||
**MainStrategy**类:
|
||
|
||
```c++
|
||
class MainStrategy : public CheckInterface
|
||
{
|
||
public:
|
||
MainStrategy();
|
||
virtual ~MainStrategy();
|
||
virtual int doMainStrategy() = 0;
|
||
protected:
|
||
void printAll() const;
|
||
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
|
||
};
|
||
|
||
```
|
||
|
||
**MainViewMenuInterface**类:
|
||
|
||
```c++
|
||
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();
|
||
};
|
||
|
||
```
|
||
|
||
# 四 系统测试
|
||
|
||
## 4.1 登录界面
|
||
|
||
系统运行开始的界面如图所示:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/32dbe51ca9ec80a24acaf075328f0d88.writebug)
|
||
|
||
主要通过选择结构和循环结构实现界面的前进和后退。例如,第一个登录界面出现5个选择:1.新建,2.删除,3.修改,4.浏览,5.退出
|
||
|
||
## 4.2 添加联系人
|
||
|
||
在开始界面输入“1”即添加新的成员:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/3daf468c06b3014d605f10ec00365ebe.writebug)
|
||
|
||
若显示 Information Entry Success! 则录入数据成功。若显示Information Error! 则录入数据失败。如图则因为在电话(TEL)中出现了中文字符。随后将返回主界面。
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/fe23be738be6c25fadf86502f96ea086.writebug)
|
||
|
||
## 4.3 删除联系人
|
||
|
||
在主界面输入2可删除成员:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/e6c9d2abdbec776664748b61fbff9f9f.writebug)
|
||
|
||
如我们希望删除(2)数据,则键入2:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/a9e40e12e21a997342a33c55b0fe34d0.writebug)
|
||
|
||
就可以得到(2)的详细数据。输入y/n即可选择是否删除该成员。随后程序将返回主界面。
|
||
|
||
## 4.4 修改联系人
|
||
|
||
在主界面下输入3可以修改已有的成员:我们希望修改刚刚加入的成员(1)的电话号码,同时加入他所有常用的QQ号码:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/22cda86e3b5dd5bce444b94980e3bc54.writebug)
|
||
|
||
键入1, 可修改第一个人的信息,并按照需求修改信息:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/c1775a7e9a2c6fccf937f8a0b0858f0d.writebug)
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/68481038dfb78a97894e0f0b21abdf82.writebug)
|
||
|
||
确认无误后即可修改信息,随后返回主界面。
|
||
|
||
## 4.4 搜索联系人
|
||
|
||
输入4即可按需求分类查看搜索成员:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/483615b4dc7d81f9a00b9aa623108fd0.writebug)
|
||
|
||
键入1进行精准匹配,该模式下只匹配名字:如输入“严恩伟”后匹配信息如图:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/c6d8d639c5f8e2d9fdc402b98bcabd29.writebug)
|
||
|
||
随后可以根据自身需求选择1-3选项。此处不再演示。
|
||
|
||
在view模式下键入2进行模糊匹配,该模式匹配名字,电话,地址。只要出现匹配字符即给与显示。如输入“181”后显示:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/f97505b0dc12dcd8e79ea14b481cbc0f.writebug)
|
||
|
||
选择1-3即可进入其详细页面。此处不再演示。
|
||
|
||
在view模式下键入3进行分类(category)匹配。该模式会列出所有的分类,并可根据用户选择的分类进行罗列(其中未设置的分类被标记为Unset):
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/ecf4c1f266c330014fc34ffbdf9086bf.writebug)
|
||
|
||
根据提示选择1-2即可进入相应页面。选择0即可退出。
|
||
|
||
在view模式下键入4进行全局匹配。该模式会列出所有的成员:
|
||
|
||
![](http://www.write-bug.com/myres/static/uploads/2021/10/19/b49f90fdd2d7887d5d1e1251d2001d06.writebug)
|
||
|
||
在view模式下键入5退出view模式。
|
||
|
||
在主菜单中键入5退出程序。 |