目前我也是名初學C++的菜鳥,一直在B站上看的C++的網課,這個的C++電話通訊錄是我寫的第一個有一些功能的代碼,所以在這里想分享給初學C++的小白們,如有任何問題或是建議可以在下方評論或是私信我,下面就先介紹一下這個電話通訊錄的功能。
一、功能目錄
1、添加聯系人
2、顯示聯系人
3、刪除聯系人
4、查找聯系人
5、修改聯系人
6、清空聯系人
0、退出聯系人
二、功能介紹+部分函數代碼的展示
1、添加聯系人
在控制台中輸入1就表示是添加聯系人的功能,隨后會依次出現 “姓名:”、“性別:”、“年齡:”、“聯系電話:”、“聯系地址:”的字樣,我們只需正確輸入即可,其中“性別:”中只能輸入“男”或“女”,如果輸入其他的字樣就會被要求重新輸入。還有這個添加聯系人的添加人數是1000人。
void addpeople(peoplebook* abs)
{
if (abs->size == max) { cout << "聯系人已滿,無法添加" << endl; return; } else { string name; cout << "請輸入名字: " << endl; cin >> name; abs->peoplearray[abs->size].name = name; string sex; while (true) { cout << "請輸入性別: " << endl; cin >> sex; if (sex == "男" || sex == "女") { abs->peoplearray[abs->size].sex = sex; break; } else { cout << "輸入錯誤,請重新輸入" << endl; } } int age; cout << "請輸入年齡: " << endl; cin >> age; abs->peoplearray[abs->size].age = age; string phone; cout << "請輸入聯系電話: " << endl; cin >> phone; abs->peoplearray[abs->size].phone = phone; string map; cout << "請輸入聯系地址: " << endl; cin >> map; abs->peoplearray[abs->size].map = map; cout << "添加成功" << endl; abs->size++; system("pause"); system("cls"); } }
2、顯示聯系人
添加完聯系人之后就可以顯示聯系人了,輸入2就會顯示出之前添加的聯系人了。
void showpeople(peoplebook* abs)
{
if (abs->size == 0) { cout << "對不起,未添加聯系人,請添加聯系人" << endl; } else { for(int i=0;i<abs->size;++i) { cout << "姓名: " << "\t"; cout << abs->peoplearray[i].name << "\t"; cout << "性別: " << "\t"; cout << abs->peoplearray[i].sex << "\t"; cout << "年齡: " << "\t"; cout << abs->peoplearray[i].age << "\t"; cout << "聯系電話: " << "\t"; cout << abs->peoplearray[i].phone << "\t"; cout << "聯系地址: " << "\t"; cout << abs->peoplearray[i].map << endl; } } system("pause"); system("cls"); }
3、刪除聯系人
輸入3后,再輸入要刪除的聯系人的姓名就可以刪除想要刪除的聯系人了。
void delpeople(peoplebook* abs)
{
string name; cin >> name; int res = ispeople(name, abs); if (res == -1) { cout << "查無此人" << endl; } if(res != -1) { for (int i = res;i < abs->size;++i) { abs->peoplearray[i] = abs->peoplearray[i + 1]; } abs->size--; cout << "刪除成功!!!" << endl; } system("pause"); system("cls"); }
4、查找聯系人
添加完聯系人之后,輸入4再輸入想要查找聯系人的姓名就可以顯示出來了。
void chapeople(peoplebook* abs)
{
string name; cin >> name; int res = ispeople(name, abs); if (res == -1) { cout << "查無此人" << endl; } if (res != -1) { cout << "姓名: " << "\t"; cout << abs->peoplearray[res].name << "\t"; cout << "性別: " << "\t"; cout << abs->peoplearray[res].sex << "\t"; cout << "年齡: " << "\t"; cout << abs->peoplearray[res].age << "\t"; cout << "聯系電話: " << "\t"; cout << abs->peoplearray[res].phone << "\t"; cout << "聯系地址: " << "\t"; cout << abs->peoplearray[res].map << endl; } system("pause"); system("cls"); }
5、修改聯系人
輸入5再輸入將要修改的聯系人的姓名就可以重新添加聯系人了。
void modifypeople(peoplebook* abs)
{
string name; cin >> name; int res = ispeople(name, abs); if (res == -1) { cout << "查無此人" << endl; } if (res != -1) { string name; cout << "請輸入名字: " << endl; cin >> name; abs->peoplearray[res].name = name; string sex; while (true) { cout << "請輸入性別: " << endl; cin >> sex; if (sex == "男" || sex == "女") { abs->peoplearray[res].sex = sex; break; } else { cout << "輸入錯誤,請重新輸入" << endl; } } int age; cout << "請輸入年齡: " << endl; cin >> age; abs->peoplearray[res].age = age; string phone; cout << "請輸入聯系電話: " << endl; cin >> phone; abs->peoplearray[res].phone = phone; string map; cout << "請輸入聯系地址: " << endl; cin >> map; abs->peoplearray[res].map = map; cout << "添加成功" << endl; } system("pause"); system("cls"); }
6、清空聯系人
輸入6就可以刪除所有保存過的聯系人了。
void cleanpeople(peoplebook* abs)
{
abs->size = 0; cout << "已清空完畢" << endl; system("pause"); system("cls"); }
0、退出聯系人
輸入0就可退出控制台。
三、主函數+剩余的代碼
#include<iostream>
#include <string>
#define max 1000
using namespace std; void showMenu() { 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; } struct people { string name; string sex; int age; string phone; string map; }; struct peoplebook { people peoplearray[max]; int size=0; }; int main() { int select = 0; string name; peoplebook abs; while (true) { showMenu(); cin >> select; switch (select) { case 1://1、添加聯系人 addpeople(&abs); break; case 2://2、顯示聯系人 showpeople(&abs); break; case 3://3、刪除聯系人 cout << "請輸入聯系人的姓名:" << endl; delpeople(&abs); break; case 4://4、查找聯系人 cout << "請輸入聯系人的姓名:" << endl; chapeople(&abs); break; case 5://5、修改聯系人 cout << "請輸入聯系人的姓名:" << endl; modifypeople(&abs); break; case 6://、清空聯系人 cleanpeople(&abs); break; case 0: cout << "歡迎下次使用" << endl; return 0; break; } } return 0; }
四、結尾
此代碼是我在B站上的C++傳智播客中的案例中學習到的,有想要學習的也可以去B站上學習。這個代碼量對於初學C++來說是比較多的,但是難度並不是很大,主要是練習了struct、調用函數的使用。我相信如果把這些代碼寫下來的話,也可以增長一些對C++的興趣。因為我自身也是初學C++的菜鳥,其中有任何建議和意見可以私信給我,或者可以互相討論交流一起學習。