本文实例为大家分享了C++实现通讯录的具体代码,供大家参考,具体内容如下
简介:通讯录由一个拥有者以及通讯信息组成。
基本功能:增删改查
拥有者和通讯信息的基础结构相同,由struct构成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
#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:测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#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; } |
截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_31854267/article/details/80414242