您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++如何實現簡易通訊錄管理系統”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“C++如何實現簡易通訊錄管理系統”文章能幫助大家解決問題。
建議收藏,親手寫一遍代碼,感受指針神奇的魅力;
可以幫助你更好的鞏固知識體系,熟悉指針,結構體與函數一起使用時的妙處
完成通訊錄管理系統所需知識體系
1.結構體
2.指針
3.函數的封裝
4.指針與函數的結合使用
5.指針與結構體的結合使用
struct person { string name;//姓名 string sex; //性別 int age; //年齡 string phone;//手機號 string home;//地址 };
struct addressbook { struct person personArray[MAX]; //通訊錄擴展到100; int size=0; //當前聯系人個數(后面就相當于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( addressbook* add);//查找聯系人 void cleanperson(struct addressbook* add);//清空所有聯系人
void menu() { cout << endl; cout << "**********【主菜單】************" << endl; cout << "---------1.添加聯系人:----------" << endl; cout << "---------2.顯示聯系人:---------" << endl; cout << "---------3.刪除聯系人:---------" << endl; cout << "---------4.查找聯系人:---------" << endl; cout << "---------5.修改聯系人:---------" << endl; cout << "---------6.清空聯系人:---------" << endl; cout << "---------0.退出通訊錄:---------" << endl; cout << "*******************************" << endl; }
void putit(addressbook* add)//添加聯系人功能 { if (add->size == MAX) cout << "通訊錄已滿" << endl; else { string name; string sex; int age; string phone; string home; cout << "請輸入姓名" << endl; cin >> name; add->personArray[add->size].name = name; cout << "請輸入姓別" << endl; cin >> sex; add->personArray[add->size].sex = sex; cout << "請輸入年齡" << endl; cin >> age; add->personArray[add->size].age = age; cout << "請輸入電話號碼" << endl; cin >> phone; add->personArray[add->size].phone = phone; cout << "請輸入家庭住址" << endl; cin >> home; add->personArray[add->size].home = home; add->size++; cout << "添加聯系人成功" << 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電話號碼: " << 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 << "請輸入您想要查找的聯系人:" << endl; string name; cin >> name; int ret = if_include(add, name); if (ret == -1) { cout << "查無此人" << endl; } else { //查到此人,進行顯示操作 int i = ret; cout << "姓名:" << add->personArray[i].name << "\t"; cout << "性別:" << add->personArray[i].sex << "\t"; cout << "年齡:" << add->personArray[i].age << "\t"; cout << "聯系方式:" << add->personArray[i].phone << "\t"; cout << "地址:" << add->personArray[i].home << endl; } //按任意鍵清屏 system("pause"); system("cls"); }
void cleanperson(struct addressbook* add)//清空所有聯系人 { cout << "是否確認清空?" << endl; cout << "1 --- 是" << endl; cout << "2 --- 否" << endl; int a; cin >> a; if (a == 1) { add->size = 0;//將當前記錄聯系人數量置為0,做邏輯上的清空操作 cout << "通訊錄已清空" << endl; } system("pause"); system("cls"); }
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 << "請輸入你要刪除的人的名字" << 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 << "輸入不合法,請重新輸入" << endl; break; } } } }
#include<iostream> using namespace std; #include<string> #define MAX 100 struct person { string name;//姓名 string sex; //性別 int age; //年齡 string phone;//手機號 string home;//地址 }; struct addressbook { struct person personArray[MAX]; //通訊錄擴展到100; int size=0; //當前聯系人個數(后面就相當于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);//查找聯系人 void cleanperson(struct addressbook* add);//清空所有聯系人 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 << "請輸入你要刪除的人的名字" << 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 << "輸入不合法,請重新輸入" << endl; break; } } } } void menu() { cout << endl; cout << "***********【主菜單】***********" << endl; cout << "---------1.添加聯系人:----------" << endl; cout << "---------2.顯示聯系人:---------" << endl; cout << "---------3.刪除聯系人:---------" << endl; cout << "---------4.查找聯系人:---------" << endl; cout << "---------5.修改聯系人:---------" << endl; cout << "---------6.清空聯系人:---------" << endl; cout << "---------0.退出通訊錄:---------" << endl; cout << "********************************" << endl; } void putit(addressbook* add)//添加聯系人功能 { if (add->size == MAX) cout << "通訊錄已滿" << endl; else { string name; string sex; int age; string phone; string home; cout << "請輸入姓名" << endl; cin >> name; add->personArray[add->size].name = name; cout << "請輸入姓別" << endl; cin >> sex; add->personArray[add->size].sex = sex; cout << "請輸入年齡" << endl; cin >> age; add->personArray[add->size].age = age; cout << "請輸入電話號碼" << endl; cin >> phone; add->personArray[add->size].phone = phone; cout << "請輸入家庭住址" << endl; cin >> home; add->personArray[add->size].home = home; add->size++; cout << "添加聯系人成功" << 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電話號碼: " << 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 << "請輸入您想要查找的聯系人:" << endl; string name; cin >> name; int ret = if_include(add, name); if (ret == -1) { cout << "查無此人" << endl; } else { //查到此人,進行顯示操作 int i = ret; cout << "姓名:" << add->personArray[i].name << "\t"; cout << "性別:" << add->personArray[i].sex << "\t"; cout << "年齡:" << add->personArray[i].age << "\t"; cout << "聯系方式:" << add->personArray[i].phone << "\t"; cout << "地址:" << add->personArray[i].home << endl; } //按任意鍵清屏 system("pause"); system("cls"); } void cleanperson(struct addressbook* add)//清空所有聯系人 { cout << "是否確認清空?" << endl; cout << "1 --- 是" << endl; cout << "2 --- 否" << endl; int a; cin >> a; if (a == 1) { add->size = 0;//將當前記錄聯系人數量置為0,做邏輯上的清空操作 cout << "通訊錄已清空" << endl; } system("pause"); system("cls"); }
由于代碼量過大,建議大家可以試著分文件編寫代碼,也可以方便查看
關于“C++如何實現簡易通訊錄管理系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。