forked from bbw7561135/Numerical-Algorithms-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadressbook.cpp
More file actions
292 lines (225 loc) · 5.64 KB
/
adressbook.cpp
File metadata and controls
292 lines (225 loc) · 5.64 KB
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
#include <iostream>
#include <string>
#define MAX 2
using namespace std;
void show_menu()
{
cout << "******************************" << endl;
cout << "*******1.add persons**********" << endl;
cout << "*******2.show persons*********" << endl;
cout << "*******3.delete persons*******" << endl;
cout << "*******4.search persons*******" << endl;
cout << "*******5.modify persons*******" << endl;
cout << "*******6.clear persons********" << endl;
cout << "*******0.exit*****************" << endl;
}
struct Person
{
string m_name;
int m_sex;//1man 2woman
int m_age;
string m_phone;
string m_addr;
};
struct Addressbooks
{
struct Person person_array[MAX];
int m_size;
};
void add_person(Addressbooks *abs)
{
if(abs->m_size == MAX)
{
cout << "filled, can not add persons" << endl;
return;
}
else
{
string name;
cout << "enter name: " << endl;
cin >> name;
abs->person_array[abs->m_size].m_name = name;
cout << "sex: 1.Man 2.Woman" << endl;
int sex=0;
while(true)
{
cin >> sex;
if(sex==1||sex==2)
{
abs->person_array[abs->m_size].m_sex = sex;
break;
}
cout << "enter 1 or 2" << endl;
}
cout << "age" << endl;
int age = 0;
cin >> age;
abs->person_array[abs->m_size].m_age = age;
cout << "phone number" << endl;
string phone = "";
cin >> phone;
abs->person_array[abs->m_size].m_phone = phone;
cout << "address" << endl;
string address;
cin >> address;
abs->person_array[abs->m_size].m_addr = address;
abs->m_size++;
cout << "successfully add a person." << endl;
//system("clear");
}
}
void show_person(Addressbooks *abs)
{
if(abs->m_size==0)
{
cout << "no person" << endl;
}
else
{
for(int i=0;i<abs->m_size;i++)
{
cout << "name: " << abs->person_array[i].m_name << endl;
cout << "sex: " << abs->person_array[i].m_sex << endl;
cout << "age: " << abs->person_array[i].m_age << endl;
cout << "phone: " << abs->person_array[i].m_phone << endl;
cout << "address: " << abs->person_array[i].m_addr << endl;
}
}
}
int is_exist(Addressbooks *abs, string name)
{
for(int i=0;i<abs->m_size;i++)
{
if(abs->person_array[i].m_name==name)
{
return i;
}
}
return -1;
}
void delete_person(Addressbooks *abs)
{
cout << "person want to be deleted" << endl;
string name;
cin >> name;
int ret=is_exist(abs,name); //abs here is point
if(ret != -1)
{
for(int i=ret;i<abs->m_size-1;i++)
{
abs->person_array[i]=abs->person_array[i+1];
}
abs->m_size--;
cout<<"delete successfully."<<endl;
}
else
{
cout << "no such person" << endl;
}
}
void find_person(Addressbooks *abs)
{
cout << "person to look for" << endl;
string name;
cin >> name;
int ret = is_exist(abs,name);
if(ret !=-1)
{
cout << "name: " << abs->person_array[ret].m_name << endl;
cout << "sex: " << abs->person_array[ret].m_sex << endl;
cout << "age: " << abs->person_array[ret].m_age << endl;
cout << "phone: " << abs->person_array[ret].m_phone << endl;
cout << "address: " << abs->person_array[ret].m_addr << endl;
}
else
{
cout << "no such person." << endl;
}
}
void modify_person(Addressbooks *abs)
{
cout << "person to modify" << endl;
string name;
cin >> name;
int ret = is_exist(abs,name);
if(ret !=-1)
{
string name;
cout << "enter name: " << endl;
cin >> name;
abs->person_array[ret].m_name = name;
cout << "sex: 1.Man 2.Woman" << endl;
int sex=0;
while(true)
{
cin >> sex;
if(sex==1||sex==2)
{
abs->person_array[ret].m_sex = sex;
break;
}
cout << "enter 1 or 2" << endl;
}
cout << "age" << endl;
int age = 0;
cin >> age;
abs->person_array[ret].m_age = age;
cout << "phone number" << endl;
string phone = "";
cin >> phone;
abs->person_array[ret].m_phone = phone;
cout << "address" << endl;
string address;
cin >> address;
abs->person_array[ret].m_addr = address;
cout << "successfully modify a person." << endl;
}
else
{
cout << "no such person" << endl;
}
}
void clear_person(Addressbooks *abs)
{
abs->m_size = 0;
cout << "all clear" << endl;
}
int main()
{
int select = 0;
Addressbooks abs;
abs.m_size = 0;
while(true)
{
show_menu();
cin >> select;
switch(select)
{
case 1: //add
add_person(&abs);
break;
case 2: //show
show_person(&abs);
break;
case 3: //delete
delete_person(&abs);
break;
case 4: //search
find_person(&abs);
break;
case 5: //modify
modify_person(&abs);
break;
case 6: //clear
clear_person(&abs);
break;
case 0: //exit
cout << "Bye !" << endl;
return 0;
break;
default:
break;
}
}
return 0;
}