C++實現通訊錄管理系統項目
本文實例為大傢分享瞭C++實現通訊錄管理系統的具體代碼,供大傢參考,具體內容如下
1、通訊錄設計要點
1:添加聯系人:向通訊錄中添加新人(包括:性別,年齡,聯系電話,傢庭住址),並且最多記錄1000人
2:顯示聯系人:顯示通訊錄中所有聯系人信息
3:刪除聯系人:按照姓名進行刪除指定聯系人
4:查找聯系人:按照姓名查找聯系人
5:修改聯系人:按照姓名修改聯系人
6:清空聯系人:按照姓名清空聯系人
7:退出通訊錄:退出當前使用的通訊錄
2、設計思路
/** 本教程主要利用C++來實現一個通訊管理系統,系統中需要實現如下功能: 1:添加聯系人:向通訊錄中添加新人(包括:性別,年齡,聯系電話,傢庭住址),並且最多記錄1000人 2:顯示聯系人:顯示通訊錄中所有聯系人信息 3:刪除聯系人:按照姓名進行刪除指定聯系人 4:查找聯系人:按照姓名查找聯系人 5:修改聯系人:按照姓名修改聯系人 6:清空聯系人:按照姓名清空聯系人 7:退出通訊錄:退出當前使用的通訊錄 */ // 引入C++標準包 #include <iostream> #include <string> // #define MAX_NUMBER 2 using namespace std; // const int MAX_NUMBER2 = 3; // 定義常量通訊錄最大值 (auto 讓編譯其自己推斷變量的類型) constexpr auto MAX = 3; // 定義聯系人結構體 struct Person { string name; int sex; int age; string phoneNamer; string address; }; struct addressbook { struct Person perArray[MAX]; // struct Person personArr[MAX_NUMBER2]; // struct Person personArr[MAX_NUMBER]; int person_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; } int isExist(addressbook* personBook , string name) { for (int i = 0; i < personBook->person_size;i++) { if (personBook->perArray[i].name == name) { // 從通訊錄中找到瞭某個人 return i; } } return -1; } void addPerson(addressbook *addBook) { if (addBook->person_size < MAX) { cout << "請輸入姓名:" << endl; string name; cin >> name; // addBook 操作指針指向的哪個對象 addBook->perArray[addBook->person_size].name = name; cout << "請輸入性別對應的序號:1--男 2---女" << endl; int sex; // 通過while死循環持續性的讀取用戶輸入的性別 while(true) { cin >> sex; if ((sex ==1) || (sex ==2)) { addBook->perArray[addBook->person_size].sex = sex; break; } else { cout << "您輸入的信息有誤,請重新出入" << endl; } } int age = 0; cout << "請輸入年齡" << endl; cin >> age; addBook->perArray[addBook->person_size].age = age; string namuber; cout << "請輸入電話號碼:" << endl; cin >> namuber; addBook->perArray[addBook->person_size].phoneNamer = namuber; string address; cout << "請輸入地址" << endl; cin >> address; addBook->perArray[addBook->person_size].address = address; // 聯系人添加成功 addBook->person_size++; cout << "聯系人已成功添加到通訊錄" << endl; } else { cout << "通訊錄聯系人已滿,請刪除部分聯系人再添加!" << endl; } system("pause"); system("cls"); } // 顯示聯系人 void showPerson(addressbook person) { if (person.person_size == 0) { cout << "您的通訊錄列表為空" << endl; } else { for (int i = 0; i < person.person_size; i++) { cout << "序號:" << i + 1 << ": " << "姓名: " << person.perArray[i].name << ": " << "性別: " << person.perArray[i].sex << ": " << "年齡: " << person.perArray[i].age << ": " << "電話: " << person.perArray[i].phoneNamer << ": " << "住址: " << person.perArray[i].address << " " << endl; } } system("pause"); system("cls"); } // 刪除聯系人 void deletePerson(addressbook* person) { string name; cout << "請輸入您要刪除的聯系人姓名:" << endl; cin >> name; int isExis = isExist(person, name); if (isExis != -1) { for (int i = isExis; i < person->person_size; i++) { person->perArray[i] = person->perArray[i + 1]; } person->person_size--; cout << "刪除成功" << endl; } else { cout << "對不起,通訊錄沒有此人" << endl; } system("pause"); system("cls"); } // 查找聯系人 void findPerson(addressbook* address) { string name; cout << "請輸入您想要查找的聯系人" << endl; cin >> name; int exist = isExist(address, name); if (exist != -1) { cout << "該聯系人信息如下:" << endl; cout << "姓名: " << address->perArray[exist].name << " " << "性別: " << address->perArray[exist].sex << " " << "年齡: " << address->perArray[exist].age << " " << "電話: " << address->perArray[exist].phoneNamer << " " << "住址: " << address->perArray[exist].address << " " << endl; } else { cout << "查無此人喔!" << endl; } system("pause"); system("cls"); } void modifyPerson(addressbook *person) { string modifyName; cout << "請輸入修改後的姓名: " << endl; cin >> modifyName; int exist = isExist(person, modifyName); if (exist != -1) { person->perArray[exist].name = modifyName; while (true) { int modifySex; cout << "請輸入修改後的性別: (1:男 2:女) " << endl; cin >> modifySex; if (modifySex == 1 || modifySex ==2) { person->perArray[exist].sex = modifySex; break; } else { cout << "您應當輸入1或者2,請重新輸入" << endl; } } int modifyAge; cout << "請輸入修改後的年齡: "; cin >> modifyAge; person->perArray[exist].age = modifyAge; string modifyPhoneNum; cout << "請輸入修改後的電話: "; cin >> modifyPhoneNum; person->perArray[exist].phoneNamer = modifyPhoneNum; string modifyAddress; cout << "請輸入修改後的地址: "; cin >> modifyAddress; person->perArray[exist].address = modifyAddress; cout << "修改成功!" << endl; } else { cout << "查無此人,故無法修改" << endl; } system("pause"); system("cls"); } // 清空通訊錄 void clearPersonAddress(addressbook *personBook) { string ensure; cout << "您確定要是清空所有聯系人信息嗎?註意此操作不可逆,請謹慎操作,請輸入\"我同意\" " << endl; cin >> ensure; if (ensure == "我同意") { personBook->person_size = 0; for (int i = 0; i < personBook->person_size; i++) { personBook->perArray[i].address = ""; personBook->perArray[i].name = ""; personBook->perArray[i].phoneNamer = ""; personBook->perArray[i].age =0; personBook->perArray[i].sex =0; } cout << "已成功清空通訊錄列表" << endl; } else { cout << "撤銷清空聯系人列表" << endl; } system("pause"); system("cls"); } int main() { std::cout << "通許錄管理系統 \n"; struct addressbook address; address.person_size = 0; int userSelect = -1; while (true) { showMenu(); cout << "請在下方輸入您向選擇的功能(輸入下面數字即可)" << endl; cin >> userSelect; switch (userSelect) { case 1: addPerson(&address); break; case 2: showPerson(address); break; case 3: deletePerson(&address); break; case 4: findPerson(&address); break; case 5: modifyPerson(&address); break; case 6: clearPersonAddress(&address); break; case 0: cout << "退出系統成功,歡迎您下次使用!" << endl; return 0; default: system("pause"); break; } } }
3、運行結果
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。