c++—通訊錄管理系統

cqu發表於2022-01-02

一、運用所學的結構體、地址指標等基礎知識,完成通訊錄管理系統

二、系統主要有以下6個功能:

1.新增聯絡人
2.顯示聯絡人 
3.刪除聯絡人 
4.查詢聯絡人
5.修改聯絡人 
6.清空聯絡人 

  1 #include<iostream>
  2 using namespace std;
  3 #include<string>
  4 #define MAX 1000
  5 
  6 
  7 //選單介面
  8 void showMenu()
  9 {
 10     cout << "**************************" << endl;
 11     cout << "*****  1.新增聯絡人  *****" << endl;
 12     cout << "*****  2.顯示聯絡人  *****" << endl;
 13     cout << "*****  3.刪除聯絡人  *****" << endl;
 14     cout << "*****  4.查詢聯絡人  *****" << endl;
 15     cout << "*****  5.修改聯絡人  *****" << endl;
 16     cout << "*****  6.清空聯絡人  *****" << endl;
 17     cout << "*****  0.退出通訊錄  *****" << endl;
 18     cout << "**************************" << endl;
 19 }
 20 
 21 //聯絡人結構體
 22 struct Person
 23 {
 24     string m_Name;//姓名
 25     int m_sex;//性別1男,2女
 26     int m_age;//年齡
 27     string m_phone;//電話
 28     string m_Addr; //住址
 29 };
 30 //通訊錄結構體
 31 struct Addressbooks 
 32 {
 33     //聯絡人陣列
 34     struct Person personarray[MAX];
 35     //聯絡人個數
 36     int m_size;
 37 
 38 };
 39 //1新增聯絡人
 40 void addPerson(Addressbooks* abs)
 41 {
 42     //判斷通訊錄是否滿了
 43     if (abs->m_size==MAX)
 44     {
 45         cout << "通訊錄已滿,無法新增!" << endl;
 46         return;
 47     }
 48     else
 49     {
 50         //新增聯絡人
 51 
 52         //姓名
 53         string name="";
 54         cout << "請輸入姓名:  " << endl;
 55         cin >> name;
 56         abs->personarray[abs->m_size].m_Name = name;
 57         
 58         //性別
 59         int sex=0;
 60         cout << "請輸入性別:  " << endl;
 61         cout << "1 --- 男" << endl;
 62         cout << "2 --- 女" << endl;
 63         while (true)
 64         {
 65             cin >> sex;
 66             if (sex == 1 || sex == 2)
 67             {
 68                 abs->personarray[abs->m_size].m_sex = sex;
 69                 break;
 70             }
 71             cout << "輸入有誤,請重新輸入!" << endl;
 72         }
 73 
 74         //年齡
 75         cout << "請輸入年齡:  " << endl;
 76         int age = 0;
 77         while (true)
 78         {
 79             cin >> age;
 80             if (age<101)
 81             {
 82                 abs->personarray[abs->m_size].m_age = age;
 83                 break;
 84             } 
 85             cout << "輸入有誤,請重新輸入!" << endl;
 86             
 87         }
 88         
 89         //電話
 90         cout << "請輸入電話:  " << endl;
 91         string phone="";
 92         cin >> phone;
 93         abs->personarray[abs->m_size].m_phone = phone;
 94 
 95         //住址
 96         cout << "請輸入家庭住址:  " << endl;
 97         string place="";
 98         cin >> place;
 99         abs->personarray[abs->m_size].m_Addr = place;
100         
101         //更新通訊錄人數
102         abs->m_size++;
103         cout << "新增成功" << endl;
104 
105         system("pause");//請按任意鍵繼續
106         system("cls");//清屏操作
107     }
108 }
109 
110 //2.顯示聯絡人
111 void showperson(Addressbooks* abs) 
112 {
113     if (abs->m_size == 0)
114     {
115         cout << "當前記錄為空" << endl;
116     }
117     else
118     {
119         for (int i = 0; i < abs->m_size; i++)
120         {
121             cout << "姓名:  " << abs->personarray[i].m_Name << "\t";
122             cout << "性別:  " << (abs->personarray[i].m_sex == 1 ? "":"") << "\t";
123             cout << "年齡:  " << abs->personarray[i].m_age << "\t";
124             cout << "電話:  " << abs->personarray[i].m_phone << "\t";
125             cout << "住址:  " << abs->personarray[i].m_Addr << endl;
126         }
127     }
128     system("pause");
129     system("cls");
130 }
131 
132 //3.1檢測聯絡人是否存在
133 int Exist(Addressbooks* abs,string name)//通訊錄,對比姓名 
134 {
135     for (int i = 0; i < abs->m_size; i++)
136     {
137         if (abs->personarray[i].m_Name == name)
138         {
139             return i;
140         }
141     }
142     return -1;
143 }
144 //3.2刪除聯絡人
145 void Delteperson(Addressbooks* abs)
146 {
147     cout << "請輸入您要刪除的聯絡人" << endl;
148 
149     string name;
150     cin >> name;
151 
152     int ret = Exist(abs, name);
153 
154     if (ret!=-1)
155     {
156         for (int i = ret; i < abs->m_size; i++)
157         {
158             //資料前移
159             abs->personarray[i] = abs->personarray[i + 1];
160         }
161         abs->m_size--;//更新人員數量
162         cout << "刪除成功" << endl;
163     }
164     else
165     {
166         cout << "查無此人!" << endl;
167     }
168     system("pause");
169     system("cls");
170 }
171 
172 //4.查詢聯絡人
173 void findperson(Addressbooks* abs) 
174 {
175     cout << "請輸入需要查詢的聯絡人" << endl;
176     string name;
177     cin >> name;
178 
179     //判斷是否存在
180     int ret = Exist(abs, name);
181 
182     if (ret!=-1)
183     {
184         int i = ret;
185         cout << "姓名:  " << abs->personarray[i].m_Name << "\t";
186         cout << "性別:  " << (abs->personarray[i].m_sex == 1 ? "" : "") << "\t";
187         cout << "年齡:  " << abs->personarray[i].m_age << "\t";
188         cout << "電話:  " << abs->personarray[i].m_phone << "\t";
189         cout << "住址:  " << abs->personarray[i].m_Addr << endl;
190     }
191     else
192     {
193         cout << "查無此人!" << endl;
194     }
195     system("pause");
196     system("cls");
197 
198 }
199 
200 //5.修改聯絡人
201 void modifyperson(Addressbooks* abs)
202 {
203     cout << "請輸入需要修改的聯絡人" << endl;
204     string name;
205     cin >> name;
206 
207     //判斷是否存在
208     int ret = Exist(abs, name);
209 
210     if (ret != -1)
211     {
212         int i= ret;
213         //顯示要修改人資訊
214         cout << "---------------------------------------------" << endl;
215         cout << "姓名:  " << abs->personarray[i].m_Name << "\t";
216         cout << "性別:  " << (abs->personarray[i].m_sex == 1 ? "" : "") << "\t";
217         cout << "年齡:  " << abs->personarray[i].m_age << "\t";
218         cout << "電話:  " << abs->personarray[i].m_phone << "\t";
219         cout << "住址:  " << abs->personarray[i].m_Addr << endl;
220         
221         //根據不同需求修改聯絡人資訊
222         int n = 0;
223         cout << "---------------------------------------------" << endl;
224         cout << "           請選擇需要修改的內容選項:        " << endl;
225         cout << "---------------------------------------------" << endl;
226         cout << "1--姓名;2--性別;3--年齡;4--電話;5--住址;6--全部修改;7--退出" << endl;
227         
228         while (n!=7)
229         {
230             cin >> n;
231             string me = "";
232             int sex=0;
233             int age = 0;
234             string phone = "";
235             string place = "";
236 
237             switch (n)
238             {
239             case 1:
240                 //姓名
241                 cout << "請輸入姓名:  " << endl;
242                 cin >> me;
243                 abs->personarray[i].m_Name = me;
244                 cout << "修改成功" << endl;
245                 cout << "如要修改請繼續選擇其他選項,退出請按7" << endl;
246                 break;
247             case 2:
248                 //性別
249                 cout << "請輸入性別:  " << endl;
250                 cout << "1 --- 男" << endl;
251                 cout << "2 --- 女" << endl;
252                 
253                 while (true)
254                 {
255                     cin >> sex;
256                     if (sex == 1 || sex == 2)
257                     {
258                         abs->personarray[i].m_sex = sex;
259                         break;
260                     }
261                     cout << "輸入有誤,請重新輸入!" << endl;
262                 }
263                 cout << "修改成功" << endl;
264                 cout << "如要修改請繼續選擇其他選項,退出請按7" << endl;
265                 break;
266 
267             case 3:
268                 //年齡
269                 cout << "請輸入年齡:  " << endl;
270                 
271                 cin >> age;
272                 abs->personarray[i].m_age = age;
273                 cout << "修改成功" << endl;
274                 cout << "如要修改請繼續選擇其他選項,退出請按7" << endl;
275                 break;
276 
277             case 4:
278                 //電話
279                 cout << "請輸入電話:  " << endl;
280                 
281                 cin >> phone;
282                 abs->personarray[i].m_phone = phone;
283                 cout << "修改成功" << endl;
284                 cout << "如要修改請繼續選擇其他選項,退出請按7" << endl;
285                 break;
286 
287             case 5:
288                 //住址
289                 cout << "請輸入家庭住址:  " << endl;
290 
291                 cin >> place;
292                 abs->personarray[i].m_Addr = place;
293                 cout << "修改成功" << endl;
294                 cout << "如要修改請繼續選擇其他選項,退出請按7" << endl;
295                 break;
296                 
297 
298             case 6:
299                 //姓名
300                 cout << "請輸入姓名:  " << endl;
301                 cin >> name;
302                 abs->personarray[i].m_Name=name;
303                 
304                 //性別
305                 cout << "請輸入性別:  " << endl;
306                 cout << "1 --- 男" << endl;
307                 cout << "2 --- 女" << endl;
308 
309                 while (true)
310                 {
311                     cin >> sex;
312                     if (sex == 1 || sex == 2)
313                     {
314                         abs->personarray[i].m_sex = sex;
315                         break;
316                     }
317                     cout << "輸入有誤,請重新輸入!" << endl;
318                 }
319 
320                 //年齡
321                 cout << "請輸入年齡:  " << endl;
322                 cin >> age;
323                 abs->personarray[i].m_age = age;
324 
325                 //電話
326                 cout << "請輸入電話:  " << endl;
327                 cin >> phone;
328                 abs->personarray[i].m_phone = phone;
329 
330                 //住址
331                 cout << "請輸入家庭住址:  " << endl;
332                 cin >> place;
333                 abs->personarray[i].m_Addr = place;
334                 cout << "修改成功" << endl;
335                 n = 7;
336                 break;
337 
338             default:
339                 break;
340             }
341         }
342 
343     }
344     else
345     {
346         cout << "查無此人!" << endl;
347     }
348     system("pause");
349     system("cls");
350 
351 }
352 //6、清空所有聯絡人
353 void cleanperson(Addressbooks* abs)
354 {
355     cout << "請問是否真的要清空通訊錄? " << endl;
356     cout << "1--確定" << endl;
357     cout << "0--不清空" << endl;
358     int b;
359     cin >> b;
360     while (b==1)
361     {
362         abs->m_size = 0;
363         cout << "通訊錄已清空" << endl;
364         break;
365     }
366     system("pause");
367     system("cls");
368 }
369 
370 int main()
371 {
372     //建立通訊錄結構體變數
373     Addressbooks abs;
374     //當前人員個數
375     abs.m_size = 0;
376     
377     int select = 0;//建立使用者選擇輸入變數
378 
379     while (true) //除了選0,能一直使用
380     {
381         //選單呼叫
382         showMenu();
383 
384         cin >> select;
385         switch (select)
386         {
387         case 1:        //1.新增聯絡人
388             addPerson(&abs); //地址傳遞
389             break;
390         case 2:        //2.顯示聯絡人
391             showperson(&abs);
392             break;
393         case 3:        //3.刪除聯絡人
394             Delteperson(&abs);
395             break;
396         case 4:        //4.查詢聯絡人
397             findperson(&abs);
398             break;
399         case 5:        //5.修改聯絡人 
400             modifyperson(&abs);
401             break;
402         case 6:        //6.清空聯絡人 
403             cleanperson(&abs);
404             break;
405         case 0:        //0.退出通訊錄  
406             cout << "歡迎下次使用" << endl;
407             system("pause");
408             return 0;
409             break;
410         default:
411             break;
412         }
413     }
414 
415     
416 
417     system("pause");
418     return 0;
419 }

 


0.退出通訊錄

相關文章