『C++&C语言』简易通讯录类实现原理

『C++&C 语言』简易通讯录类实现原理

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <bits/stdc++.h>
using namespace std;
class Informat
{
private:
string phone;
string address;
string name;
public:
Informat():name(""),phone("12345"),address("Anywhere"){}
Informat(string a,string b,string q):name(a),address(q){ setpho(b);}
void setpho(string a)
{
while(a.length()!=11&&a.length()!=5){
cout<<"Please Key Again"<<endl;
cin>>a;
}
phone=a;
}
void setname(string a){ name=a;}
void setaddr(string a){address=a;}
string getpho() const{return phone;}
string getname()const{return name;}
string getaddr()const{return address;}
friend ostream&operator<<(ostream &out,const Informat &ob);
friend istream&operator>>(istream &in, Informat &ob);
};
ostream&operator<<(ostream &out,const Informat &ob){
out<<ob.name<<" "<<ob.phone<<" "<<ob.address<<" ";
return out;
}
istream &operator>>(istream &in,Informat &ob)
{
in>>ob.name>>ob.phone;
while(ob.phone.length()!=11&&ob.phone.length()!=5)
{
cout<<"Please Key Again"<<endl;
in>>ob.phone;
}
in>>ob.address;
return in;
}
/_int main()
{
Informat demo("李彦宏","1","中国北京");
cout<<demo<<endl;
Informat demo2;
string tem1,tem2,tem3;
cin>>demo;
cout<<demo.getname()<<' '<<demo.getpho()<<' '<<demo.getaddr()<<endl;
cin>>tem1>>tem2>>tem3;
demo2.setname(tem1);
demo2.setpho(tem2);
demo2.setaddr(tem3);
cout<<demo2<<endl;;
}_/
class Contacts
{
vector<Informat>infor;
map<string,int>na;
map<string,int>ph;
public:
Contacts()
{
load();
}
void create()
{
Informat tem;
cin>>tem;
infor.push_back(tem);
if(!na.insert(make_pair(tem.getname(),infor.size()-1)).second)
{
cout<<infor[na.find(tem.getname())->second]<<endl;
cout<<"Please Key \"Yes\" To Creat name(2) Or Key\"No\" Update and overwrite "<<endl;
string flag;
cin>>flag;
while(flag!="NO"&&flag!="No"&&flag!="no"&&flag!="YES"&&flag!="yes"&&flag!="Yes") cin>>flag;
if(flag=="NO"||flag=="No"||flag=="no")
{
na.erase(tem.getname());
na.insert(make_pair(tem.getname(),infor.size()-1));
}
else {
tem.setname(tem.getname()+" ");
na.insert(make_pair(tem.getname(),infor.size()-1));
}
}
if(!ph.insert(make_pair(tem.getpho(),infor.size()-1)).second)
{
cout<<infor[ph.find(tem.getpho())->second]<<endl;
cout<<"Please Key \"Yes\" To Creat Phone(2) Or Key\"No\" Update and overwrite "<<endl;
string flag;
cin>>flag;
while(flag!="NO"&&flag!="No"&&flag!="no"&&flag!="YES"&&flag!="yes"&&flag!="Yes") cin>>flag;
if(flag=="NO"||flag=="No"||flag=="no")
{
na.erase(tem.getpho());
na.insert(make_pair(tem.getpho(),infor.size()-1));
}
else {
tem.setpho(tem.getpho()+"2");
na.insert(make_pair(tem.getpho(),infor.size()-1));
}
}
}
void find1(string a)
{
if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null"){
cout<<infor[ph.find(a)->second]<<endl;
if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
cout<<infor[na.find(a)->second]<<endl;
return ;
}
}
else if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
cout<<infor[na.find(a)->second]<<endl;
}
else cout<<"查无此人/号"<<endl;
}
void modifyaddr(string a )
{
string w;
if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null"){
cout<<infor[ph.find(a)->second]<<endl;
cin>>w;
{
if(w=="No"||w=="no"||w=="NO")
if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
cout<<infor[na.find(a)->second]<<endl;
cin>>w;
infor[na.find(a)->second].setaddr(w);
return ;
}
else {
infor[na.find(a)->second].setaddr(w);
return ;
}
}
}
if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
cout<<infor[na.find(a)->second]<<endl;
cin>>w;
infor[na.find(a)->second].setaddr(w);
return ;
}
else cout<<"查无此人"<<endl;
}
void modifyname(string a )
{
string w;
if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null"){
cout<<infor[na.find(a)->second]<<endl;
cin>>w;
infor[na.find(a)->second].setname(w);
na.insert(make_pair(w,na.find(a)->second));
na.erase(a);
}
else cout<<"查无此人"<<endl;
}
void modifyphone(string a )
{
string w;
if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null"){
cout<<infor[ph.find(a)->second]<<endl;
cin>>w;
infor[ph.find(a)->second].setpho(w);
ph.insert(make_pair(w,ph.find(a)->second));
ph.erase(a);
}
else cout<<"查无此号"<<endl;
}
void delph(string a)
{
if(ph.find(a)!=ph.end()&&infor[ph.find(a)->second].getpho()!="Null")
{
infor[ph.find(a)->second].setpho("Null");
infor[ph.find(a)->second].setname("Null");
}
else cout<<"查无此号"<<endl;
}
void delna(string a)
{
if(na.find(a)!=na.end()&&infor[na.find(a)->second].getname()!="Null")
{
infor[na.find(a)->second].setpho("Null");
infor[na.find(a)->second].setname("Null");
}
else cout<<"查无此人"<<endl;
}
~ Contacts()
{
save();
}
void load()
{
ifstream in("d:\\shuju.txt",ios::in);
infor.clear();
na.clear();
ph.clear();
if(!in) return ;
Informat ob;
while(in>>ob)
{
infor.push_back(ob);
na.insert(make_pair(ob.getname(),infor.size()-1));
ph.insert(make_pair(ob.getpho(),infor.size()-1));
in.close();
}
}
void save()
{
ofstream out("d:\\shuju.txt",ios::out);
for(auto po=infor.begin();po!=infor.end();po++)
{
if((*po).getname()!="Null") out<<*po;
}
out.close();
}
};
int main()
{
Contacts ob;
ob.create();
ob.create();
ob.create();
string tem;
cin>>tem;
ob.find1(tem);
cin>>tem;
ob.find1(tem);
cin>>tem;
ob. modifyaddr(tem);
ob.find1(tem);
cin>>tem;
ob. modifyaddr(tem);
ob.find1(tem);
cin>>tem;
ob. modifyname(tem);
ob.find1(tem);
cin>>tem;
ob. modifyname(tem);
ob.find1(tem);
cin>>tem;
ob. modifyphone(tem);
ob.find1(tem);
cin>>tem;
ob. modifyphone(tem);
ob.find1(tem);
cin>>tem;
ob. delph(tem);
ob.find1(tem);
cin>>tem;
ob. delph(tem);
ob.find1(tem);
cin>>tem;
ob. delna(tem);
ob.find1(tem);
cin>>tem;
ob.delna(tem);
ob.find1(tem);
}



『C++&C语言』简易通讯录类实现原理
https://chiamzhang.github.io/2024/06/29/『C++&C语言』简易通讯录类实现原理/
Author
Chiam
Posted on
June 29, 2024
Licensed under