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