C++實現(xiàn)通訊錄系統(tǒng)項目實戰(zhàn)
更新時間:2022年06月20日 14:24:40 作者:青衫哥
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)通訊錄系統(tǒng)項目實戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++實現(xiàn)通訊錄系統(tǒng)項目的具體代碼,供大家參考,具體內(nèi)容如下
制作一個具有添加聯(lián)系人、刪除聯(lián)系人、修改聯(lián)系人等功能的通訊錄系統(tǒng)
效果圖:
代碼如下:
#include <iostream> using namespace std; #include <string> #define Max 1000 //創(chuàng)建聯(lián)系人結(jié)構(gòu)體 struct person { ? ? string p_name; ? ? int p_sex; ? ? ? //1、男 ?2、女 ? ? int p_age; ? ? string p_phone; ? ? string p_address; }; //創(chuàng)建通訊錄結(jié)構(gòu)體 struct addressbooks { ? ? //保存的聯(lián)系人數(shù)組 ? ? struct person personarr[Max]; ? ? //當(dāng)前記錄的人數(shù) ? ? int p_size; }; //菜單 void showmenu() { ? ? cout << "********************************" << endl; ? ? cout << "***** 1、添加聯(lián)系人 *****" << endl; ? ? cout << "***** 2、顯示聯(lián)系人 *****" << endl; ? ? cout << "***** 3、刪除聯(lián)系人 *****" << endl; ? ? cout << "***** 4、查找聯(lián)系人 *****" << endl; ? ? cout << "***** 5、修改聯(lián)系人 *****" << endl; ? ? cout << "***** 6、清空聯(lián)系人 *****" << endl; ? ? cout << "***** 0、退出通訊錄 *****" << endl; ? ? cout << "********************************" << endl; } ? //添加聯(lián)系人 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,退出循環(huán). ?輸入其他數(shù)字重新循環(huán) ? ? ? ? ? ? ? ? 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 << "請輸入聯(lián)系電話" << 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"); } ? //顯示聯(lián)系人 void showperson(addressbooks* abs) { ? ? if (abs->p_size == 0) ? ? { ? ? ? ? cout << "當(dāng)前聯(lián)系人為空" << 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"); } ? //檢測聯(lián)系人是非存在通訊錄中,如果存在,返回該聯(lián)系人在通訊錄中的位置,不存在返回-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 ? ? } } ? //刪除聯(lián)系人 void deleteperson(addressbooks* abs) { ? ? cout << "請輸入要刪除的聯(lián)系人姓名" << endl; ? ? string name; ? ? cin >> name; ? ? int ret = isExist(abs, name); ? ? if (ret != -1) ? ? { ? ? ? ? for (int i = ret; i < abs->p_size; i++) ? ? ? ? { ? ? ? ? ? ? //后面的數(shù)據(jù)全部往前覆蓋一格 ? ? ? ? ? ? abs->personarr[i] = abs->personarr[i + 1]; ? ? ? ? } ? ? ? ? abs->p_size--; ? ? ? ? cout << "刪除成功" << endl; ? ? } ? ? else ? ? { ? ? ? ? cout << "通訊錄中不存在該聯(lián)系人" << endl; ? ? } ? ? system("pause"); ? ? system("cls"); } ? //查找指定聯(lián)系人 void findperson(addressbooks* abs) { ? ? cout << "請輸入您要查找的聯(lián)系人姓名: " << 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 << "通訊錄中不存在該聯(lián)系人" << endl; ? ? } ? ? ? system("pause"); ? ? system("cls"); } ? //修改聯(lián)系人 void modifyperson(addressbooks* abs) { ? ? cout << "請輸入您要查找的聯(lián)系人姓名: " << 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,退出循環(huán). ?輸入其他數(shù)字重新循環(huán) ? ? ? ? ? ? ? ? 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 << "通訊錄中無該聯(lián)系人" << endl; ? ? } ? ? ? system("pause"); ? ? system("cls"); } ? //清空聯(lián)系人 void emptyperson(addressbooks* abs) { ? ? cout << "確認(rèn)此操作?" << endl; ? ? cout << "輸入: 1--確認(rèn)" << endl; ? ? cout << "輸入: 其他--否" << endl; ? ? int select2 = 0; ? ? cin >> select2; ? ? if (select2 == 1) ? ? { ? ? ? ? //將該通訊錄中的聯(lián)系人數(shù)量清零 ? ? ? ? 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、添加聯(lián)系人 ? ? ? ? ? ? addperson(&abs); ? ? ? //利用地址傳遞修飾實參 ? ? ? ? ? ? break; ? ? ? ? case 2: ? ?//2、顯示聯(lián)系人 ? ? ? ? ? ? showperson(&abs); ? ? ? ? ? ? break; ? ? ? ? case 3: ? ?//3、刪除聯(lián)系人 ? ? ? ? { ? ? ? ? ? ? deleteperson(&abs); ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? case 4: ? ?//4、查找聯(lián)系人 ? ? ? ? ? ? findperson(&abs); ? ? ? ? ? ? break; ? ? ? ? case 5: ? ?//5、修改聯(lián)系人 ? ? ? ? ? ? modifyperson(&abs); ? ? ? ? ? ? break; ? ? ? ? case 6: ? ?//6、清空聯(lián)系人 ? ? ? ? ? ? emptyperson(&abs); ? ? ? ? ? ? break; ? ? ? ? case 0: ? ?//0、退出通訊錄 ? ? ? ? ? ? cout << "歡迎下次使用" << endl; ? ? ? ? ? ? system("pause"); ? ? ? ? ? ? return 0; ? ? ? ? ? ? break; ? ? ? ? default: ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? ? system("pause"); ? ? return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實現(xiàn)LeetCode(161.一個編輯距離)
這篇文章主要介紹了C++實現(xiàn)LeetCode(161.一個編輯距離),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07