您好,登錄后才能下訂單哦!
本篇內容介紹了“C++如何實現通訊錄系統”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
制作一個具有添加聯系人、刪除聯系人、修改聯系人等功能的通訊錄系統
效果圖:
代碼如下:
#include <iostream> using namespace std; #include <string> #define Max 1000 //創建聯系人結構體 struct person { string p_name; int p_sex; //1、男 2、女 int p_age; string p_phone; string p_address; }; //創建通訊錄結構體 struct addressbooks { //保存的聯系人數組 struct person personarr[Max]; //當前記錄的人數 int p_size; }; //菜單 void showmenu() { cout << "********************************" << endl; cout << "***** 1、添加聯系人 *****" << endl; cout << "***** 2、顯示聯系人 *****" << endl; cout << "***** 3、刪除聯系人 *****" << endl; cout << "***** 4、查找聯系人 *****" << endl; cout << "***** 5、修改聯系人 *****" << endl; cout << "***** 6、清空聯系人 *****" << endl; cout << "***** 0、退出通訊錄 *****" << endl; cout << "********************************" << endl; } //添加聯系人 void addperson(addressbooks* abs) { //先判斷通訊錄是否已滿 if (abs->p_size == Max) { cout << "通訊錄已滿" << endl; return; } else { string name; cout << "請輸入姓名: " << endl; cin >> name; abs->personarr[abs->p_size].p_name = name; cout << "請輸入性別: " << endl; cout << "輸入1 -- 男" << endl; cout << "輸入2 -- 女" << endl; int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { //輸入1或者2,退出循環. 輸入其他數字重新循環 abs->personarr[abs->p_size].p_sex = sex; break; } cout << "請重新輸入" << endl; } } //年齡 cout << "請輸入年齡" << endl; int age = 0; cin >> age; abs->personarr[abs->p_size].p_age = age; //電話 cout << "請輸入聯系電話" << endl; string phone; cin >> phone; abs->personarr[abs->p_size].p_phone = phone; //地址 cout << "請輸入地址" << endl; string address; cin >> address; abs->personarr[abs->p_size].p_address = address; //更新通訊錄 abs->p_size++; cout << "添加成功" << endl; system("pause"); system("cls"); } //顯示聯系人 void showperson(addressbooks* abs) { if (abs->p_size == 0) { cout << "當前聯系人為空" << endl; } else { for (int i = 0; i < abs->p_size; i++) { cout << "姓名: " << abs->personarr[i].p_name << "\t"; cout << "性別: " << (abs->personarr[i].p_sex == 1 ? "男" : "女") << "\t"; cout << "年齡: " << abs->personarr[i].p_age << "\t"; cout << "電話: " << abs->personarr[i].p_phone << "\t"; cout << "地址: " << abs->personarr[i].p_address << endl; } } //清屏回到最初菜單 system("pause"); system("cls"); } //檢測聯系人是非存在通訊錄中,如果存在,返回該聯系人在通訊錄中的位置,不存在返回-1 int isExist(addressbooks* abs, string name) { for (int i = 0; i < abs->p_size; i++) { if (abs->personarr[i].p_name == name) { return i; } return -1; //遍歷整個通訊錄沒有,則返回值為-1 } } //刪除聯系人 void deleteperson(addressbooks* abs) { cout << "請輸入要刪除的聯系人姓名" << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { for (int i = ret; i < abs->p_size; i++) { //后面的數據全部往前覆蓋一格 abs->personarr[i] = abs->personarr[i + 1]; } abs->p_size--; cout << "刪除成功" << endl; } else { cout << "通訊錄中不存在該聯系人" << endl; } system("pause"); system("cls"); } //查找指定聯系人 void findperson(addressbooks* abs) { cout << "請輸入您要查找的聯系人姓名: " << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { cout << "姓名: " << abs->personarr[ret].p_name << "\t"; cout << "性別: " << abs->personarr[ret].p_sex << "\t"; cout << "年齡: " << abs->personarr[ret].p_age << "\t"; cout << "電話 " << abs->personarr[ret].p_phone << "\t"; cout << "地址: " << abs->personarr[ret].p_address << endl; } else { cout << "通訊錄中不存在該聯系人" << endl; } system("pause"); system("cls"); } //修改聯系人 void modifyperson(addressbooks* abs) { cout << "請輸入您要查找的聯系人姓名: " << endl; string name; cin >> name; int ret = isExist(abs, name); if (ret != -1) { string name; cout << "請輸入新的姓名: " << endl; cin >> name; abs->personarr[ret].p_name = name; int sex = 0; cout << "請輸入性別: 1--男 2--女" << endl; cout << "請輸入性別: " << endl; cout << "輸入1 -- 男" << endl; cout << "輸入2 -- 女" << endl; while (true) { cin >> sex; if (sex == 1 || sex == 2) { //輸入1或者2,退出循環. 輸入其他數字重新循環 abs->personarr[abs->p_size].p_sex = sex; break; } cout << "請重新輸入" << endl; } int age = 0; cout << "請輸入年齡: " << endl; cin >> age; abs->personarr[ret].p_age = age; string phone; cout << "請輸入新的電話: " << endl; cin >> phone; abs->personarr[ret].p_phone = phone; string address; cout << "請輸入新的地址: " << endl; cin >> address; abs->personarr[ret].p_address = address; cout << "修改成功!" << endl; } else { cout << "通訊錄中無該聯系人" << endl; } system("pause"); system("cls"); } //清空聯系人 void emptyperson(addressbooks* abs) { cout << "確認此操作?" << endl; cout << "輸入: 1--確認" << endl; cout << "輸入: 其他--否" << endl; int select2 = 0; cin >> select2; if (select2 == 1) { //將該通訊錄中的聯系人數量清零 abs->p_size = 0; cout << " 已清空 " << endl; } system("pause"); system("cls"); } int main() { addressbooks abs; abs.p_size = 0; int select = 0; while (true) { showmenu(); cin >> select; //選擇模式 switch (select) { case 1: //1、添加聯系人 addperson(&abs); //利用地址傳遞修飾實參 break; case 2: //2、顯示聯系人 showperson(&abs); break; case 3: //3、刪除聯系人 { deleteperson(&abs); break; } case 4: //4、查找聯系人 findperson(&abs); break; case 5: //5、修改聯系人 modifyperson(&abs); break; case 6: //6、清空聯系人 emptyperson(&abs); break; case 0: //0、退出通訊錄 cout << "歡迎下次使用" << endl; system("pause"); return 0; break; default: break; } } system("pause"); return 0; }
“C++如何實現通訊錄系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。