1、系統(tǒng)需求
通訊錄是一個(gè)可以記錄親人、好友信息的工具。本教程主要利用C++來(lái)實(shí)現(xiàn)一個(gè)通訊錄管理系統(tǒng)系統(tǒng)中需要實(shí)現(xiàn)的功能如下:
- 添加聯(lián)系人:向通訊錄中添加新人,信息包括(姓名、性別、年齡、聯(lián)系電話(huà)、家庭住址)最多記錄1000人
- 顯示聯(lián)系人:顯示通訊錄中所有聯(lián)系人信息
- 刪除聯(lián)系人:按照姓名進(jìn)行刪除指定聯(lián)系人
- 查找聯(lián)系人:按照姓名查看指定聯(lián)系人信息
- 修改聯(lián)系人:按照姓名重新修改指定聯(lián)系人
- 清空聯(lián)系人:清空通訊錄中所有信息
- 退出通訊錄:退出當(dāng)前使用的通訊錄
2、菜單功能
#include <iostream> using namespace std; //菜單界面 void showMenu() { 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; } int main() { //菜單的調(diào)用 showMenu(); system("pause"); return 0; }
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-685256.html
3、退出功能
#include <iostream> using namespace std; //菜單界面 void showMenu() { 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; } int main() { int select = 0;//創(chuàng)建用戶(hù)選擇輸入的變量 while (true) { //菜單的調(diào)用 showMenu(); cin >> select; switch (select) { case 1://1、添加聯(lián)系人 break; case 2://2、顯示聯(lián)系人 break; case 3://3、刪除聯(lián)系人 break; case 4://4、查找聯(lián)系人 break; case 5://5、修改聯(lián)系人 break; case 6://6、清空聯(lián)系人 break; case 0://0、退出通訊錄 cout << "歡迎下次使用" << endl; system("pause"); return 0; break; default: break; } } system("pause"); return 0; }
4、添加聯(lián)系人
#include <iostream> using namespace std; #include <string> #define MAX 1000 //聯(lián)系人的結(jié)構(gòu)體 struct Person { //姓名 string m_Name; //性別 1、男 2、女 int m_Sex; //年齡 int m_Age; //電話(huà) string m_Phone; //住址 string m_Addr; }; //通訊錄的結(jié)構(gòu)體 struct Addressbooks { //通訊錄中保存的聯(lián)系人數(shù)組 struct Person personArray[MAX]; //通訊錄中當(dāng)前記錄聯(lián)系人個(gè)數(shù) int m_Size; }; //添加聯(lián)系人 void addPerson(Addressbooks * abs) { //判斷通訊錄是否已滿(mǎn),滿(mǎn)了不在添加 if (abs->m_Size == MAX) { cout << "通訊錄已滿(mǎn),無(wú)法添加" << endl; return; } else { //添加具體聯(lián)系人 //姓名 string name; cout << "請(qǐng)輸入姓名:" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name; //性別 cout << "請(qǐng)輸入性別:" << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } cout << "輸入有誤,請(qǐng)重新輸入" << endl; } //年齡 cout << "請(qǐng)輸入年齡:" << endl; int age = 0; cin >> age; abs->personArray[abs->m_Size].m_Age = age; //電話(huà) cout << "請(qǐng)輸入電話(huà):" << endl; string Phone; cin >> Phone; abs->personArray[abs->m_Size].m_Phone = Phone; //地址 cout << "請(qǐng)輸入地址:" << endl; string address; cin >> address; abs->personArray[abs->m_Size].m_Addr = address; //更新通訊錄人數(shù) abs->m_Size++; cout << "添加成功" << endl; system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } } //菜單界面 void showMenu() { 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; } int main() { Addressbooks abs; //初始化通訊錄中當(dāng)前人員個(gè)數(shù) abs.m_Size = 0; int select = 0;//創(chuàng)建用戶(hù)選擇輸入的變量 while (true) { //菜單的調(diào)用 showMenu(); cin >> select; switch (select) { case 1://1、添加聯(lián)系人 addPerson(&abs);//利用地址傳遞可以修飾實(shí)參 break; case 2://2、顯示聯(lián)系人 break; case 3://3、刪除聯(lián)系人 break; case 4://4、查找聯(lián)系人 break; case 5://5、修改聯(lián)系人 break; case 6://6、清空聯(lián)系人 break; case 0://0、退出通訊錄 cout << "歡迎下次使用" << endl; system("pause"); return 0; break; default: break; } } system("pause"); return 0; }
5、顯示聯(lián)系人
//2、顯示聯(lián)系人 void showPerson(Addressbooks * abs) { //判斷通訊錄中人數(shù)是否為0 if (abs->m_Size == 0) { cout << "當(dāng)前記錄為空" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "姓名:" << abs->personArray[i].m_Name << " 性別:" << (abs->personArray[i].m_Sex == 1?"男":"女") << " 年齡:" << abs->personArray[i].m_Age << " 電話(huà):" << abs->personArray[i].m_Phone << " 地址:" << abs->personArray[i].m_Addr << endl; } } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 }
6、刪除聯(lián)系人
//檢測(cè)聯(lián)系人是否存在,如果存在,返回聯(lián)系人所在數(shù)組中的具體位置,不存在返回-1 //參數(shù)1 通訊錄 參數(shù)2 對(duì)比姓名 int isExist(Addressbooks* abs,string name) { for (int i = 0; i < abs->m_Size; i++) { //找到用戶(hù)輸入的姓名 if (abs->personArray[i].m_Name == name) { return 1; } } return -1; } //3、刪除指定聯(lián)系人 void deletePerson(Addressbooks* abs) { cout << "請(qǐng)輸入您要?jiǎng)h除的聯(lián)系人" << endl; string name; cin >> name; //ret==-1 未查到 //ret!=-1 查到了 int ret=isExist(abs, name); if (ret != -1) { //查找到人,進(jìn)行刪除 for (int i = ret; i < abs->m_Size; i++) { //數(shù)據(jù)前移 abs->personArray[i] = abs->personArray[i + 1]; } abs->m_Size--;//更新通訊錄中的人員數(shù) cout << "刪除成功" << endl; } else { cout << "查無(wú)此人" << endl; } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 }
7、查找聯(lián)系人
void findPerson(Addressbooks* abs) { cout << "請(qǐng)輸入您要查找的聯(lián)系人" << endl; string name; cin >> name; //判斷指定的聯(lián)系人是否存在 int ret=isExist(abs, name); if (ret != -1) { cout << "姓名:" << abs->personArray[ret].m_Name << "\t"; cout << "性別:" << abs->personArray[ret].m_Sex << "\t"; cout << "年齡:" << abs->personArray[ret].m_Age << "\t"; cout << "電話(huà):" << abs->personArray[ret].m_Phone << "\t"; cout << "地址:" << abs->personArray[ret].m_Addr << endl; } else { cout << "查無(wú)此人" << endl; } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 }
8、修改聯(lián)系人
//5、修改指定聯(lián)系人信息 void modifyPerson(Addressbooks* abs) { cout << "請(qǐng)輸入您要修改的聯(lián)系人" << endl; string name; cin >> name; //判斷指定的聯(lián)系人是否存在 int ret = isExist(abs, name); if (ret != -1) { //姓名 string name; cout << "請(qǐng)輸入姓名:" << endl; cin >> name; abs->personArray[ret].m_Name = name; //性別 int sex=0; cout << "請(qǐng)輸入性別:" << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; break; } cout << "輸入有誤,重新輸入" << endl; } //年齡 int age; cout << "請(qǐng)輸入年齡:" << endl; cin >> age; abs->personArray[ret].m_Age = age; //電話(huà) string Phone; cout << "請(qǐng)輸入聯(lián)系電話(huà):" << endl; cin >> Phone; abs->personArray[ret].m_Phone = Phone; //地址 string address; cout << "請(qǐng)輸入地址:" << endl; cin >> address; abs->personArray[ret].m_Addr = address; cout << "修改成功" << endl; } else { cout << "查無(wú)此人" << endl; } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 }
9、清空通訊錄
//6、清空所有聯(lián)系人 void cleanPerson(Addressbooks* abs) { abs->m_Size = 0; cout << "清空成功" << endl; system("pause");//按任意鍵繼續(xù) system("cls");//清屏 }
10、總結(jié)
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-685256.html
#include <iostream> using namespace std; #include <string> #define MAX 1000 //聯(lián)系人的結(jié)構(gòu)體 struct Person { //姓名 string m_Name; //性別 1、男 2、女 int m_Sex; //年齡 int m_Age; //電話(huà) string m_Phone; //住址 string m_Addr; }; //通訊錄的結(jié)構(gòu)體 struct Addressbooks { //通訊錄中保存的聯(lián)系人數(shù)組 struct Person personArray[MAX]; //通訊錄中當(dāng)前記錄聯(lián)系人個(gè)數(shù) int m_Size; }; //1、添加聯(lián)系人 void addPerson(Addressbooks * abs) { //判斷通訊錄是否已滿(mǎn),滿(mǎn)了不在添加 if (abs->m_Size == MAX) { cout << "通訊錄已滿(mǎn),無(wú)法添加" << endl; return; } else { //添加具體聯(lián)系人 //姓名 string name; cout << "請(qǐng)輸入姓名:" << endl; cin >> name; abs->personArray[abs->m_Size].m_Name = name; //性別 cout << "請(qǐng)輸入性別:" << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; int sex = 0; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[abs->m_Size].m_Sex = sex; break; } cout << "輸入有誤,請(qǐng)重新輸入" << endl; } //年齡 cout << "請(qǐng)輸入年齡:" << endl; int age = 0; cin >> age; abs->personArray[abs->m_Size].m_Age = age; //電話(huà) cout << "請(qǐng)輸入電話(huà):" << endl; string Phone; cin >> Phone; abs->personArray[abs->m_Size].m_Phone = Phone; //地址 cout << "請(qǐng)輸入地址:" << endl; string address; cin >> address; abs->personArray[abs->m_Size].m_Addr = address; //更新通訊錄人數(shù) abs->m_Size++; cout << "添加成功" << endl; system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } } //2、顯示聯(lián)系人 void showPerson(Addressbooks * abs) { //判斷通訊錄中人數(shù)是否為0 if (abs->m_Size == 0) { cout << "當(dāng)前記錄為空" << endl; } else { for (int i = 0; i < abs->m_Size; i++) { cout << "姓名:" << abs->personArray[i].m_Name << " 性別:" << (abs->personArray[i].m_Sex == 1?"男":"女") << " 年齡:" << abs->personArray[i].m_Age << " 電話(huà):" << abs->personArray[i].m_Phone << " 地址:" << abs->personArray[i].m_Addr << endl; } } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } //檢測(cè)聯(lián)系人是否存在,如果存在,返回聯(lián)系人所在數(shù)組中的具體位置,不存在返回-1 //參數(shù)1 通訊錄 參數(shù)2 對(duì)比姓名 int isExist(Addressbooks* abs,string name) { for (int i = 0; i < abs->m_Size; i++) { //找到用戶(hù)輸入的姓名 if (abs->personArray[i].m_Name == name) { return i; } } return -1; } //3、刪除指定聯(lián)系人 void deletePerson(Addressbooks* abs) { cout << "請(qǐng)輸入您要?jiǎng)h除的聯(lián)系人" << endl; string name; cin >> name; //ret==-1 未查到 //ret!=-1 查到了 int ret=isExist(abs, name); if (ret != -1) { //查找到人,進(jìn)行刪除 for (int i = ret; i < abs->m_Size; i++) { //數(shù)據(jù)前移 abs->personArray[i] = abs->personArray[i + 1]; } abs->m_Size--;//更新通訊錄中的人員數(shù) cout << "刪除成功" << endl; } else { cout << "查無(wú)此人" << endl; } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } //4、查找聯(lián)系人 void findPerson(Addressbooks* abs) { cout << "請(qǐng)輸入您要查找的聯(lián)系人" << endl; string name; cin >> name; //判斷指定的聯(lián)系人是否存在 int ret=isExist(abs, name); if (ret != -1) { cout << "姓名:" << abs->personArray[ret].m_Name << "\t"; cout << "性別:" << abs->personArray[ret].m_Sex << "\t"; cout << "年齡:" << abs->personArray[ret].m_Age << "\t"; cout << "電話(huà):" << abs->personArray[ret].m_Phone << "\t"; cout << "地址:" << abs->personArray[ret].m_Addr << endl; } else { cout << "查無(wú)此人" << endl; } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } //5、修改指定聯(lián)系人信息 void modifyPerson(Addressbooks* abs) { cout << "請(qǐng)輸入您要修改的聯(lián)系人" << endl; string name; cin >> name; //判斷指定的聯(lián)系人是否存在 int ret = isExist(abs, name); if (ret != -1) { //姓名 string name; cout << "請(qǐng)輸入姓名:" << endl; cin >> name; abs->personArray[ret].m_Name = name; //性別 int sex=0; cout << "請(qǐng)輸入性別:" << endl; cout << "1 --- 男" << endl; cout << "2 --- 女" << endl; while (true) { cin >> sex; if (sex == 1 || sex == 2) { abs->personArray[ret].m_Sex = sex; break; } cout << "輸入有誤,重新輸入" << endl; } //年齡 int age; cout << "請(qǐng)輸入年齡:" << endl; cin >> age; abs->personArray[ret].m_Age = age; //電話(huà) string Phone; cout << "請(qǐng)輸入聯(lián)系電話(huà):" << endl; cin >> Phone; abs->personArray[ret].m_Phone = Phone; //地址 string address; cout << "請(qǐng)輸入地址:" << endl; cin >> address; abs->personArray[ret].m_Addr = address; cout << "修改成功" << endl; } else { cout << "查無(wú)此人" << endl; } system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } //6、清空所有聯(lián)系人 void cleanPerson(Addressbooks* abs) { abs->m_Size = 0; cout << "清空成功" << endl; system("pause");//按任意鍵繼續(xù) system("cls");//清屏 } //菜單界面 void showMenu() { 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; } int main() { Addressbooks abs; //初始化通訊錄中當(dāng)前人員個(gè)數(shù) abs.m_Size = 0; int select = 0;//創(chuàng)建用戶(hù)選擇輸入的變量 while (true) { //菜單的調(diào)用 showMenu(); cin >> select; switch (select) { case 1://1、添加聯(lián)系人 addPerson(&abs);//利用地址傳遞可以修飾實(shí)參 break; case 2://2、顯示聯(lián)系人 showPerson(&abs); break; case 3://3、刪除聯(lián)系人 //測(cè)試代碼 // //switch中case里語(yǔ)句過(guò)多需要將case中用{}括起來(lái),否則會(huì)報(bào)錯(cuò) //{ // cout << "請(qǐng)輸入刪除聯(lián)系人姓名:" << endl; // string name; // cin >> name; // if (isExist(&abs, name) == -1) { // cout << "查無(wú)此人" << endl; // } // else { // cout << "找到此人" << endl; // } //} deletePerson(&abs); break; case 4://4、查找聯(lián)系人 findPerson(&abs); break; case 5://5、修改聯(lián)系人 modifyPerson(&abs); break; case 6://6、清空聯(lián)系人 cleanPerson(&abs); break; case 0://0、退出通訊錄 cout << "歡迎下次使用" << endl; system("pause"); return 0; break; default: break; } } system("pause"); return 0; }
到了這里,關(guān)于通訊錄管理系統(tǒng)(個(gè)人學(xué)習(xí)筆記黑馬學(xué)習(xí))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!