本文实例为大家分享了C++实现通讯录的具体代码,供大家参考,具体内容如下
简介:通讯录由一个拥有者以及通讯信息组成。
基本功能:增删改查
拥有者和通讯信息的基础结构相同,由struct构成
struct Person {
int m_id;
string m_name;
string m_tele;//手机号码可以作为id,但是过于长(11位)
//string m_addr;
Person& operator = (const Person& r) {
if (this == &r) return *this;
m_id = r.m_id;
m_name = r.m_name;
m_tele = r.m_tele;
return *this;
}
};
Person:id+姓名+手机号。还可以添加需要的信息,例如:地址、性别
重载了一个=操作符
通讯录建立class AddressList
class AddressList {
private:
Person owner;//通讯录拥有者
vector<Person> information;//通讯录:好友的信息组成
public:
AddressList();
AddressList(const Person&, const vector<Person>& info = {});
AddressList(const AddressList&);
void Add(const Person&);//添加一个好友信息至通讯录
void Delete();//通过姓名删除
//通过电话号码删除
void Modify();//输入id 修改姓名and号码
void Search(int);//1:id搜索
//2:姓名搜索
//3:号码指定搜索
void Print()const;
//查看通讯录所有信息:仅显示id和姓名,详细信息输入id查看
};
cpp:
#include "AddressList.h"
#include <iostream>
using namespace std;
AddressList::AddressList() {}
AddressList::AddressList(const Person& r, const vector<Person>& info) {
owner = r;
for (const auto& i : info) {
information.push_back(i);
}
}
AddressList::AddressList(const AddressList& r) {
owner = r.owner;
for (const auto& i : r.information) {
information.push_back(i);
}
}
void AddressList::Add(const Person& p) {
//添加一个好友信息至通讯录
//首先确认不存在:id+tele
for (const auto& it : information) {
if (p.m_id == it.m_id) {
cout << "Id已存在,添加失败!\n";
return;
}
else if (p.m_tele == it.m_tele) {
cout << "Telephone已存在,添加失败!\n";
return;
}
}
information.push_back(p);
}
void AddressList::Delete() {
//通过姓名删除
//略:通过电话号码删除
string name;
cout << "姓名:"; cin >> name;
cout << "查找到信息如下:";
auto it = information.begin();
vector<int> info;//存储下标
for (int i(0); it != information.end(); ++it,++i) {
if (it->m_name == name) info.push_back(i);
}
if (info.size() == 0) {
cout << "No such name.\n";
return;
}
for (const auto& i : info) {
cout << i << ":\t" << information[i].m_id << '\t' << information[i].m_name
<< '\t' << information[i].m_tele << endl;
}
int ind;
cout << "输入下标(第一列)删除信息:";
cin>>ind;
for (const auto& i : info) {
if (i == ind) {
information.erase(information.begin() + i);
return;
}
}
cout << "输入信息错误,删除失败!\n";
}
void AddressList::Modify() {
//输入id:修改姓名and号码
long id;
cout << "Id:"; cin >> id;
cout << "查找到信息如下:\n";
auto it = information.begin();
for (; it != information.end(); ++it) {
if (it->m_id == id) {
cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
break;
}
}
if (it == information.end()) {
cout << "No such Id.\n";
return;
}
cout << "修改信息:\n";
string name;
string tele;
cout << "新的姓名:"; cin >> name;
cout << "新的号码:"; cin >> tele;
char c;
cout << "确认?<y/n> ";
cin >> c;
if (c == 'y' || c == 'Y') {
it->m_name = name;
it->m_tele = tele;
return;
}
cout << "取消修改!\n";
return;
}
void AddressList::Search(int cho) {
//1:id搜索
//2:姓名搜索
//3:号码指定搜索
int id;
string name;
string tele;
auto it = information.begin();
switch (cho) {
case 1:
cout << "Id:";
cin >> id;
cout << "搜索到的信息如下:\n";
for (it = information.begin(); it != information.end(); ++it) {
if (it->m_id == id) {
cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
break;
}
}
break;
case 2:
cout << "Name:";
cin >> name;
cout << "搜索到的信息如下:\n";
for (it = information.begin(); it != information.end(); ++it) {
if (it->m_name == name)
cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
}
break;
case 3:
cout << "Tele:";
cin >> tele;
cout << "搜索到的信息如下:\n";
for (it = information.begin(); it != information.end(); ++it) {
if (it->m_tele == tele) {
cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl;
break;
}
}
break;
default:break;
}
}
void AddressList::Print()const {
cout << "ID:" << owner.m_id << endl;
cout << "Owner:" << owner.m_name << endl
<< "Tele:" << owner.m_tele << endl;
int n(information.size());
cout << "通讯录人数:" << n << endl;
for (int i(0); i < n; ++i) {
cout << information[i].m_id << '\t' << information[i].m_name << endl;
}
while (1) {
cout << endl
<< "详细信息,请输入id:-1终止查看\n";
int id;
cin >> id;
if (id == -1) break;
bool b(false);
for (const auto& it : information) {
if (id == it.m_id) {
b = true;
cout << it.m_id << '\t' << it.m_name << '\t' << it.m_tele << endl;
break;
}
}
if (!b) {
cout << "No such Id.!" << endl;
}
}
}
main.cpp:测试
#include"AddressList.h"
#include <iostream>
using namespace std;
int main() {
Person p;
{
p.m_id = 0;
p.m_name = "一号";
p.m_tele = "11012011900";//任意
}
int I = 1;//m_id编号
AddressList addr(p);
{
cout << "0.退出\n"
<< "1.添加\n"
<< "2.删除\n"
<< "3.修改\n"
<< "4.搜索\n"
<< "5.查看\n"
<< endl
<< endl;
}
int cho2;
bool b(true);
while (b) {
int cho;
int id;
string name;
string tele;
cout << "Your choose:";
cin >> cho;
switch (cho) {
case 0:
b = false;
break;
case 1:
cout << "添加信息:\n"
<< "姓名:";
cin >> name;
cout << "号码:";
cin >> tele;
id = I++;
{
p.m_id = id;
p.m_name = name;
p.m_tele = tele;
}
addr.Add(p);
break;
case 2:
cout << "删除信息:\n";
addr.Delete();
break;
case 3:
cout << "修改信息:\n";
addr.Modify();
break;
case 4:
cout << "搜索信息\n"
<< "1.Id\n"
<< "2.Name\n"
<< "3.Telephone\n";
cout << "Chosse:";
cin >> cho2;
addr.Search(cho2);
break;
case 5:
cout << "查看信息\n";
addr.Print();
break;
default:break;
}
cout << endl;
}
return 0;
}
截图: