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"); }
main函數
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"); }
由於代碼量過大,建議大傢可以試著分文件編寫代碼,也可以方便查看
運行結果
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。