C++實(shí)現(xiàn)簡(jiǎn)易通訊錄管理系統(tǒng)
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡(jiǎn)易通訊錄管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
前言
建議收藏,親手寫一遍代碼,感受指針神奇的魅力;
可以幫助你更好的鞏固知識(shí)體系,熟悉指針,結(jié)構(gòu)體與函數(shù)一起使用時(shí)的妙處
完成通訊錄管理系統(tǒng)所需知識(shí)體系
1.結(jié)構(gòu)體
2.指針
3.函數(shù)的封裝
4.指針與函數(shù)的結(jié)合使用
5.指針與結(jié)構(gòu)體的結(jié)合使用
結(jié)構(gòu)體
聯(lián)系人結(jié)構(gòu)體
struct person { ?? ?string name;//姓名 ?? ?string sex; //性別 ?? ?int age; ?//年齡 ?? ?string phone;//手機(jī)號(hào) ?? ?string home;//地址 };
通訊錄結(jié)構(gòu)體
struct addressbook { ?? ?struct person personArray[MAX]; //通訊錄擴(kuò)展到100; ?? ?int size=0; ?//當(dāng)前聯(lián)系人個(gè)數(shù)(后面就相當(dāng)于i++) };
函數(shù)模塊
void menu();//菜單 void putit(addressbook* add);//添加聯(lián)系人 void showperson(addressbook* add);// 顯示聯(lián)系人 int if_include(addressbook* add, string name);//判斷聯(lián)系人 void deleteperson(addressbook* add, int i);//刪除聯(lián)系人 void findPerson( addressbook* add);//查找聯(lián)系人 void cleanperson(struct addressbook* add);//清空所有聯(lián)系人
菜單
void menu() { ?? ?cout << endl; ?? ?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; ?? ?cout << "*******************************" << endl; }
添加聯(lián)系人
void putit(addressbook* add)//添加聯(lián)系人功能 { ?? ?if (add->size == MAX) ?? ??? ?cout << "通訊錄已滿" << endl; ?? ?else ?? ?{ ?? ??? ?string name; ?? ??? ?string sex; ?? ??? ?int age; ?? ??? ?string phone; ?? ??? ?string home; ?? ??? ?cout << "請(qǐng)輸入姓名" << endl; ?? ??? ?cin >> name; ?? ??? ?add->personArray[add->size].name = name; ?? ??? ?cout << "請(qǐng)輸入姓別" << endl; ?? ??? ?cin >> sex; ?? ??? ?add->personArray[add->size].sex = sex; ?? ??? ?cout << "請(qǐng)輸入年齡" << endl; ?? ??? ?cin >> age; ?? ??? ?add->personArray[add->size].age = age; ?? ??? ?cout << "請(qǐng)輸入電話號(hào)碼" << endl; ?? ??? ?cin >> phone; ?? ??? ?add->personArray[add->size].phone = phone; ?? ??? ?cout << "請(qǐng)輸入家庭住址" << endl; ?? ??? ?cin >> home; ?? ??? ?add->personArray[add->size].home = home; ?? ??? ?add->size++; ?? ??? ?cout << "添加聯(lián)系人成功" << endl; ?? ??? ? ?? ?} ?? ?system("pause"); ?? ?system("cls"); ?? ?menu(); }
有人問添加聯(lián)系人函數(shù)中為什么要使用指針?
因?yàn)橹祩鬟f中形參無法改變實(shí)參;
而利用指針的地址傳遞可以通過函數(shù)中的形參改變實(shí)參;
具體原理可以參考博主之前的指針基礎(chǔ)和指針進(jìn)階內(nèi)容]
顯示聯(lián)系人
void showperson(addressbook* add) { ?? ?for (int i = 0; i <add->size; i++) ?? ?{ ?? ??? ?cout << "姓名: ?" << add->personArray[i].name; ?? ??? ?cout << "\t姓別: ?" << add->personArray[i].sex; ?? ??? ?cout << "\t年齡: ?" << add->personArray[i].age; ?? ??? ?cout << "\t電話號(hào)碼: ?" << add->personArray[i].phone; ?? ??? ?cout << "\t家庭住址: ?" << add->personArray[i].home << endl; ?? ?} ?? ?system("pause"); ?? ?system("cls"); ?? ?menu(); }
判斷聯(lián)系人
int if_include(addressbook* add, string name) { ?? ?for (int i = 0; i < add->size; i++) ?? ?{ ?? ??? ?if (name == add->personArray[i].name) ?? ??? ?{ ?? ??? ??? ?return i; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return -1; ?? ??? ?} ?? ?} }
刪除聯(lián)系人
void deleteperson(addressbook* add, int i) { ?? ?for (; i < add->size; i++) ?? ?{ ?? ??? ?add->personArray[i] = add->personArray[i + 1]; ?? ?} ?? ?system("pause"); ?? ?system("cls"); }
查找聯(lián)系人
void findPerson( addressbook* add) { ?? ?cout << "請(qǐng)輸入您想要查找的聯(lián)系人:" << endl; ?? ?string name; ?? ?cin >> name; ?? ?int ret = if_include(add, name); ?? ?if (ret == -1) ?? ?{ ?? ??? ?cout << "查無此人" << endl; ?? ?} ?? ?else ?? ?{ ? //查到此人,進(jìn)行顯示操作 ?? ??? ?int i = ret; ?? ??? ?cout << "姓名:" << add->personArray[i].name << "\t"; ?? ??? ?cout << "性別:" << add->personArray[i].sex << "\t"; ?? ??? ?cout << "年齡:" << add->personArray[i].age << "\t"; ?? ??? ?cout << "聯(lián)系方式:" << add->personArray[i].phone << "\t"; ?? ??? ?cout << "地址:" << add->personArray[i].home << endl; ?? ?} ?? ?//按任意鍵清屏 ?? ?system("pause"); ?? ?system("cls"); }
清空所有聯(lián)系人
void cleanperson(struct addressbook* add)//清空所有聯(lián)系人 { ?? ?cout << "是否確認(rèn)清空?" << endl; ?? ?cout << "1 --- 是" << endl; ?? ?cout << "2 --- 否" << endl; ?? ?int a; ?? ?cin >> a; ?? ?if (a == 1) ?? ?{ ?? ??? ?add->size = 0;//將當(dāng)前記錄聯(lián)系人數(shù)量置為0,做邏輯上的清空操作 ?? ??? ?cout << "通訊錄已清空" << endl; ?? ?} ?? ?system("pause"); ?? ?system("cls"); }
main函數(shù)
int main() { ?? ?menu(); ?? ?addressbook add;//定義一個(gè)通訊錄 ?? ?int choice=1; ?? ?while (choice != 0) ?? ?{ ?? ??? ?cin >> choice; ?? ??? ?switch (choice)//選擇 ?? ??? ?{ ?? ??? ?case 1: putit(&add); ?? ??? ??? ?break; ?? ??? ?case 2: showperson(&add); ?? ??? ??? ?break; ?? ??? ?case 3: {cout << "請(qǐng)輸入你要?jiǎng)h除的人的名字" << endl; ?? ??? ??? ?string aname; ?? ??? ??? ?cin >> aname; ?? ??? ??? ?if (if_include(&add, aname) == -1) ?? ??? ??? ?{ ?? ??? ??? ??? ?cout << "查無此人" << endl; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?if (if_include(&add, aname)) ?? ??? ??? ?{ ?? ??? ??? ??? ?deleteperson(&add, if_include(&add, aname)); ?? ??? ??? ?} ?? ??? ?} ?? ??? ??? ?break;? ?? ??? ?case 4: ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?break; ?? ??? ?case 0: cout << "歡迎下次使用" << endl; ?? ??? ??? ?return 0; ?? ??? ??? ?break; ?? ??? ?default: {cout << "輸入不合法,請(qǐng)重新輸入" << endl; ?? ??? ??? ?break; } ?? ??? ?} ?? ?} }
源代碼
#include<iostream> using namespace std; #include<string> #define MAX 100 struct person { ?? ?string name;//姓名 ?? ?string sex; //性別 ?? ?int age; ?//年齡 ?? ?string phone;//手機(jī)號(hào) ?? ?string home;//地址 }; struct addressbook { ?? ?struct person personArray[MAX]; //通訊錄擴(kuò)展到100; ?? ?int size=0; ?//當(dāng)前聯(lián)系人個(gè)數(shù)(后面就相當(dāng)于i++) }; void menu();//菜單 void putit(addressbook* add); void showperson(addressbook* add);? int if_include(addressbook* add, string name); void deleteperson(addressbook* add, int i); void findPerson(struct addressbooks* add);//查找聯(lián)系人 void cleanperson(struct addressbook* add);//清空所有聯(lián)系人 int main() { ?? ?menu(); ?? ?addressbook add; ?? ?int choice=1; ?? ?while (choice != 0) ?? ?{ ?? ??? ?cin >> choice; ?? ??? ?switch (choice) ?? ??? ?{ ?? ??? ?case 1: putit(&add); ?? ??? ??? ?break; ?? ??? ?case 2: showperson(&add); ?? ??? ??? ?break; ?? ??? ?case 3: { ?? ??? ??? ?cout << "請(qǐng)輸入你要?jiǎng)h除的人的名字" << endl; ?? ??? ??? ?string aname; ?? ??? ??? ?cin >> aname; ?? ??? ??? ?if (if_include(&add, aname) == -1) ?? ??? ??? ?{ ?? ??? ??? ??? ?cout << "查無此人" << endl; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?if (if_include(&add, aname)) ?? ??? ??? ?{ ?? ??? ??? ??? ?deleteperson(&add, if_include(&add, aname)); ?? ??? ??? ?} ?? ??? ?} ?? ??? ??? ?break;? ?? ??? ?case 4: ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?break; ?? ??? ?case 6: ?? ??? ??? ?break; ?? ??? ?case 0: cout << "歡迎下次使用" << endl; ?? ??? ??? ?return 0; ?? ??? ??? ?break; ?? ??? ?default: {cout << "輸入不合法,請(qǐng)重新輸入" << endl; ?? ??? ??? ?break; } ?? ??? ?} ?? ?} } void menu() { ?? ?cout << endl; ?? ?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; ?? ?cout << "********************************" << endl; } void putit(addressbook* add)//添加聯(lián)系人功能 { ?? ?if (add->size == MAX) ?? ??? ?cout << "通訊錄已滿" << endl; ?? ?else ?? ?{ ?? ??? ?string name; ?? ??? ?string sex; ?? ??? ?int age; ?? ??? ?string phone; ?? ??? ?string home; ?? ??? ?cout << "請(qǐng)輸入姓名" << endl; ?? ??? ?cin >> name; ?? ??? ?add->personArray[add->size].name = name; ?? ??? ?cout << "請(qǐng)輸入姓別" << endl; ?? ??? ?cin >> sex; ?? ??? ?add->personArray[add->size].sex = sex; ?? ??? ?cout << "請(qǐng)輸入年齡" << endl; ?? ??? ?cin >> age; ?? ??? ?add->personArray[add->size].age = age; ?? ??? ?cout << "請(qǐng)輸入電話號(hào)碼" << endl; ?? ??? ?cin >> phone; ?? ??? ?add->personArray[add->size].phone = phone; ?? ??? ?cout << "請(qǐng)輸入家庭住址" << endl; ?? ??? ?cin >> home; ?? ??? ?add->personArray[add->size].home = home; ?? ??? ?add->size++; ?? ??? ?cout << "添加聯(lián)系人成功" << endl; ?? ??? ? ?? ?} ?? ?system("pause"); ?? ?system("cls"); ?? ?menu(); } void showperson(addressbook* add) { ?? ?for (int i = 0; i <add->size; i++) ?? ?{ ?? ??? ?cout << "姓名: ?" << add->personArray[i].name; ?? ??? ?cout << "\t姓別: ?" << add->personArray[i].sex; ?? ??? ?cout << "\t年齡: ?" << add->personArray[i].age; ?? ??? ?cout << "\t電話號(hào)碼: ?" << add->personArray[i].phone; ?? ??? ?cout << "\t家庭住址: ?" << add->personArray[i].home << endl; ?? ?} ?? ?system("pause"); ?? ?system("cls"); ?? ?menu(); } int if_include(addressbook* add, string name) { ?? ?for (int i = 0; i < add->size; i++) ?? ?{ ?? ??? ?if (name == add->personArray[i].name) ?? ??? ?{ ?? ??? ??? ?return i; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?return -1; ?? ??? ?} ?? ?} } void deleteperson(addressbook* add, int i) { ?? ?for (; i < add->size; i++) ?? ?{ ?? ??? ?add->personArray[i] = add->personArray[i + 1]; ?? ?} ?? ?system("pause"); ?? ?system("cls"); } void findPerson( addressbook* add) { ?? ?cout << "請(qǐng)輸入您想要查找的聯(lián)系人:" << endl; ?? ?string name; ?? ?cin >> name; ?? ?int ret = if_include(add, name); ?? ?if (ret == -1) ?? ?{ ?? ??? ?cout << "查無此人" << endl; ?? ?} ?? ?else ?? ?{ ? //查到此人,進(jìn)行顯示操作 ?? ??? ?int i = ret; ?? ??? ?cout << "姓名:" << add->personArray[i].name << "\t"; ?? ??? ?cout << "性別:" << add->personArray[i].sex << "\t"; ?? ??? ?cout << "年齡:" << add->personArray[i].age << "\t"; ?? ??? ?cout << "聯(lián)系方式:" << add->personArray[i].phone << "\t"; ?? ??? ?cout << "地址:" << add->personArray[i].home << endl; ?? ?} ?? ?//按任意鍵清屏 ?? ?system("pause"); ?? ?system("cls"); } void cleanperson(struct addressbook* add)//清空所有聯(lián)系人 { ?? ?cout << "是否確認(rèn)清空?" << endl; ?? ?cout << "1 --- 是" << endl; ?? ?cout << "2 --- 否" << endl; ?? ?int a; ?? ?cin >> a; ?? ?if (a == 1) ?? ?{ ?? ??? ?add->size = 0;//將當(dāng)前記錄聯(lián)系人數(shù)量置為0,做邏輯上的清空操作 ?? ??? ?cout << "通訊錄已清空" << endl; ?? ?} ?? ?system("pause"); ?? ?system("cls"); }
由于代碼量過大,建議大家可以試著分文件編寫代碼,也可以方便查看
運(yùn)行結(jié)果
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 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)完整功能的通訊錄管理系統(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++編程中將引用類型作為函數(shù)參數(shù)的方法指南
這篇文章主要介紹了C++編程中將引用類型作為函數(shù)參數(shù)的方法指南,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09隊(duì)列的動(dòng)態(tài)鏈?zhǔn)酱鎯?chǔ)實(shí)現(xiàn)代碼分享
DynaLnkQueue.cpp - 動(dòng)態(tài)鏈?zhǔn)疥?duì)列,即隊(duì)列的動(dòng)態(tài)鏈?zhǔn)酱鎯?chǔ)實(shí)現(xiàn)2014-02-02Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01C++實(shí)現(xiàn)英文句子中的單詞逆序輸出的方法
這篇文章主要介紹了C++實(shí)現(xiàn)英文句子中的單詞逆序輸出的方法,涉及C++字符串遍歷、分割、截取、輸出等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01