C++實(shí)現(xiàn)完整功能的通訊錄管理系統(tǒng)詳解
一、確定結(jié)構(gòu)體
通訊錄里應(yīng)該存有聯(lián)系人的信息,包括姓名、性別、電話、地址等等,通訊錄也應(yīng)該有長度,存的聯(lián)系人要有上限。所以我們這樣確定結(jié)構(gòu)體:
#define Max 1000
struct person
{
string m_Name;
string m_Sex;//規(guī)定 1 為男 2為女
int m_age;
string m_phone;
string m_Address;
};
struct addressBooks
{
struct person personArray[Max];//通訊錄中保存的聯(lián)系人數(shù)組
int m_size = 0;//通訊錄中人員個(gè)數(shù)
};結(jié)構(gòu)體 addressBooks 中定義聯(lián)系人數(shù)組最大為1000,同時(shí)初始化人聯(lián)系人為0。還有一點(diǎn)值得注意,被嵌套的結(jié)構(gòu)體person 需要在 addressBooks前創(chuàng)建,避免出現(xiàn)未定義的情況。
二、簡(jiǎn)易菜單
要做通訊錄管理系統(tǒng),就要首先確定系統(tǒng)的功能。所以我確定了通訊錄的增、刪、改、查、顯示、清空和退出 七個(gè)功能,代碼上簡(jiǎn)單編寫一個(gè)無返回值(void)的函數(shù)即可。
void showMenu()//菜單功能
{
cout << "\t*************************" << endl;
cout << "\t***** 1、添加聯(lián)系人 *****" << endl;
cout << "\t***** 2、顯示聯(lián)系人 *****" << endl;
cout << "\t***** 3、查找聯(lián)系人 *****" << endl;
cout << "\t***** 4、修改聯(lián)系人 *****" << endl;
cout << "\t***** 5、刪除聯(lián)系人 *****" << endl;
cout << "\t***** 6、清空通訊錄 *****" << endl;
cout << "\t***** 0、退出通訊錄 *****" << endl;
cout << "\t*************************" << endl;
}tips:這里“\t”會(huì)讓光標(biāo)向后跳8個(gè)字符的位置,可以理解為將菜單“居中”顯示,稍微美觀一點(diǎn)。
三、為通訊錄添加功能
代碼展示:
//主函數(shù)完整代碼
int main()
{
//創(chuàng)建通訊錄結(jié)構(gòu)體變量
addressBooks abc;
while (1) {
showMenu();
int select = 0;
cout << "請(qǐng)選擇你的操作:";
cin >> select;
switch (select)
{
case 1://添加聯(lián)系人
addPerson(&abc);
break;
case 2://顯示聯(lián)系人
showPerson(&abc);
break;
case 3://查找聯(lián)系人
{
findPerson(&abc);
}
break;
case 4://修改聯(lián)系人
modifyPerson(&abc);
break;
case 5://刪除聯(lián)系人
{
deletePerson(&abc);
}//case 語句 里的代碼多的話就用{}括起來,不報(bào)錯(cuò)
break;
case 6://清空通訊錄
clearPerson(&abc);
break;
case 0://退出通訊錄
cout << "歡迎下次使用,祝您生活愉快" << endl; return 0; break;
default:
cout << "請(qǐng)合理輸入操作數(shù)0~6:" << endl;
cin >> select;
break;
}
}
}首先創(chuàng)建通訊錄結(jié)構(gòu)體變量,while(1) 是個(gè)死循環(huán),除非我們選擇“0”功能,運(yùn)行return 0 語句,否則不會(huì)結(jié)束循環(huán),這也是我們可以重復(fù)選擇功能的根本原因。流程控制語句switch case 語句無需多講,只要注意每段語句結(jié)束加上break就行了。然后注意這段代碼都是采用地址傳遞的方法,其實(shí)我更喜歡引用傳遞,不過是當(dāng)時(shí)不太了解引用傳遞,關(guān)于函數(shù)傳參的區(qū)別可以參考博主的這篇文章詳解函數(shù)傳參的三種方式
四、各功能與實(shí)現(xiàn)詳解
功能之添加聯(lián)系人
void addPerson(addressBooks* abc)//添加聯(lián)系人
{
if (abc->m_size >= Max)
{
cout << "通訊錄已滿,添加失敗" << endl;
}
else {
string name; cout << "添加聯(lián)系人名字:" << endl; cin >> name;
abc->personArray[abc->m_size].m_Name = name;
int sex = 0; cout << "聯(lián)系人性別為:" << endl;
cout << "1---男\(zhòng)n" << "2---女\n"; cin >> sex;
while (1) {
if (sex == 1 || sex == 2)
{
if (sex == 1) abc->personArray[abc->m_size].m_Sex = "男";
else abc->personArray[abc->m_size].m_Sex = "女";
break;
}
else cout << "輸入有誤,請(qǐng)?jiān)跀?shù)字 1和2中選擇" << endl; cin >> sex;
}
int age = 0; cout << "聯(lián)系人年齡為:" << endl; cin >> age;
abc->personArray[abc->m_size].m_age = age;
string phoneNumber = "0"; cout << "聯(lián)系人電話:" << endl; cin >> phoneNumber;
abc->personArray[abc->m_size].m_phone = phoneNumber;
string address = "0"; cout << "聯(lián)系人地址為:" << endl; cin >> address;
abc->personArray[abc->m_size].m_Address = address; cout << "添加成功" << endl;
//更新通訊錄人數(shù)
abc->m_size++;
system("pause"); system("cls");
}
}代碼詳解
首先判斷通訊錄當(dāng)前聯(lián)系人數(shù)量,如果大于最大值停止添加聯(lián)系人;然后作一個(gè)輸入流來輸入聯(lián)系人信息,利用聯(lián)系人數(shù)組填入聯(lián)系人信息;姓名屬性我們簡(jiǎn)單做了一個(gè)范圍選擇,只允許輸入1和0并給出提示1代表男性;倒數(shù)第二行 abc->m_size++,更新聯(lián)系人數(shù)量;最后有兩個(gè)系統(tǒng)函數(shù) system("pause") 和 system("cls"),分別時(shí)“按任意鍵繼續(xù)...”和清空屏幕函數(shù),讓我們的通訊錄更加穩(wěn)定和美觀。
功能之顯示聯(lián)系人
void showPerson(addressBooks* abc)
{
if (abc->m_size == 0) cout << "當(dāng)前記錄為空" << endl;
else {
for (int i = 0; i < abc->m_size; i++) {
cout << "聯(lián)系人:\t" << abc->personArray[i].m_Name << "\t性別: "
<< abc->personArray[i].m_Sex<< "\t年齡: " <<
abc->personArray[i].m_age << "\t電話: " << abc->personArray[i].m_phone
<< "\t地址: " << abc->personArray[i].m_Address << endl;
}
}
system("pause"); system("cls");
}代碼詳解
首先判斷有無聯(lián)系人,沒有就輸出“當(dāng)前記錄為空”,然后利用一重for 循環(huán)輸出所有聯(lián)系人的信息,最后也是加上暫停函數(shù)和清空屏幕函數(shù)使運(yùn)行界面美觀。
功能之查找聯(lián)系人
判定函數(shù)和實(shí)現(xiàn)查找
int isExit(addressBooks* abc, string name)//判定通訊錄是否有此人
{
for (int i = 0; i < abc->m_size; i++)
{
if (abc->personArray[i].m_Name == name) return i;
}
return -1;
}
void findPerson(addressBooks* abc)
{
cout << "輸入要查找聯(lián)系人的名字:" << endl;
string name = "0"; cin >> name;
int i = isExit(abc, name);
if (i != -1)
{
cout << "聯(lián)系人:\t" << abc->personArray[i].m_Name << "\t性別: "
<< abc->personArray[i].m_Sex << "\t年齡: " <<
abc->personArray[i].m_age << "\t電話: "
<< abc->personArray[i].m_phone << "\t地址: "
<< abc->personArray[i].m_Address << endl;
}
else {
cout << "查無此人" << endl;
}
system("pause"); system("cls");
}代碼詳解
isExit()函數(shù)用來返回查找聯(lián)系人在數(shù)組中的下標(biāo),如果沒有該名字就返回 -1;結(jié)合findPerson() 函數(shù),如果找到該聯(lián)系人則輸出所有信息,找不到則輸出“查無此人”,最后也是老套路,使用暫停和清空屏幕函數(shù)。
功能之修改聯(lián)系人
void modifyPerson(addressBooks* abc)
{
cout << "輸入要修改的聯(lián)系人名字" << endl;
string name = "0"; cin >> name;
int v = isExit(abc, name);
if (v != -1)
{
string name; cout << "更改后的名字為:" << endl; cin >> name;
abc->personArray[v].m_Name = name;
int sex = 0; cout << "更改后性別為:" << endl;
cout << "1---男\(zhòng)n" << "2---女\n"; cin >> sex;
while (1) {
if (sex == 1 || sex == 2)
{
if (sex == 1) abc->personArray[v].m_Sex = "男";
else abc->personArray[v].m_Sex = "女";
break;
}
else cout << "輸入有誤,請(qǐng)?jiān)跀?shù)字 1和2中選擇" << endl; cin >> sex;
}
int age = 0; cout << "更改后年齡為:" << endl; cin >> age;
abc->personArray[v].m_age = age;
string phoneNumber = "0"; cout << "更改后電話號(hào)碼:" << endl; cin >> phoneNumber;
abc->personArray[v].m_phone = phoneNumber;
string address = "0"; cout << "更改后地址為:" << endl; cin >> address;
abc->personArray[v].m_Address = address; cout << "更改成功" << endl;
}
else {
cout << "查無此人" << endl;
}
system("pause"); system("cls");
}代碼詳解
這個(gè)功能函數(shù)其實(shí)很好理解,無非就是判定函數(shù)和添加聯(lián)系人功能的結(jié)合,這里就不做詳細(xì)介紹了。
功能之刪除聯(lián)系人
void deletePerson(addressBooks* abc)
{
if (abc->m_size == 0)
{
cout << "當(dāng)前記錄為空" << endl;
}
else {
cout << "輸入要?jiǎng)h除的聯(lián)系人:" << endl;
string name = "0"; cin >> name;
int v = isExit(abc, name);
if (v == -1) {
cout << "查無此人" << endl;
}
else if (v != -1) {
for (int i = v; i < abc->m_size; i++)
{
abc->personArray[i] = abc->personArray[i + 1];
}
abc->m_size--;
cout << "刪除成功" << endl;
}
}
system("pause"); system("cls");
}代碼詳解
首先判斷有無聯(lián)系人,沒有就輸出“當(dāng)前記錄為空”,再利用判斷函數(shù)判斷是否有此人,如果存在,那我們也是利用一重循環(huán),讓 i 等于返回的數(shù)組下標(biāo),條件是 i < abc->m_size,注意最好不要寫"<=",因?yàn)槲覀冃枰獙?shù)組中的元素前移,只需要abc->m_size - 1 個(gè)長度就行了。最后將整個(gè)聯(lián)系人數(shù)量減一,完成刪除操作。
功能之清空通訊錄
void clearPerson(addressBooks* abc)
{
abc->m_size = 0;
cout << "通訊錄已清空" << endl;
system("pause"); system("cls");
}將聯(lián)系人數(shù)組置為零,即可清空通訊錄。
四、源碼
//Txl.h
#include<iostream>
using namespace std;
#define Max 1000
struct person
{
string m_Name;
string m_Sex;//規(guī)定 1 為男 2為女
int m_age;
string m_phone;
string m_Address;
};
struct addressBooks
{
struct person personArray[Max];//通訊錄中保存的聯(lián)系人數(shù)組
int m_size = 0;//通訊錄中人員個(gè)數(shù)
};
void addPerson(addressBooks* abc); //添加聯(lián)系人
void showPerson(addressBooks* abc); //顯示聯(lián)系人
int isExit(addressBooks* abc, string name); //遍歷通訊錄
void deletePerson(addressBooks* abc); //刪除聯(lián)系人
void findPerson(addressBooks* abc); //查找聯(lián)系人
void modifyPerson(addressBooks* abc); //修改聯(lián)系人
void clearPerson(addressBooks* abc); //清空通訊錄
//Txl.cpp
#include"Txl.h"
void addPerson(addressBooks* abc)//添加聯(lián)系人
{
if (abc->m_size >= Max)
{
cout << "通訊錄已滿,添加失敗" << endl;
}
else {
string name; cout << "添加聯(lián)系人名字:" << endl; cin >> name;
abc->personArray[abc->m_size].m_Name = name;
int sex = 0; cout << "聯(lián)系人性別為:" << endl;
cout << "1---男\(zhòng)n" << "2---女\n"; cin >> sex;
while (1) {
if (sex == 1 || sex == 2)
{
if (sex == 1) abc->personArray[abc->m_size].m_Sex = "男";
else abc->personArray[abc->m_size].m_Sex = "女";
break;
}
else cout << "輸入有誤,請(qǐng)?jiān)跀?shù)字 1和2中選擇" << endl; cin >> sex;
}
int age = 0; cout << "聯(lián)系人年齡為:" << endl; cin >> age;
abc->personArray[abc->m_size].m_age = age;
string phoneNumber = "0"; cout << "聯(lián)系人電話:" << endl; cin >> phoneNumber;
abc->personArray[abc->m_size].m_phone = phoneNumber;
string address = "0"; cout << "聯(lián)系人地址為:" << endl; cin >> address;
abc->personArray[abc->m_size].m_Address = address; cout << "添加成功" << endl;
//更新通訊錄人數(shù)
abc->m_size++;
system("pause"); system("cls");
}
}
void showPerson(addressBooks* abc)
{
if (abc->m_size == 0) cout << "當(dāng)前記錄為空" << endl;
else {
for (int i = 0; i < abc->m_size; i++) {
cout << "聯(lián)系人:\t" << abc->personArray[i].m_Name << "\t性別: "
<< abc->personArray[i].m_Sex<< "\t年齡: " <<
abc->personArray[i].m_age << "\t電話: " << abc->personArray[i].m_phone
<< "\t地址: " << abc->personArray[i].m_Address << endl;
}
}
system("pause"); system("cls");
}
int isExit(addressBooks* abc, string name)//判定通訊錄是否有此人
{
for (int i = 0; i < abc->m_size; i++)
{
if (abc->personArray[i].m_Name == name) return i;
}
return -1;
}
void findPerson(addressBooks* abc)
{
cout << "輸入要查找聯(lián)系人的名字:" << endl;
string name = "0"; cin >> name;
int i = isExit(abc, name);
if (i != -1)
{
cout << "聯(lián)系人:\t" << abc->personArray[i].m_Name << "\t性別: "
<< abc->personArray[i].m_Sex << "\t年齡: " <<
abc->personArray[i].m_age << "\t電話: "
<< abc->personArray[i].m_phone << "\t地址: "
<< abc->personArray[i].m_Address << endl;
}
else {
cout << "查無此人" << endl;
}
system("pause"); system("cls");
}
void deletePerson(addressBooks* abc)
{
if (abc->m_size == 0)
{
cout << "當(dāng)前記錄為空" << endl;
}
else {
cout << "輸入要?jiǎng)h除的聯(lián)系人:" << endl;
string name = "0"; cin >> name;
int v = isExit(abc, name);
if (v == -1) {
cout << "查無此人" << endl;
}
else if (v != -1) {
for (int i = v; i < abc->m_size; i++)
{
abc->personArray[i] = abc->personArray[i + 1];
}
abc->m_size--;
cout << "刪除成功" << endl;
}
}
system("pause"); system("cls");
}
void modifyPerson(addressBooks* abc)
{
cout << "輸入要修改的聯(lián)系人名字" << endl;
string name = "0"; cin >> name;
int v = isExit(abc, name);
if (v != -1)
{
string name; cout << "更改后的名字為:" << endl; cin >> name;
abc->personArray[v].m_Name = name;
int sex = 0; cout << "更改后性別為:" << endl;
cout << "1---男\(zhòng)n" << "2---女\n"; cin >> sex;
while (1) {
if (sex == 1 || sex == 2)
{
if (sex == 1) abc->personArray[v].m_Sex = "男";
else abc->personArray[v].m_Sex = "女";
break;
}
else cout << "輸入有誤,請(qǐng)?jiān)跀?shù)字 1和2中選擇" << endl; cin >> sex;
}
int age = 0; cout << "更改后年齡為:" << endl; cin >> age;
abc->personArray[v].m_age = age;
string phoneNumber = "0"; cout << "更改后電話號(hào)碼:" << endl; cin >> phoneNumber;
abc->personArray[v].m_phone = phoneNumber;
string address = "0"; cout << "更改后地址為:" << endl; cin >> address;
abc->personArray[v].m_Address = address; cout << "更改成功" << endl;
}
else {
cout << "查無此人" << endl;
}
system("pause"); system("cls");
}
void clearPerson(addressBooks* abc)
{
abc->m_size = 0;
cout << "通訊錄已清空" << endl;
system("pause"); system("cls");
}
//函數(shù)聲明,可加可不加,這里加上為了更直觀的表現(xiàn)出來
void addPerson(addressBooks* abc); //添加聯(lián)系人
void showPerson(addressBooks* abc); //顯示聯(lián)系人
int isExit(addressBooks* abc, string name); //遍歷通訊錄
void deletePerson(addressBooks* abc); //刪除聯(lián)系人
void findPerson(addressBooks* abc); //查找聯(lián)系人
void modifyPerson(addressBooks* abc); //修改聯(lián)系人
void clearPerson(addressBooks* abc); //清空通訊錄五、運(yùn)行效果
運(yùn)行效果圖


