C++ 第一个实战项目:通讯录系统管理

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 # define MAX 1000
  5 
  6 struct Person
  7 {
  8     string m_Name;
  9     int m_Sex;
 10     int m_Age;
 11     string m_Phone;
 12     string m_Addr;
 13 };
 14 
 15 struct AddressBook
 16 {
 17     Person personArray[MAX];
 18     int m_Size;
 19 };
 20 
 21 
 22 
 23 void showMenu()
 24 {
 25     cout << "**************************" << endl;
 26     cout << "*****  1.添加联系人  *****" << endl;
 27     cout << "*****  2.显示联系人  *****" << endl;
 28     cout << "*****  3.删除联系人  *****" << endl;
 29     cout << "*****  4.查找联系人  *****" << endl;
 30     cout << "*****  5.修改联系人  *****" << endl;
 31     cout << "*****  6.清空联系人  *****" << endl;
 32     cout << "*****  0.退出通讯录  *****" << endl;
 33     cout << "**************************" << endl;
 34 }
 35 
 36 
 37 // 1. 添加联系人
 38 void addPerson(AddressBook* abs)
 39 {
 40     if (abs->m_Size == MAX)
 41     {
 42         cout << "通讯录已满!" << endl;
 43         return;
 44     }
 45 
 46     else
 47     {
 48         // 1. 
 49         string name;
 50         cout << "输入姓名:" << endl;
 51         cin >> name;
 52         abs->personArray[abs->m_Size].m_Name = name;
 53 
 54         // 2. 
 55         int sex = 0;
 56         cout << "输入性别:(1---男, 2---女)" << endl;
 57 
 58         while (true)
 59         {
 60             cin >> sex;
 61             if (sex == 1 || sex == 2)
 62             {
 63                 abs->personArray[abs->m_Size].m_Sex = sex;
 64                 break;
 65             }
 66 
 67             cout << "输入错误,请重新输入:" << endl;
 68             
 69 
 70         }
 71 
 72         // 3. 
 73         int age;
 74         cout << "输入年龄:" << endl;
 75         cin >> age;
 76         abs->personArray[abs->m_Size].m_Age = age;
 77 
 78         // 4. 
 79         string phone_num;
 80         cout << "输入电话" << endl;
 81         cin >> phone_num;
 82         abs->personArray[abs->m_Size].m_Phone = phone_num;
 83 
 84         // 5. 
 85         string address;
 86         cout << "输入住址:" << endl;
 87         cin >> address;
 88         abs->personArray[abs->m_Size].m_Addr = address;   
 89     }
 90 
 91     cout << "添加成功!" << endl;
 92     abs->m_Size++;
 93     system("pause");
 94     system("cls");
 95 
 96 
 97 }
 98 
 99 
