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


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


我們在編譯器的上面選擇Release,并啟動調試,然后這個項目對應的文件夾會自動生成Release文件夾,點開就是第二個圖的內容,然后雙擊就可以執(zhí)行了,實測分享給朋友也可以用哦
結語
到此這篇關于C++實現完整功能的通訊錄管理系統(tǒng)詳解的文章就介紹到這了,更多相關C++通訊錄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