還有更多功能就不展示了,你們可以復(fù)制源碼后自己操作,挺有趣的喔
生成可執(zhí)行程序


我們?cè)诰幾g器的上面選擇Release,并啟動(dòng)調(diào)試,然后這個(gè)項(xiàng)目對(duì)應(yīng)的文件夾會(huì)自動(dòng)生成Release文件夾,點(diǎn)開就是第二個(gè)圖的內(nèi)容,然后雙擊就可以執(zhí)行了,實(shí)測(cè)分享給朋友也可以用哦
結(jié)語
到此這篇關(guān)于C++實(shí)現(xiàn)完整功能的通訊錄管理系統(tǒng)詳解的文章就介紹到這了,更多相關(guān)C++通訊錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實(shí)現(xiàn)簡(jiǎn)易通訊錄管理系統(tǒng)
- C++實(shí)現(xiàn)簡(jiǎn)單版通訊錄管理系統(tǒng)
- C++實(shí)現(xiàn)簡(jiǎn)單的通訊錄管理系統(tǒng)
- 利用C++實(shí)現(xiàn)通訊錄管理系統(tǒng)的完整代碼
- C++實(shí)現(xiàn)簡(jiǎn)單通訊錄管理系統(tǒng)
- C++ 實(shí)現(xiàn)的通訊錄管理系統(tǒng)詳解
- C++鏈表實(shí)現(xiàn)通訊錄管理系統(tǒng)
- C++實(shí)現(xiàn)通訊錄管理系統(tǒng)
- C++實(shí)現(xiàn)簡(jiǎn)易的通訊錄管理系統(tǒng)
相關(guān)文章
C#將Unicode編碼轉(zhuǎn)換為漢字字符串的簡(jiǎn)單方法
下面小編就為大家?guī)硪黄狢#將Unicode編碼轉(zhuǎn)換為漢字字符串的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
c++多線程之死鎖的發(fā)生的情況解析(包含兩個(gè)歸納,6個(gè)示例)
這篇文章主要介紹了c++多線程之死鎖的發(fā)生的情況解析(包含兩個(gè)歸納,6個(gè)示例),需要的朋友可以參考下2018-01-01
c++中的消息框messagebox()詳細(xì)介紹及使用方法
本文將介紹下c++中的messagebox()的使用方法:常用屬性/按鈕的形式/返回值等等,感興趣的朋友可以了解下,希望本文可以幫助到你2013-02-02
C++詳細(xì)講解內(nèi)存管理工具primitives
文章向大家介紹C++內(nèi)存管理primitives,主要包括primitives使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下2022-06-06
C++算法計(jì)時(shí)器的實(shí)現(xiàn)示例
本文主要介紹了C++算法計(jì)時(shí)器的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

