目录
完整源代码及文件在资源链接: C++实现通讯录管理系统(OOP类,链表,文件读取等操作实现通讯录的基本功能) 下载后可直接运行使用
分析
1.需求分析
需求分析
1、具有联系人基本信息的添加、修改、删除、显示信息和查询功能的通讯录管理系统。
2、联系人数据:姓名,性别,索引、电话号码和QQ账号,电子邮件。
3、可对记录中的姓名和电话号码和QQ账号进行修改。
4、可增加和删除联系人。
5、可显示所有的联系人信息。
6、可按人名或电话号码进行查询联系人基本信息或联系人是否存在。
2.设计
1.添加联系人。
2.查询联系人
3.修改联系人
4.删除联系人
5.显示联系人基本信息
6.保存联系人基本信息到文件
3.抽象类型定义
//*创建Info类*
class Info
{
public:
Info();//构造函数
friend class AddressBook; //声明AddressBook类为其友元类
#ifdef _VC2010_VER_
friend ofstream & operator << (ofstream &,const Info &);//运算符 << 重载
friend ifstream & operator >> (ifstream &, Info &);//运算符 >> 重载
#endif
private:
// 禁用拷贝
Info(const Info &);
const Info &operator=(const Info &);
//定义成员变量
char name[20]; // 姓名
char gender[5]; // 性别
char phone[50]; // 电话号码
char qq[20]; // qq号码
char email[50]; // 电子邮件
int index; // 索引值
Info * next;
};
通讯录管理系统功能介绍
1.添加联系人
利用链表存储数据,以链式存储结构存储通讯录数据。
使用链表存储数据,方便删除和修改,提高了代码灵活性
void AddressBook::Add() //*添加数据*
{
cout << "请输入联系人信息: " <<endl;
Info * temp = new Info;
// 实际开发中这里要捕获异常:内存申请失败
cout << "姓名( 1 ~ " << sizeof(temp->name) - 1 << " 字符): " << ends;
cin >> temp->name;
cout << "性别( 男 女 ): " << ends;
cin >> temp->gender;
cout << "电话( 1 ~ " << sizeof(temp->phone) - 1 << " 字符): " << ends;
cin >> temp->phone;
cout << "QQ( 1 ~ " << sizeof(temp->qq) - 1 << " 字符): " << ends;
cin >> temp->qq;
cout << "email( 0 ~ " << sizeof(temp->email) - 1 << " 字符): " << ends;
cin >> temp->email;
AddBase(temp);
}
2.查询联系人
输入基本信息后进入文件函数中,打开并读取文件,从而查询联系人
void AddressBook::Query() //*查询联系人(按姓名或电话号)*
{
cout << " 请选择查询方式 " << endl;
cout << " 0. 按名字 " << endl;
cout << " 1. 电话 " << endl;
int choose = 0;
cin >> choose;
//输入查找方式
char str[50] = {0};
switch (choose)
{
case 0:
cout << "请输入名字: " << ends;
cin >> str;
break;
case 1:
cout << "请输入电话: " << ends;
cin >> str;
break;
default:
cout << "没有该选项" << endl;
return;
}
Info * result = head;
//输出表头信息
ShowInfoTitle();
int index = 0;
do
{
//查找
result = QueryBase(result, choose, str);
// 显示查询到的项
if (result!= NULL)
{
result->index = index;
ShowInfo(result);
index++;
result = result->next;
}
} while (result != NULL);
}
3.修改联系人
输入要修改的联系人基本信息,查找是否存在后,进行修改(此处的修改是利用DelBase(index)函数删除原有的,利用Add()函数添加新的联系人),这就是使用链表的好处。
void AddressBook::Modify() //*修改联系人信息*
{
cout << "请选择要修改的联系人(输入序号): " << endl;
//输出所有信息
int count = ShowAllInfo();
int index = 0;
cin >> index;
//判断输入的序号是否存在
if (index < 0 || index >= count)
{
cout << "选择无效, 没有该序号的联系人" << endl;
return;
}
//删除
DelBase(index);
//添加
Add();
//完成修改
}
4.删除联系人
先判断联系人是否存在,若存在,利用DelBase(index);进行链表的删除操作
void AddressBook::Del() //*删除联系人*
{
cout << "请选择要删除的联系人(输入序号): " << endl;
//输出所有的信息
int count = ShowAllInfo();
int index = 0;
//输入序号
cin >> index;
//判断序号是否存在
if (index < 0 || index >= count)
{
cout << "选择无效, 没有该序号的联系人" << endl;
return;
}
//存在则删除
DelBase(index);
}
5.显示所有联系人
void AddressBook::ShowAll()//*输出所有信息*
{
ShowAllInfo();
}
6.保存到文件
将通讯录中的数据保存在data.txt文件中
1.可读取
2.可写入
void AddressBook::Load() //*读取文件中的数据*
{
//文件目录当前目录下的data.txt
ifstream ifile(".\\data.txt");
//如果没有文件则,打印:"打开文件失败"
if (!ifile)
{
cout << "打开文件失败" << endl;
return;
}
//文件存在读取出来
while (!ifile.eof())
{
Info * temp = new Info;
#ifdef _VC2010_VER_
ifile >> *temp;
#else
ifile >> temp->name >> temp->gender >> temp->phone >> temp->qq >> temp->email;
#endif
//加入到链表中
if (ifile)
{
AddBase(temp);
}
}
//关闭流
ifile.close();
}
void AddressBook::Save() //*将数据保存到文件*
{
//将文件保存到
ofstream ofile(".\\data.txt", ios::trunc);
if (!ofile)
{
cout << "保存失败" << endl;
return;
}
Info * temp = head;
//写入
while (temp != NULL)
{
#ifdef _VC2010_VER_
ofile <<* temp<<endl;
#else
ofile << temp->name << " " << temp->gender << " " << temp->phone << " " << temp->qq << " " << temp->email << " " << endl;
#endif
temp = temp->next;
}
ofile.close();
}
0.退出系统
其他副函数
1.按照序号删除
进行链表的删除操作。
void AddressBook::DelBase(int index) //*按序号删除*
{
Info * temp = head;
Info ** parent = &head;
while (temp != NULL)
{
if (temp->index == index)
{
*parent = temp->next;
delete temp;
return;
}
parent = &temp->next;
temp = temp->next;
}
}
2.输出联系人信息
void AddressBook::ShowInfoTitle() //*输出联系人信息*
{
cout << "序号 姓名 性别 电话 QQ email" << endl;
}
void AddressBook::ShowInfo(const Info * pInfo)
{
cout << setiosflags(ios::left) // 输出左对齐
<< setw(14) << pInfo->index
<< setw(14) << pInfo->name
<< setw(14) << pInfo->gender
<< setw(14) << pInfo->phone
<< setw(14) << pInfo->qq
<< setw(14) << pInfo->email << endl;
}
3.输出序号
int AddressBook::ShowAllInfo() //*输出序号*
{
int index = 0;
Info * temp = head;
ShowInfoTitle();
while (temp)
{
temp->index = index;
index++;
ShowInfo(temp);
temp = temp->next;
}
return index;
}
4.主菜单函数
int menu()//主菜单函数
{
system("cls");//清屏
cout<<" ****************************************************************"<<endl;
cout<<" | |"<<endl;
cout<<" | 欢迎登录通讯录管理系统 |"<<endl;
cout<<" | |"<<endl;
cout<<" ****************************************************************"<<endl;
cout<<" | ☆1 . 添加联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆2 . 查询联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆3 . 修改联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆4 . 删除联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆5 . 显示所有联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆6 . 保存到文件 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆0 . 退出系统 |"<<endl;
cout<<" ****************************************************************"<<endl;
int m = 0;
do
{
cout << "请输入选项0-8\n";
cin >> m;
} while( m < 0 || m > 8 );
return m;
}
main函数
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//*通讯录管理系统*
#include "addressmain.h"
//*主函数*
int main()
{
int m = 0;
AddressBook book;
book.Load();
do
{
m = menu();
switch (m)
{
case 0:
exit();//退出
break;
case 1:
book.Add();
break;
case 2:
book.Query();
break;
case 3:
book.Modify();
break;
case 4:
book.Del();
break;
case 5:
book.ShowAll();
break;
case 6:
book.Save();
break;
default:
cout << "暂不支持该选项\n" << endl;
}
cout << "回车继续..." << endl;
getch();
} while (m > 0);
book.Save();
return 0;
}
void exit(); //*退出函数,用来退出通讯录管理系统*
int menu(); //*主菜单函数,用于输出通讯录界面*
//#endif
end
完整源代码及文件在资源链接: C++实现通讯录管理系统(OOP类,链表,文件读取等操作实现通讯录的基本功能)
下载后可直接运行使用
完整方法代码
//*Info类*
Info::Info()//构造函数
{
//初始化
memset(name, 0, sizeof(name));
memset(gender,0, sizeof(gender));
memset(phone, 0, sizeof(phone));
memset(qq, 0, sizeof(qq));
memset(email, 0, sizeof(email));
index = 0;
next = NULL;
}
#ifdef _VC2010_VER_
ofstream & operator << (ofstream & ofs,const Info & c)//运算符 << 重载
{
ofs << c.name << " "<< c.gender << " " << c.phone << " " << c.qq << " " << c.email << " ";
return ofs;
}
ifstream & operator >> (ifstream & ifs,Info & c)//运算符 >> 重载
{
ifs >> c.name >> c.gender >> c.phone >> c.qq >> c.email;
return ifs;
}
#endif
//*AddressBook类*
AddressBook::AddressBook() //构造函数
{
head = NULL;
}
AddressBook::~AddressBook() //析构函数
{
Info * temp = head;
while (head != NULL)
{
temp = head;
head = head->next;
delete temp;
}
}
void AddressBook::Load() //*读取文件中的数据*
{
//文件目录当前目录下的data.txt
ifstream ifile(".\\data.txt");
//如果没有文件则,打印:"打开文件失败"
if (!ifile)
{
cout << "打开文件失败" << endl;
return;
}
//文件存在读取出来
while (!ifile.eof())
{
Info * temp = new Info;
#ifdef _VC2010_VER_
ifile >> *temp;
#else
ifile >> temp->name >> temp->gender >> temp->phone >> temp->qq >> temp->email;
#endif
//加入到链表中
if (ifile)
{
AddBase(temp);
}
}
//关闭流
ifile.close();
}
void AddressBook::Save() //*将数据保存到文件*
{
//将文件保存到
ofstream ofile(".\\data.txt", ios::trunc);
if (!ofile)
{
cout << "保存失败" << endl;
return;
}
Info * temp = head;
//写入
while (temp != NULL)
{
#ifdef _VC2010_VER_
ofile <<* temp<<endl;
#else
ofile << temp->name << " " << temp->gender << " " << temp->phone << " " << temp->qq << " " << temp->email << " " << endl;
#endif
temp = temp->next;
}
ofile.close();
}
void AddressBook::Add() //*添加数据*
{
cout << "请输入联系人信息: " <<endl;
Info * temp = new Info;
// 实际开发中这里要捕获异常:内存申请失败
cout << "姓名( 1 ~ " << sizeof(temp->name) - 1 << " 字符): " << ends;
cin >> temp->name;
cout << "性别( 男 女 ): " << ends;
cin >> temp->gender;
cout << "电话( 1 ~ " << sizeof(temp->phone) - 1 << " 字符): " << ends;
cin >> temp->phone;
cout << "QQ( 1 ~ " << sizeof(temp->qq) - 1 << " 字符): " << ends;
cin >> temp->qq;
cout << "email( 0 ~ " << sizeof(temp->email) - 1 << " 字符): " << ends;
cin >> temp->email;
AddBase(temp);
}
void AddressBook::Query() //*查询联系人(按姓名或电话号)*
{
cout << " 请选择查询方式 " << endl;
cout << " 0. 按名字 " << endl;
cout << " 1. 电话 " << endl;
int choose = 0;
cin >> choose;
//输入查找方式
char str[50] = {0};
switch (choose)
{
case 0:
cout << "请输入名字: " << ends;
cin >> str;
break;
case 1:
cout << "请输入电话: " << ends;
cin >> str;
break;
default:
cout << "没有该选项" << endl;
return;
}
Info * result = head;
//输出表头信息
ShowInfoTitle();
int index = 0;
do
{
//查找
result = QueryBase(result, choose, str);
// 显示查询到的项
if (result!= NULL)
{
result->index = index;
ShowInfo(result);
index++;
result = result->next;
}
} while (result != NULL);
}
void AddressBook::Modify() //*修改联系人信息*
{
cout << "请选择要修改的联系人(输入序号): " << endl;
//输出所有信息
int count = ShowAllInfo();
int index = 0;
cin >> index;
//判断输入的序号是否存在
if (index < 0 || index >= count)
{
cout << "选择无效, 没有该序号的联系人" << endl;
return;
}
//删除
DelBase(index);
//添加
Add();
//完成修改
}
void AddressBook::Del() //*删除联系人*
{
cout << "请选择要删除的联系人(输入序号): " << endl;
//输出所有的信息
int count = ShowAllInfo();
int index = 0;
//输入序号
cin >> index;
//判断序号是否存在
if (index < 0 || index >= count)
{
cout << "选择无效, 没有该序号的联系人" << endl;
return;
}
//存在则删除
DelBase(index);
}
void AddressBook::ShowAll()//*输出所有信息*
{
ShowAllInfo();
}
void AddressBook::AddBase(Info * pInfo)
{
Info ** parent = &head;
Info * temp = head;
while (temp)
{
// 按名字递增
if (strcmp(temp->name, pInfo->name) >= 0)
{
break;
}
parent = &temp->next;
temp = temp->next;
}
pInfo->next = temp;
*parent = pInfo;
}
Info * AddressBook::QueryBase(Info * start, int choose, const char * str) //*按名字或电话号查找*
{
while (start != NULL)
{
switch (choose)
{
case 0: // 按名字匹配
if (strcmp(start->name, str) == 0)
{
return start;
}else{
start=start->next;
continue;
}
break;
case 1: // 按电话号码匹配
if (strcmp(start->phone, str) == 0)
{
return start;
}else{
start=start->next;
continue;
}
break;
default:
break;
}
return NULL;
}
return start;
}
void AddressBook::DelBase(int index) //*按序号删除*
{
Info * temp = head;
Info ** parent = &head;
while (temp != NULL)
{
if (temp->index == index)
{
*parent = temp->next;
delete temp;
return;
}
parent = &temp->next;
temp = temp->next;
}
}
void AddressBook::ShowInfoTitle() //*输出联系人信息*
{
cout << "序号 姓名 性别 电话 QQ email" << endl;
}
void AddressBook::ShowInfo(const Info * pInfo)
{
cout << setiosflags(ios::left) // 输出左对齐
<< setw(14) << pInfo->index
<< setw(14) << pInfo->name
<< setw(14) << pInfo->gender
<< setw(14) << pInfo->phone
<< setw(14) << pInfo->qq
<< setw(14) << pInfo->email << endl;
}
int AddressBook::ShowAllInfo() //*输出序号*
{
int index = 0;
Info * temp = head;
ShowInfoTitle();
while (temp)
{
temp->index = index;
index++;
ShowInfo(temp);
temp = temp->next;
}
return index;
}
void exit()//退出函数
{
cout << " ****************************************************************"<<endl;
cout << " ******************* 谢谢使用 *****************"<<endl;
cout << " ****************************************************************"<<endl;
}
int menu()//主菜单函数
{
system("cls");//清屏
cout<<" ****************************************************************"<<endl;
cout<<" | |"<<endl;
cout<<" | 欢迎登录通讯录管理系统 |"<<endl;
cout<<" | |"<<endl;
cout<<" ****************************************************************"<<endl;
cout<<" | ☆1 . 添加联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆2 . 查询联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆3 . 修改联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆4 . 删除联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆5 . 显示所有联系人 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆6 . 保存到文件 |"<<endl;
cout<<" | |"<<endl;
cout<<" | ☆0 . 退出系统 |"<<endl;
cout<<" ****************************************************************"<<endl;
int m = 0;
do
{
cout << "请输入选项0-8\n";
cin >> m;
} while( m < 0 || m > 8 );
return m;
}