您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++如何實(shí)現(xiàn)簡(jiǎn)易通訊錄管理系統(tǒng)”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“C++如何實(shí)現(xiàn)簡(jiǎn)易通訊錄管理系統(tǒng)”文章能幫助大家解決問題。
建議收藏,親手寫一遍代碼,感受指針神奇的魅力;
可以幫助你更好的鞏固知識(shí)體系,熟悉指針,結(jié)構(gòu)體與函數(shù)一起使用時(shí)的妙處
完成通訊錄管理系統(tǒng)所需知識(shí)體系
1.結(jié)構(gòu)體
2.指針
3.函數(shù)的封裝
4.指針與函數(shù)的結(jié)合使用
5.指針與結(jié)構(gòu)體的結(jié)合使用
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);//添加聯(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; }
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)容]
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"); }
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"); }
由于代碼量過大,建議大家可以試著分文件編寫代碼,也可以方便查看
關(guān)于“C++如何實(shí)現(xiàn)簡(jiǎn)易通訊錄管理系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。