您好,登錄后才能下訂單哦!
C語言中怎么實現一個通訊錄系統,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
全部代碼如下所示:
#include <iostream> #include <string> using namespace std; const int MAX = 1000; //聯系人結構體 typedef struct person { string m_name; //姓名 int m_sex; //性別 1 男,0 女 int m_age; //年齡 string m_phone; //電話 string m_addr; //住址 }person_t; //通訊錄結構體 typedef struct addressbooks { person_t personArray[MAX]; //通訊錄中保存的聯系人數組 int m_size; //通訊錄中人員個數 }addressbooks_t; 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; } void addPerson(addressbooks_t *abs) { //判斷通訊錄是否滿了,如果滿了就不再添加 if(abs->m_size == MAX) { cout << "通訊錄已滿,無法添加!" << endl; return ; } else { //添加具體聯系人 cout << "請輸入姓名: "; cin >> abs->personArray[abs->m_size].m_name; int sex; while(true) { cout << "請輸入性別(1男,0女): "; cin >> sex; if(sex == 1 || sex == 0) { abs->personArray[abs->m_size].m_sex = sex; break; } else { cout << "輸入有誤,請重新輸入!" << endl; } } cout << "請輸入年齡: "; cin >> abs->personArray[abs->m_size].m_age; cout << "請輸入電話: "; cin >> abs->personArray[abs->m_size].m_phone; cout << "請輸入地址: "; cin >> abs->personArray[abs->m_size].m_addr; //更新通訊錄人數 ++ abs->m_size; cout << "添加成功!" << endl; system("pause"); } } void displayPerson(addressbooks_t *abs) { if(abs->m_size == 0) { cout << "記錄為空!" << endl; } else { cout << "姓名\t" << "年齡\t" << "性別\t" << "電話\t\t" << "地址\t\t" << endl; for(int i = 0; i < abs->m_size; ++i) { cout << abs->personArray[i].m_name << "\t"; cout << abs->personArray[i].m_age << "\t"; cout << (abs->personArray[i].m_sex == 1 ? "男" : "女") << "\t"; cout << abs->personArray[i].m_phone << "\t"; cout << abs->personArray[i].m_addr << "\t"; cout << endl; } } system("pause"); } //按姓名查找用戶,如果查找到返回聯系人在數組中的下標,未查找到返回-1 int findByName(addressbooks_t *abs,string name) { for(int i = 0; i < abs->m_size; ++i) { if(name == abs->personArray[i].m_name) { return i; } } return -1; } void deletePerson(addressbooks_t *abs) { string name; cout << "請輸入要刪除聯的系人姓名:"; cin >> name; int index = findByName(abs,name); if(index == -1) { cout << "通訊錄中未查到" << name << endl; } else { for(int i = index; i < abs->m_size; ++i) { abs->personArray[i] = abs->personArray[i+1]; } --abs->m_size; cout << "刪除成功" << endl; } system("pause"); } void findPerson(addressbooks_t *abs) { string name; cout << "請輸入要查找的聯系人姓名:"; cin >> name; int index = findByName(abs,name); if(index == -1) { cout << "通訊錄中未查到" << name << endl; } else { cout << "姓名\t" << "年齡\t" << "性別\t" << "電話\t\t" << "地址\t\t" << endl; cout << abs->personArray[index].m_name << "\t"; cout << abs->personArray[index].m_age << "\t"; cout << (abs->personArray[index].m_sex == 1 ? "男" : "女") << "\t"; cout << abs->personArray[index].m_phone << "\t"; cout << abs->personArray[index].m_addr << "\t"; cout << endl; } system("pause"); } void updatePerson(addressbooks_t *abs) { string name; cout << "請輸入要修改的聯系人姓名:"; cin >> name; int index = findByName(abs,name); if(index == -1) { cout << "通訊錄中未查到" << name << endl; } else { //添加具體聯系人 cout << "請輸入姓名: "; cin >> abs->personArray[index].m_name; int sex; while(true) { cout << "請輸入性別(1男,0女): "; cin >> sex; if(sex == 1 || sex == 0) { abs->personArray[index].m_sex = sex; break; } else { cout << "輸入有誤,請重新輸入!" << endl; } } cout << "請輸入年齡: "; cin >> abs->personArray[index].m_age; cout << "請輸入電話: "; cin >> abs->personArray[index].m_phone; cout << "請輸入地址: "; cin >> abs->personArray[index].m_addr; cout << "修改成功!" << endl; } system("pause"); } void clearPerson(addressbooks_t *abs) { abs->m_size = 0; cout << "清空成功!" << endl; system("pause"); } int main() { //創建一個結構體變量 addressbooks_t abs; //初始化通訊錄中人員個數 abs.m_size = 0; int select = 0; //創建用戶選擇輸入的變量 while(true) { system("cls"); //清空屏幕 showMenu(); //菜單調用 cout << "請輸入您的選擇: "; cin >> select; switch (select) { case 0: //0.退出通訊錄 cout << "歡迎下次使用" << endl; return 0; break; case 1: //1.添加聯系人 addPerson(&abs); break; case 2: //2.顯示聯系人 displayPerson(&abs); break; case 3: //3.刪除聯系人 deletePerson(&abs); break; case 4: //4.查找聯系人 findPerson(&abs); break; case 5: //5.修改聯系人 updatePerson(&abs); break; case 6: //6.清空聯系人 clearPerson(&abs); break; default: break; } } return 0; }
關于C語言中怎么實現一個通訊錄系統問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。