利用C++實現(xiàn)通訊錄管理系統(tǒng)的完整代碼
通訊錄管理系統(tǒng)
學(xué)習(xí)目標:
對C++的基礎(chǔ)進行復(fù)習(xí),為后續(xù)深入學(xué)習(xí)打好基礎(chǔ)
案例描述:
通訊錄是一個可以記錄親人、好友信息的工具。
本教程主要利用C++來實現(xiàn)一個通訊錄管理系統(tǒng)
系統(tǒng)中需要實現(xiàn)的功能如下:
- 添加聯(lián)系人:向通訊錄中添加新人,信息包括(姓名、性別、年齡、聯(lián)系電話、家庭住址)最多記錄1000人
- 顯示聯(lián)系人:顯示通訊錄中所有聯(lián)系人信息
- 刪除聯(lián)系人:按照姓名進行刪除指定聯(lián)系人
- 查找聯(lián)系人:按照姓名查看指定聯(lián)系人信息
- 修改聯(lián)系人:按照姓名重新修改指定聯(lián)系人
- 清空聯(lián)系人:清空通訊錄中所有信息
- 退出通訊錄:退出當(dāng)前使用的通訊錄
實現(xiàn)代碼:
#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、添加聯(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; } void addPerson(addressbook* aaa) //添加聯(lián)系人 { if (aaa->person_size < MAX) { string name; cout << "請輸入姓名:" << endl; cin >> name; aaa->personArr[aaa->person_size].name = name; int sex; cout << "請輸入性別對應(yīng)序號:(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 << "添加聯(lián)系人成功!" << endl; } else { cout << "聯(lián)系人已滿,請刪除部分聯(lián)系人再添加!" << endl; } system("pause"); system("cls"); } void showPerson(addressbook person) { if (person.person_size == 0) { cout << "聯(lián)系人列表為空!" << 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)//根據(jù)姓名判斷是否存在 { for (int i = 0; i < person->person_size; i++) { if (person->personArr[i].name == name) { return i; } } return -1; } void deletePerson( addressbook* person)//刪除聯(lián)系人 { string name; cout << "請輸入您要刪除的聯(lián)系人姓名!" << 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)//查找聯(lián)系人 { string name; cout << "請輸入您要查找的聯(lián)系人姓名:" << endl; cin >> name; int exist = isExist(person, name); if (exist != -1) { cout << "該聯(lián)系人信息如下:" << 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 << "請輸入要修改聯(lián)系人的姓名 :" << 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 << "您應(yīng)當(dāng)輸入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 << "您確定要清空所有聯(lián)系人嗎,此操作不可逆,如需清空,請輸入\"我同意\"這三個字: " << endl; cin >> ensure; if (ensure == "我同意") { person->person_size = 0; cout << "清空聯(lián)系人成功" << endl; } else { cout << "撤銷了清空聯(lián)系人操作!" << endl; } system("pause"); system("cls"); } int main() { int userselect = 0; struct addressbook aaa; aaa.person_size = 0; while (true) { showMenu(); cout << "請在下方輸入您想選擇的功能(輸入前面的數(shù)字即可): " << 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 << "退出系統(tǒng)成功,歡迎您下次使用!" << endl; system("pause"); return 0; default: cout << "輸入有誤,請重新輸入!" << endl; break; } } }
運行結(jié)果:
這個系統(tǒng)里用到了system(“cls”),這個是清屏的意思。
“system("cls")”是在C語言程序中,調(diào)用系統(tǒng)命令cls完成清屏操作。
system函數(shù)是C語言提供的與操作系統(tǒng)銜接的函數(shù),函數(shù)原型如下:
#include <stdlib.h> //所在頭文件 int system(const char *command); //參數(shù)為操作系統(tǒng)命令
函數(shù)功能:execute a shell command 執(zhí)行一個操作系統(tǒng)命令
如:
system("time /t") ;顯示時間 system("dir"); //列目錄
示例:
#include<stdlib.h> main() {system("cls");/*清屏*/ system("dirc://");/*列C盤根目錄*/ }
總結(jié)
到此這篇關(guān)于利用C++實現(xiàn)通訊錄管理系統(tǒng)的文章就介紹到這了,更多相關(guān)C++通訊錄管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式
這篇文章主要介紹了C++?vector與數(shù)組轉(zhuǎn)換寫入/讀出文件方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11