100 // 2. 显示联系人
101 void showPerson(AddressBook* abs)
102 {
103     if (abs->m_Size == 0)
104     {
105         cout << "通讯录为空!" << endl;
106     }
107     
108     else
109     {
110         for (int i = 0; i < abs->m_Size; i++)
111         {
112             cout << "姓名:" << abs->personArray[i].m_Name << "\t";
113             cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "" : "") << "\t";
114             cout << "年龄:" << abs->personArray[i].m_Age << "\t";
115             cout << "电话号码:" << abs->personArray[i].m_Phone << "\t";
116             cout << "住址:" << abs->personArray[i].m_Addr << "\t" << endl;
117 
118         }
119     }
120     system("pause");
121     system("cls");
122 
123 }
124 
125 
126 // 3.1 删除联系人(先判断是否存在)
127 int isExist(AddressBook* abs, string name)
128 {
129     for (int i = 0; i < abs->m_Size; i++)
130     {
131         if (abs->personArray[i].m_Name == name)
132         {
133             return i;
134         }
135         
136     }
137 
138     return -1;
139 
140 }
141 
142 
143 // 3.2 删除
144 void deletePerson(AddressBook* abs, string name)
145 {
146     int position = isExist(abs, name);
147     if (position == -1)
148     {
149         cout << "查无此人!" << endl;
150     }
151     else
152     {
153         for (int i = position; i < abs->m_Size; i++)
154         {
155             abs->personArray[i].m_Name = abs->personArray[i + 1].m_Name;
156             abs->personArray[i].m_Age = abs->personArray[i + 1].m_Age;
157             abs->personArray[i].m_Sex = abs->personArray[i + 1].m_Sex;
158             abs->personArray[i].m_Phone = abs->personArray[i + 1].m_Phone;
159             abs->personArray[i].m_Addr = abs->personArray[i + 1].m_Addr;
160 
161 
162         }
163 
164         abs->m_Size--;
165     }
166 
167     cout << "删除成功!" << endl;
168     system("pause");
169     system("cls");
170 
171 
172 }
173 
174 
175 // 4.查找联系人
176 void findPerson(AddressBook* abs, string name)
177 {
178     int position = isExist(abs, name);
179     if (position == -1)
180     {
181         cout << "查无此人!" << endl;
182     }
183     else
184     {
185         cout << "姓名" << abs->personArray[position].m_Name << "\t"
186             << "年龄" << abs->personArray[position].m_Age << "\t"
187             << "性别" << (abs->personArray[position].m_Sex == 1 ? "":"") << "\t"
188             << "电话号码" << abs->personArray[position].m_Phone << "\t"
189             << "住址" << abs->personArray[position].m_Addr << "\t" << endl;
190     }
191     system("pause");
192     system("cls");
193 
194 }
195 
196 
197 // 5.修改联系人
198 void modifyPerson(AddressBook* abs)
199 {
200     cout << "输入要修改的联系人:" << endl;
201     string name;
202     cin >> name;
203 
204     int position = isExist(abs, name);
205     if (position == -1)
206     {
207         cout << "查无此人!" << endl;
208     }
209 
210     else
211     {
212         // 1. 
213         string the_name;
214         cout << "输入姓名:" << endl;
215         cin >> the_name;
216         abs->personArray[position].m_Name = the_name;
217 
218         // 2. 
219         int sex;
220         cout << "输入性别:(1---男,2---女)" << endl;
221         while (true)
222         {
223             cin >> sex;
224             if (sex == 1 || sex == 2)
225             {
226                 abs->personArray[position].m_Sex = sex;
227                 break;
228             }
229             cout << "输入错误,重新输入:" << endl;
230 
231         }
232 
233         // 3. 
234         int age;
235         cout << "输入年龄:" << endl;
236         cin >> age;
237         abs->personArray[position].m_Age = age;
238 
239         // 4. 手机号码 
240         string phone;
241         cout << "输入电话号码:" << endl;
242         cin >> phone;
243         abs->personArray[position].m_Phone = phone;
244 
245         // 5. 住址
246         string address;
247         cout << "输入地址: " << endl;
248         cin >> address;
249         abs->personArray[position].m_Addr = address;
250     }
251 
252         cout << "修改成功!" << endl;
253         system("pause");
254         system("cls");
255 
256 
257 }
258 
259 
260 // 6. 清空联系人
261 void cleanPerson(AddressBook* abs)
262 {
263     abs->m_Size = 0;
264     cout << "通讯录清除成功!" << endl;
265     system("pause");
266     system("cls");
267 
268 }
269 
270 
271 int main()
272 {
273     int select = 0;
274     AddressBook abs;
275     abs.m_Size = 0;
276 
277     while (true)
278     {
279         showMenu();
280         cin >> select;
281 
282         switch (select)
283         {
284 
285         // 1. 添加联系人
286         case 1:
287             addPerson(&abs);
288             break;
289 
290         // 2. 显示联系人
291         case 2:
292             showPerson(&abs);
293             break;
294 
295         // 3. 删除联系人
296         case 3:
297         {
298             cout << "输入要删去的联系人:" << endl;
299             string name;
300             cin >> name;
301             deletePerson(&abs, name);
302         }
303             break;
304 
305         // 4. 查找联系人
306         case 4:
307         {
308             string the_name;
309             cout << "输入要查找的联系人:" << endl;
310             cin >> the_name;
311             findPerson(&abs, the_name);
312         }
313             break;
314 
315 
316         case 5:
317             modifyPerson(&abs);
318             break;
319 
320 
321         case 6:
322             cleanPerson(&abs);
323             break;
324 
325 
326         case 0:
327             cout << " "<< endl;
328             system("pause");
329             return 0;
330             break;
331 
332 
333         default:
334             break;
335         }
336     }
337 
338     system("pause");
339     return 0;
340 }

 

posted on 2021-02-01 19:39  Hrunjie  阅读(63)  评论(0编辑  收藏  举报

导航