通讯录的实现方式很多,也可用STL实现,在本例代码中,先封装好数据类型,再实现底层数据运算,最后封装上层字符界面的操作,实现上层与下层的交互。
数据节点如下:
//定义节点数据类型
typedef pair<string, pair<string, string> > m_pair;//重命名自定义pair
class PersonList
{
public:
vector<m_pair> m_vector;
vector<m_pair>::iterator it;
vector<m_pair>::const_iterator const_it;
};
底层类的实现
//底层链表类数据操作模块
class AddressList
{
public:
//构造
AddressList(){}
//析构
~AddressList(){}
//读入数据
void CreateList(string file_name)
{
ifstream readFile;
readFile.open(file_name);
//文件流对象关联失败
if (readFile.bad() || readFile.fail())
{
exit(0);
}
//正常关联流对象,读取数据
if (readFile.is_open())
{
string txtstr, listTxt;//txtstr存放文件流对象读取信息,listTxt读取内存中字符串流信息
while (getline(readFile, txtstr))
{
if (readFile.good())
{
istringstream istring(txtstr);//定义并初始化字符串流
m_pair id_name_tel;//定义学生信息关联容器
//在内存中以‘ ’分割文件流对象数据,并赋值关联容器
getline(istring, listTxt, ' ');//得到id
id_name_tel.first.assign(listTxt);
getline(istring, listTxt, ' ');//得到name
id_name_tel.second.first.assign(listTxt);
getline(istring, listTxt, ' ');//得到tel
id_name_tel.second.second.assign(listTxt);
List.m_vector.push_back(id_name_tel);//读入顺序容器
}
}
readFile.close();
}
}
//插入新数据
bool insert_new_info(m_pair &new_info)
{
//读入容器
List.m_vector.push_back(new_info);
return true;
}
//搜索学号删除节点
bool del_info(string id)
{
List.const_it = List.m_vector.begin();//初始化迭代器
for (auto this_info : List.m_vector)//遍历顺序容器
{
//搜索到对应信息
if (this_info.first.compare(id)==0)
{
List.m_vector.erase(List.const_it);//删除
return true;
}
//否则迭代器+1
else
{
List.const_it++;
}
}
//没有对应信息
return false;
}
//查找学生姓名,找到则返回学生信息
bool is_fond(string name, m_pair &res_info)
{
for (auto this_info : List.m_vector)//遍历顺序容器
{
//搜索到对应信息
if (this_info.second.first.compare(name)==0)
{
res_info.first = this_info.first;//返回学号
res_info.second.first = this_info.second.first;//返回姓名
res_info.second.second = this_info.second.second;//返回电话号码
return true;
}
}
//没有对应信息
return false;
}
//输出所有信息
void output_all()
{
cout << "ID" << ends << "NAME" << ends << "TEL" << endl;
for (auto this_info : List.m_vector)//遍历顺序容器
{
//输出信息
cout << this_info.first << ends
<< this_info.second.first << ends
<< this_info.second.second << endl;
}
}
//清空所有学生信息
bool clear_all()
{
List.m_vector.erase(List.m_vector.begin(), List.m_vector.end());//删除所有信息
return true;
}
//排序
bool sort_info()
{
sort(List.m_vector.begin(), List.m_vector.end());//排序
return true;
//output_all();//输出
}
//文件保存
bool is_save()
{
ofstream writeFile;
writeFile.open("data");
if (writeFile.bad() || writeFile.fail())//无法关联流对象,结束程序运行
{
return false;
exit(0);
}
else if (writeFile.is_open())//正确关联流对象
{
for (auto this_info : List.m_vector)//遍历顺序容器
{
//输入文件
writeFile << this_info.first << ' '
<< this_info.second.first << ' '
<< this_info.second.second << '\n';
}
writeFile.close();
return true;
}
else
{
return false;
}
}
private:
PersonList List;
};
上层类的实现
//上层字符界面模块
class GUI_INFO
{
public:
//构造
GUI_INFO()
{
m_list.CreateList("data");
}
//析构
~GUI_INFO(){}
//外接口
void GUI_operations()
{
judge_input_info();//命令提示界面
}
private:
//字符提示界面
void char_mention()
{
cout << "Display\tInsert\tDelete\tSearch\tSort\tExit" << endl;
cout << "cmd>";
cin >> choice;
}
void char_del_mention()
{
cout << "Part\tAll" << endl;
cout << "cmd>";
cin >> choice;
}
//输入信息判断
void judge_input_info()
{
bool flag = true;
while (flag)
{
char_mention();
if (choice.compare("Display") == 0 || choice.compare("display") == 0)
{
output_all_info();//输出所有信息
}
else if (choice.compare("Insert") == 0 || choice.compare("insert") == 0)
{
input_new_info();//插入新信息
}
else if (choice.compare("Delete") == 0 || choice.compare("delete") == 0)
{
char_del_mention();//选择删除类型
if (choice.compare("Part") == 0 || choice.compare("part") == 0)
{
delete_this_info();//删除指定信息
}
else
{
clear_all_info();//删除所有信息
}
}
else if (choice.compare("Search") == 0 || choice.compare("search") == 0)
{
get_this_info();//查找信息
}
else if (choice.compare("Sort") == 0 || choice.compare("sort") == 0)
{
sort_this_info();//对当前信息进行排序
}
else if (choice.compare("Exit") == 0 || choice.compare("exit") == 0)
{
cout << "Save those operations to data<Y/N>:";
cin >> choice;
if (choice.compare("Y") == 0 || choice.compare("y") == 0)
{
save_this_info();//保存信息
}
flag = false;//退出循环
}
else
{
cout << "Invilid commend." << endl;//输入错误命令处理
}
}
}
//插入信息
void input_new_info()
{
//输入信息
m_pair new_info;
cout << "Input new id, name, tel:";
cin >> new_info.first//输入学号
>> new_info.second.first//输入姓名
>> new_info.second.second;//输出电话号码
//插入信息
if (m_list.insert_new_info(new_info))
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//搜索对应学号下信息并删除该信息
void delete_this_info()
{
//输入信息—id
string id;
cout << "Input id:";
cin >> id;
//删除信息
if (m_list.del_info(id))
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//查找对应姓名学生的所有信息
void get_this_info()
{
//输入信息—name
string name;
m_pair this_info;
cout << "Input name:";
cin >> name;
//查找信息,找到输出,找不到输出提示
if (m_list.is_fond(name, this_info))
{
cout << "ID: " << this_info.first << ends//输出学号
<< "NAME: " << this_info.second.first << ends//输出姓名
<< "TEL: " << this_info.second.second << endl;//输出电话号码
}
else
{
cout << "NO." << endl;
}
}
//输出所有信息
void output_all_info()
{
m_list.output_all();//输出所有信息
}
//排序当前信息
void sort_this_info()
{
if (m_list.sort_info())//排序
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//清空当前信息
void clear_all_info()
{
if (m_list.clear_all())//清空当前信息
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//保存当前所有信息至文件
void save_this_info()
{
if (m_list.is_save())//正确保存
{
cout << "OK." << endl;
}
else//保存出错
{
cout << "NO." << endl;
}
}
//上层类数据成员
AddressList m_list;//底层类接口数据
string choice;//用户选择
};
完整实例
/*
学生通讯录的实现,调用C++标准模板库
*/
#include<iostream>
#include<string>
#include<fstream>
#include<utility>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;
//定义节点数据类型
typedef pair<string, pair<string, string> > m_pair;//重命名自定义pair
class PersonList
{
public:
vector<m_pair> m_vector;
vector<m_pair>::iterator it;
vector<m_pair>::const_iterator const_it;
};
//底层链表类数据操作模块
class AddressList
{
public:
//构造
AddressList(){}
//析构
~AddressList(){}
//读入数据
void CreateList(string file_name)
{
ifstream readFile;
readFile.open(file_name);
//文件流对象关联失败
if (readFile.bad() || readFile.fail())
{
exit(0);
}
//正常关联流对象,读取数据
if (readFile.is_open())
{
string txtstr, listTxt;//txtstr存放文件流对象读取信息,listTxt读取内存中字符串流信息
while (getline(readFile, txtstr))
{
if (readFile.good())
{
istringstream istring(txtstr);//定义并初始化字符串流
m_pair id_name_tel;//定义学生信息关联容器
//在内存中以‘ ’分割文件流对象数据,并赋值关联容器
getline(istring, listTxt, ' ');//得到id
id_name_tel.first.assign(listTxt);
getline(istring, listTxt, ' ');//得到name
id_name_tel.second.first.assign(listTxt);
getline(istring, listTxt, ' ');//得到tel
id_name_tel.second.second.assign(listTxt);
List.m_vector.push_back(id_name_tel);//读入顺序容器
}
}
readFile.close();
}
}
//插入新数据
bool insert_new_info(m_pair &new_info)
{
//读入容器
List.m_vector.push_back(new_info);
return true;
}
//搜索学号删除节点
bool del_info(string id)
{
List.const_it = List.m_vector.begin();//初始化迭代器
for (auto this_info : List.m_vector)//遍历顺序容器
{
//搜索到对应信息
if (this_info.first.compare(id)==0)
{
List.m_vector.erase(List.const_it);//删除
return true;
}
//否则迭代器+1
else
{
List.const_it++;
}
}
//没有对应信息
return false;
}
//查找学生姓名,找到则返回学生信息
bool is_fond(string name, m_pair &res_info)
{
for (auto this_info : List.m_vector)//遍历顺序容器
{
//搜索到对应信息
if (this_info.second.first.compare(name)==0)
{
res_info.first = this_info.first;//返回学号
res_info.second.first = this_info.second.first;//返回姓名
res_info.second.second = this_info.second.second;//返回电话号码
return true;
}
}
//没有对应信息
return false;
}
//输出所有信息
void output_all()
{
cout << "ID" << ends << "NAME" << ends << "TEL" << endl;
for (auto this_info : List.m_vector)//遍历顺序容器
{
//输出信息
cout << this_info.first << ends
<< this_info.second.first << ends
<< this_info.second.second << endl;
}
}
//清空所有学生信息
bool clear_all()
{
List.m_vector.erase(List.m_vector.begin(), List.m_vector.end());//删除所有信息
return true;
}
//排序
bool sort_info()
{
sort(List.m_vector.begin(), List.m_vector.end());//排序
return true;
//output_all();//输出
}
//文件保存
bool is_save()
{
ofstream writeFile;
writeFile.open("data");
if (writeFile.bad() || writeFile.fail())//无法关联流对象,结束程序运行
{
return false;
exit(0);
}
else if (writeFile.is_open())//正确关联流对象
{
for (auto this_info : List.m_vector)//遍历顺序容器
{
//输入文件
writeFile << this_info.first << ' '
<< this_info.second.first << ' '
<< this_info.second.second << '\n';
}
writeFile.close();
return true;
}
else
{
return false;
}
}
private:
PersonList List;
};
//上层字符界面模块
class GUI_INFO
{
public:
//构造
GUI_INFO()
{
m_list.CreateList("data");
}
//析构
~GUI_INFO(){}
//外接口
void GUI_operations()
{
judge_input_info();//命令提示界面
}
private:
//字符提示界面
void char_mention()
{
cout << "Display\tInsert\tDelete\tSearch\tSort\tExit" << endl;
cout << "cmd>";
cin >> choice;
}
void char_del_mention()
{
cout << "Part\tAll" << endl;
cout << "cmd>";
cin >> choice;
}
//输入信息判断
void judge_input_info()
{
bool flag = true;
while (flag)
{
char_mention();
if (choice.compare("Display") == 0 || choice.compare("display") == 0)
{
output_all_info();//输出所有信息
}
else if (choice.compare("Insert") == 0 || choice.compare("insert") == 0)
{
input_new_info();//插入新信息
}
else if (choice.compare("Delete") == 0 || choice.compare("delete") == 0)
{
char_del_mention();//选择删除类型
if (choice.compare("Part") == 0 || choice.compare("part") == 0)
{
delete_this_info();//删除指定信息
}
else
{
clear_all_info();//删除所有信息
}
}
else if (choice.compare("Search") == 0 || choice.compare("search") == 0)
{
get_this_info();//查找信息
}
else if (choice.compare("Sort") == 0 || choice.compare("sort") == 0)
{
sort_this_info();//对当前信息进行排序
}
else if (choice.compare("Exit") == 0 || choice.compare("exit") == 0)
{
cout << "Save those operations to data<Y/N>:";
cin >> choice;
if (choice.compare("Y") == 0 || choice.compare("y") == 0)
{
save_this_info();//保存信息
}
flag = false;//退出循环
}
else
{
cout << "Invilid commend." << endl;//输入错误命令处理
}
}
}
//插入信息
void input_new_info()
{
//输入信息
m_pair new_info;
cout << "Input new id, name, tel:";
cin >> new_info.first//输入学号
>> new_info.second.first//输入姓名
>> new_info.second.second;//输出电话号码
//插入信息
if (m_list.insert_new_info(new_info))
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//搜索对应学号下信息并删除该信息
void delete_this_info()
{
//输入信息—id
string id;
cout << "Input id:";
cin >> id;
//删除信息
if (m_list.del_info(id))
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//查找对应姓名学生的所有信息
void get_this_info()
{
//输入信息—name
string name;
m_pair this_info;
cout << "Input name:";
cin >> name;
//查找信息,找到输出,找不到输出提示
if (m_list.is_fond(name, this_info))
{
cout << "ID: " << this_info.first << ends//输出学号
<< "NAME: " << this_info.second.first << ends//输出姓名
<< "TEL: " << this_info.second.second << endl;//输出电话号码
}
else
{
cout << "NO." << endl;
}
}
//输出所有信息
void output_all_info()
{
m_list.output_all();//输出所有信息
}
//排序当前信息
void sort_this_info()
{
if (m_list.sort_info())//排序
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//清空当前信息
void clear_all_info()
{
if (m_list.clear_all())//清空当前信息
{
cout << "OK." << endl;
}
else
{
cout << "NO." << endl;
}
}
//保存当前所有信息至文件
void save_this_info()
{
if (m_list.is_save())//正确保存
{
cout << "OK." << endl;
}
else//保存出错
{
cout << "NO." << endl;
}
}
//上层类数据成员
AddressList m_list;//底层类接口数据
string choice;//用户选择
};
int main()
{
GUI_INFO operations;
operations.GUI_operations();//调用外层字符界面类操作函数
return 0;
}
因为测试时使用了真是的信息,所以这里就不给出测试运行的结果了。有需要的娃自己动手试试吧^_^