您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++如何實現通訊錄小功能”,在日常操作中,相信很多人在C++如何實現通訊錄小功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++如何實現通訊錄小功能”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
1.顯示菜單欄
void menu() { 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; }
2.退出
int main() { int select = 0; cin >> select; switch (select) { case 0://退出通訊錄 cout << "歡迎下次使用" << endl; system("pause"); return 0; break; } system("pause"); return 0; }
3.創建結構體
3.1創建聯系人結構體
3.2創建通訊錄結構體
//定義聯系人結構體 //姓名、電話號碼、郵箱、地址 struct person { string name; string number; string Email; string address; }; // 定義通訊錄結構體 struct contacts { int people_num; struct person personarr[MAX];//子結構體:聯系人信息 };
4.添加聯系人
void addperson(struct contacts* p) { if (p->people_num == MAX ) { cout << "通訊錄已滿" << endl; } else {//添加聯系人信息 string name, number, Email, address; cout << "請輸入姓名:" << endl; cin >> name; cout << "請輸入電話:" << endl; cin >> number; cout << "請輸入郵箱:" << endl; cin >> Email; cout << "請輸入地址:" << endl; cin >> address; p->personarr[p->people_num].name = name; p->personarr[p->people_num].number = number; p->personarr[p->people_num].Email = Email; p->personarr[p->people_num].address = address; p->people_num++; cout << "添加成功!" << endl; } }
5.刪除聯系人
判斷聯系人是否存在
int existperson(struct contacts* p,string name) { for (int i = 0; i < p->people_num; i++) { if ( p->personarr[i].name == name ) { return i; } } return -1; }
若存在,獲取聯系人在通訊錄位置,將position后面的都往前移動一個位置,覆蓋之前的值
//刪除聯系人 void delperson(struct contacts* p,int position) { while (position < (p->people_num)) { p->personarr[position] = p->personarr[position + 1]; position++; } p->people_num--; cout << "刪除成功!" << endl; }
6.修改
檢查要修改聯系人是否存在,并獲取當前位置
void modifyperson(struct contacts* p, int position) { string number, Email, address; cout << "請輸入修改電話:" << endl; cin >> number; cout << "請輸入修改郵箱:" << endl; cin >> Email; cout << "請輸入修改地址:" << endl; cin >> address; p->personarr[position].number = number; p->personarr[position].Email = Email; p->personarr[position].address = address; cout << "修改成功!" << endl; }
7.查找
void searchperson(struct contacts* p, int position) { cout << "姓名:" << p->personarr[position].name << " " << "電話:" << p->personarr[position].number << " " << "郵箱:" << p->personarr[position].Email << " " << "地址:" << p->personarr[position].address << " "; }
8.顯示通訊錄
void showcontact(struct contacts* p) { for (int i = 0; i < (p->people_num); i++) { cout <<"姓名:" << p->personarr[i].name << " " <<"電話:" << p->personarr[i].number << " " <<"郵箱:" << p->personarr[i].Email << " " <<"地址:" << p->personarr[i].address << " "<< endl; } }
9.清空通訊錄
void cleancontact(struct contacts* p) { p->people_num = 0; cout << "已清空!" << endl; }
#include<iostream> using namespace std; #define MAX 200 //1.菜單欄顯示 void menu() { 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; } //2.定義結構體 //2.1定義聯系人結構體 //姓名、電話號碼、郵箱、地址 struct person { string name; string number; string Email; string address; }; //2.2 定義通訊錄結構體 struct contacts { int people_num; struct person personarr[MAX];//子結構體:聯系人信息 }; //3.添加聯系人 void addperson(struct contacts* p) { if (p->people_num == MAX ) { cout << "通訊錄已滿" << endl; } else {//添加聯系人信息 string name, number, Email, address; cout << "請輸入姓名:" << endl; cin >> name; cout << "請輸入電話:" << endl; cin >> number; cout << "請輸入郵箱:" << endl; cin >> Email; cout << "請輸入地址:" << endl; cin >> address; p->personarr[p->people_num].name = name; p->personarr[p->people_num].number = number; p->personarr[p->people_num].Email = Email; p->personarr[p->people_num].address = address; p->people_num++; cout << "添加成功!" << endl; } } //4.刪除聯系人 //4.1檢測聯系人是否存在 int existperson(struct contacts* p,string name) { for (int i = 0; i < p->people_num; i++) { if ( p->personarr[i].name == name ) { return i; } } return -1; } //刪除聯系人 void delperson(struct contacts* p,int position) { while (position < (p->people_num)) { p->personarr[position] = p->personarr[position + 1]; position++; } p->people_num--; cout << "刪除成功!" << endl; } //5.修改聯系人 void modifyperson(struct contacts* p, int position) { string number, Email, address; cout << "請輸入修改電話:" << endl; cin >> number; cout << "請輸入修改郵箱:" << endl; cin >> Email; cout << "請輸入修改地址:" << endl; cin >> address; p->personarr[position].number = number; p->personarr[position].Email = Email; p->personarr[position].address = address; cout << "修改成功!" << endl; } //6.查找聯系人 void searchperson(struct contacts* p, int position) { cout << "姓名:" << p->personarr[position].name << " " << "電話:" << p->personarr[position].number << " " << "郵箱:" << p->personarr[position].Email << " " << "地址:" << p->personarr[position].address << " "; } //7.顯示通訊錄 void showcontact(struct contacts* p) { for (int i = 0; i < (p->people_num); i++) { cout <<"姓名:" << p->personarr[i].name << " " <<"電話:" << p->personarr[i].number << " " <<"郵箱:" << p->personarr[i].Email << " " <<"地址:" << p->personarr[i].address << " "<< endl; } } //8.清空聯系人 void cleancontact(struct contacts* p) { p->people_num = 0; cout << "已清空!" << endl; } int main() { //創建通訊錄結構體變量 struct contacts c; //初始化通訊錄當前聯系人個數 c.people_num = 0; int select = 0; string name; while (1) { menu(); cin >> select; switch (select) { case 1:// 添加聯系人 addperson(&c); system("pause"); break; case 2:// 刪除聯系人 cout << "請輸入刪除聯系人的姓名:"; cin >> name; if (existperson(&c,name)==-1) { cout << "該聯系人不存在!"; } else{ delperson(&c, existperson(&c, name)); } system("pause"); break; case 3:// 修改聯系人 cout << "請輸入要修改聯系人的姓名:"; cin >> name; if (existperson(&c,name) == -1) { cout << "該聯系人不存在!"; } else { modifyperson(&c, existperson(&c, name)); } system("pause"); break; case 4:// 查找聯系人 cout << "請輸入查找聯系人的姓名:"; cin >> name; if (existperson(&c,name) == -1) { cout << "該聯系人不存在!"; } else { searchperson(&c, existperson(&c, name)); } system("pause"); break; case 5:// 顯示通訊錄 showcontact(&c); system("pause"); break; case 6:// 清空聯系人 cleancontact(&c); system("pause"); break; case 0://退出通訊錄 cout << "歡迎下次使用" << endl; system("pause");//暫停批文件的處理 return 0; break; } system("cls");//清屏 } system("pause"); return 0; }
到此,關于“C++如何實現通訊錄小功能”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。