利用C++實現通訊錄管理系統的完整代碼
通訊錄管理系統
學習目標:
對C++的基礎進行復習,為後續深入學習打好基礎
案例描述:
通訊錄是一個可以記錄親人、好友信息的工具。
本教程主要利用C++來實現一個通訊錄管理系統
系統中需要實現的功能如下:
- 添加聯系人:向通訊錄中添加新人,信息包括(姓名、性別、年齡、聯系電話、傢庭住址)最多記錄1000人
- 顯示聯系人:顯示通訊錄中所有聯系人信息
- 刪除聯系人:按照姓名進行刪除指定聯系人
- 查找聯系人:按照姓名查看指定聯系人信息
- 修改聯系人:按照姓名重新修改指定聯系人
- 清空聯系人:清空通訊錄中所有信息
- 退出通訊錄:退出當前使用的通訊錄
實現代碼:
#include <iostream> #include <string> using namespace std; #define MAX 1000 struct person { string name; int sex {}; int age {}; string phonenumber; string address; }; struct addressbook { struct person personArr[MAX]; 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; } void addPerson(addressbook* aaa) //添加聯系人 { if (aaa->person_size < MAX) { string name; cout << "請輸入姓名:" << endl; cin >> name; aaa->personArr[aaa->person_size].name = name; int sex; cout << "請輸入性別對應序號:(1--男 2--女)" << endl; while (true) { cin >> sex; if ((sex == 1) || (sex == 2)) { aaa->personArr[aaa->person_size].sex = sex; break; } else { cout << "您輸入的有誤,請檢查後重新輸入!" << endl; } } int age = 0; cout << "請輸入年齡:" << endl; cin >> age; aaa->personArr[aaa->person_size].age = age; string phonenumber; cout << "請輸入電話:" << endl; cin >> phonenumber; aaa->personArr[aaa->person_size].phonenumber = phonenumber; string address; cout << "請輸入地址:" << endl; cin >> address; aaa->personArr[aaa->person_size].address = address; aaa->person_size++; cout << "添加聯系人成功!" << endl; } else { cout << "聯系人已滿,請刪除部分聯系人再添加!" << endl; } system("pause"); system("cls"); } void showPerson(addressbook person) { if (person.person_size == 0) { cout << "聯系人列表為空!" << endl; } for (int i = 0; i < person.person_size; i++) { cout << i + 1 << ". " << "姓名:" << person.personArr[i].name << " " << "性別:" << (person.personArr[i].sex == 1 ? "男" : "女") << " " << "年齡:" << person.personArr[i].age << " " << "電話:" << person.personArr[i].phonenumber << " " << "住址:" << person.personArr[i].address << " " << endl; } system("pause"); system("cls"); } int isExist(addressbook* person,string name)//根據姓名判斷是否存在 { for (int i = 0; i < person->person_size; i++) { if (person->personArr[i].name == name) { return i; } } return -1; } void deletePerson( addressbook* person)//刪除聯系人 { string name; cout << "請輸入您要刪除的聯系人姓名!" << endl; cin >> name; int exist = isExist(person, name); if(exist != -1) { for (int i = exist; i < person->person_size; i++) { { person->personArr[i] = person->personArr[i + 1]; } } (person->person_size)--; cout << "刪除成功!" << endl; } else { cout << "沒有這個人!" << endl; } system("pause"); system("cls"); } void findPerson(addressbook* person)//查找聯系人 { string name; cout << "請輸入您要查找的聯系人姓名:" << endl; cin >> name; int exist = isExist(person, name); if (exist != -1) { cout << "該聯系人信息如下:" << endl; cout << "姓名:" << person->personArr[exist].name << " " << "性別:" << (person->personArr[exist].sex == 1 ? "男" : "女") << " " << "年齡:" << person->personArr[exist].age << " " << "電話:" << person->personArr[exist].phonenumber << " " << "住址:" << person->personArr[exist].address << " " << endl; } else { cout << "沒有查到這個人哦!" << endl; } system("pause"); system("cls"); } void modifyPerson(addressbook* person) { string name; cout << "請輸入要修改聯系人的姓名 :" << endl; cin >> name; int exist = isExist(person,name); if (exist != -1) { string modifyName; cout << "請輸入修改後的名字:"; cin >> modifyName; person->personArr[exist].name = modifyName; while (true) { int modifySex; cout << "請輸入修改後的性別(1、男 2、女):"; cin >> modifySex; if (modifySex == 1 || modifySex == 2) { person->personArr[exist].sex = modifySex; break; } else { cout << "您應當輸入1或2,請重新輸入!" << endl; } } int modifyAge; cout << "請輸入修改後的年齡:"; cin >> modifyAge; person->personArr[exist].age = modifyAge; string modifyPhone; cout << "請輸入修改後的電話:"; cin >> modifyPhone; person->personArr[exist].phonenumber = modifyPhone; string modifyAddress; cout << "請輸入修改後的住址:"; cin >> modifyAddress; person->personArr[exist].address = modifyAddress; cout << "修改成功" << endl; } else { cout << "沒有查到這個名字的人,故無法修改" << endl; } system("pause"); system("cls"); } void emptyPerson(addressbook* person) { string ensure; cout << "您確定要清空所有聯系人嗎,此操作不可逆,如需清空,請輸入\"我同意\"這三個字: " << endl; cin >> ensure; if (ensure == "我同意") { person->person_size = 0; cout << "清空聯系人成功" << endl; } else { cout << "撤銷瞭清空聯系人操作!" << endl; } system("pause"); system("cls"); } int main() { int userselect = 0; struct addressbook aaa; aaa.person_size = 0; while (true) { showMenu(); cout << "請在下方輸入您想選擇的功能(輸入前面的數字即可): " << endl; cin >> userselect; switch (userselect) { case 1: addPerson(&aaa); break; case 2: showPerson(aaa); break; case 3: deletePerson(&aaa); break; case 4: findPerson(&aaa); break; case 5: modifyPerson(&aaa); break; case 6: emptyPerson(&aaa); break; case 0: cout << "退出系統成功,歡迎您下次使用!" << endl; system("pause"); return 0; default: cout << "輸入有誤,請重新輸入!" << endl; break; } } }
運行結果:
這個系統裡用到瞭system(“cls”),這個是清屏的意思。
“system("cls")”是在C語言程序中,調用系統命令cls完成清屏操作。
system函數是C語言提供的與操作系統銜接的函數,函數原型如下:
#include <stdlib.h> //所在頭文件 int system(const char *command); //參數為操作系統命令
函數功能:execute a shell command 執行一個操作系統命令
如:
system("time /t") ;顯示時間 system("dir"); //列目錄
示例:
#include<stdlib.h> main() {system("cls");/*清屏*/ system("dirc://");/*列C盤根目錄*/ }
總結
到此這篇關於利用C++實現通訊錄管理系統的文章就介紹到這瞭,更多相關C++通訊錄管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!