C++實現通訊錄系統項目實戰
本文實例為大傢分享瞭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; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。