C++實(shí)現(xiàn)通訊錄功能
本文實(shí)例為大家分享了C++實(shí)現(xiàn)通訊錄的具體代碼,供大家參考,具體內(nèi)容如下
簡介:通訊錄由一個(gè)擁有者以及通訊信息組成。
基本功能:增刪改查
擁有者和通訊信息的基礎(chǔ)結(jié)構(gòu)相同,由struct構(gòu)成
struct Person { ?? ?int m_id; ?? ?string m_name; ?? ?string m_tele;//手機(jī)號(hào)碼可以作為id,但是過于長(11位) ?? ?//string m_addr; ? ?? ?Person& operator = (const Person& r) { ?? ??? ?if (this == &r) return *this; ?? ??? ?m_id = r.m_id; ?? ??? ?m_name = r.m_name; ?? ??? ?m_tele = r.m_tele; ?? ??? ?return *this; ?? ?} };
Person:id+姓名+手機(jī)號(hào)。還可以添加需要的信息,例如:地址、性別
重載了一個(gè)=操作符
通訊錄建立class AddressList
class AddressList { private: ?? ?Person owner;//通訊錄擁有者 ?? ?vector<Person> information;//通訊錄:好友的信息組成 public: ?? ?AddressList(); ?? ?AddressList(const Person&, const vector<Person>& info = {}); ?? ?AddressList(const AddressList&); ? ?? ?void Add(const Person&);//添加一個(gè)好友信息至通訊錄 ?? ?void Delete();//通過姓名刪除 ?? ??? ? ? ? ?//通過電話號(hào)碼刪除 ?? ?void Modify();//輸入id 修改姓名and號(hào)碼 ?? ?void Search(int);//1:id搜索 ?? ??? ??? ? //2:姓名搜索 ?? ??? ??? ? //3:號(hào)碼指定搜索 ?? ?void Print()const; ?? ?//查看通訊錄所有信息:僅顯示id和姓名,詳細(xì)信息輸入id查看?? ? };
cpp:
#include "AddressList.h" #include <iostream> using namespace std; ? AddressList::AddressList() {} ? AddressList::AddressList(const Person& r, const vector<Person>& info) { ?? ?owner = r; ?? ?for (const auto& i : info) { ?? ??? ?information.push_back(i); ?? ?} } ? AddressList::AddressList(const AddressList& r) { ?? ?owner = r.owner; ?? ?for (const auto& i : r.information) { ?? ??? ?information.push_back(i); ?? ?} } ? void AddressList::Add(const Person& p) { ?? ?//添加一個(gè)好友信息至通訊錄 ?? ?//首先確認(rèn)不存在:id+tele ?? ?for (const auto& it : information) { ?? ??? ?if (p.m_id == it.m_id) { ?? ??? ??? ?cout << "Id已存在,添加失敗!\n"; ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?else if (p.m_tele == it.m_tele) { ?? ??? ??? ?cout << "Telephone已存在,添加失敗!\n"; ?? ??? ??? ?return; ?? ??? ?} ?? ?} ?? ?information.push_back(p); } ? void AddressList::Delete() { ?? ?//通過姓名刪除 ?? ?//略:通過電話號(hào)碼刪除 ?? ?string name; ?? ?cout << "姓名:"; cin >> name; ?? ?cout << "查找到信息如下:"; ?? ?auto it = information.begin(); ?? ?vector<int> info;//存儲(chǔ)下標(biāo) ?? ?for (int i(0); it != information.end(); ++it,++i) { ?? ??? ?if (it->m_name == name) info.push_back(i); ?? ?} ?? ?if (info.size() == 0) { ?? ??? ?cout << "No such name.\n"; ?? ??? ?return; ?? ?} ?? ?for (const auto& i : info) { ?? ??? ?cout << i << ":\t" << information[i].m_id << '\t' << information[i].m_name ?? ??? ??? ?<< '\t' << information[i].m_tele << endl; ?? ?} ? ?? ?int ind; ?? ?cout << "輸入下標(biāo)(第一列)刪除信息:"; ?? ?cin>>ind; ?? ?for (const auto& i : info) { ?? ??? ?if (i == ind) { ?? ??? ??? ?information.erase(information.begin() + i); ?? ??? ??? ?return; ?? ??? ?} ?? ?} ?? ?cout << "輸入信息錯(cuò)誤,刪除失敗!\n"; } ? void AddressList::Modify() { ?? ?//輸入id:修改姓名and號(hào)碼 ?? ?long id; ?? ?cout << "Id:"; cin >> id; ?? ?cout << "查找到信息如下:\n"; ?? ?auto it = information.begin(); ?? ?for (; it != information.end(); ++it) { ?? ??? ?if (it->m_id == id) { ?? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl; ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?if (it == information.end()) { ?? ??? ?cout << "No such Id.\n"; ?? ??? ?return; ?? ?} ?? ?cout << "修改信息:\n"; ?? ??? ??? ?string name; ?? ??? ??? ?string tele; ?? ??? ??? ?cout << "新的姓名:"; cin >> name; ?? ??? ??? ?cout << "新的號(hào)碼:"; cin >> tele; ?? ??? ??? ?char c; ?? ??? ??? ?cout << "確認(rèn)?<y/n> "; ?? ??? ??? ?cin >> c; ?? ??? ??? ?if (c == 'y' || c == 'Y') { ?? ??? ??? ??? ?it->m_name = name; ?? ??? ??? ??? ?it->m_tele = tele; ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ??? ?cout << "取消修改!\n"; ?? ??? ??? ?return; } void AddressList::Search(int cho) { ?? ?//1:id搜索 ?? ?//2:姓名搜索 ?? ?//3:號(hào)碼指定搜索 ?? ?int id; ?? ?string name; ?? ?string tele; ?? ?auto it = information.begin(); ?? ?switch (cho) { ?? ??? ?case 1: ?? ??? ??? ?cout << "Id:"; ?? ??? ??? ?cin >> id; ?? ??? ??? ?cout << "搜索到的信息如下:\n"; ?? ??? ??? ?for (it = information.begin(); it != information.end(); ++it) { ?? ??? ??? ??? ?if (it->m_id == id) { ?? ??? ??? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?cout << "Name:"; ?? ??? ??? ?cin >> name; ?? ??? ??? ?cout << "搜索到的信息如下:\n"; ?? ??? ??? ?for (it = information.begin(); it != information.end(); ++it) { ?? ??? ??? ??? ?if (it->m_name == name) ?? ??? ??? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl; ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?cout << "Tele:"; ?? ??? ??? ?cin >> tele; ?? ??? ??? ?cout << "搜索到的信息如下:\n"; ?? ??? ??? ?for (it = information.begin(); it != information.end(); ++it) { ?? ??? ??? ??? ?if (it->m_tele == tele) { ?? ??? ??? ??? ??? ?cout << it->m_id << '\t' << it->m_name << '\t' << it->m_tele << endl; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ??? ?break; ?? ??? ?default:break; ?? ?} } void AddressList::Print()const { ?? ?cout << "ID:" << owner.m_id << endl; ?? ?cout << "Owner:" << owner.m_name << endl ?? ??? ?<< "Tele:" << owner.m_tele << endl; ?? ?int n(information.size()); ?? ?cout << "通訊錄人數(shù):" << n << endl; ?? ?for (int i(0); i < n; ++i) { ?? ??? ?cout << information[i].m_id << '\t' << information[i].m_name << endl; ?? ?} ?? ?while (1) { ?? ??? ?cout << endl ?? ??? ??? ?<< "詳細(xì)信息,請(qǐng)輸入id:-1終止查看\n"; ?? ??? ?int id; ?? ??? ?cin >> id; ?? ??? ?if (id == -1) break; ?? ??? ?bool b(false); ?? ??? ?for (const auto& it : information) { ?? ??? ??? ?if (id == it.m_id) { ?? ??? ??? ??? ?b = true; ?? ??? ??? ??? ?cout << it.m_id << '\t' << it.m_name << '\t' << it.m_tele << endl; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (!b) { ?? ??? ??? ?cout << "No such Id.!" << endl; ?? ??? ?} ?? ?} }
main.cpp:測試
#include"AddressList.h" #include <iostream> using namespace std; ? int main() { ?? ?Person p; ?? ?{ ?? ??? ?p.m_id = 0; ?? ??? ?p.m_name = "一號(hào)"; ?? ??? ?p.m_tele = "11012011900";//任意 ?? ?} ?? ?int I = 1;//m_id編號(hào) ?? ?AddressList addr(p); ?? ?{ ?? ??? ?cout << "0.退出\n" ?? ??? ??? ?<< "1.添加\n" ?? ??? ??? ?<< "2.刪除\n" ?? ??? ??? ?<< "3.修改\n" ?? ??? ??? ?<< "4.搜索\n" ?? ??? ??? ?<< "5.查看\n" ?? ??? ??? ?<< endl ?? ??? ??? ?<< endl; ?? ?} ?? ?int cho2; ?? ?bool b(true); ?? ?while (b) { ?? ??? ?int cho; ?? ??? ?int id; ?? ??? ?string name; ?? ??? ?string tele; ? ?? ??? ?cout << "Your choose:"; ?? ??? ?cin >> cho; ?? ??? ?switch (cho) { ?? ??? ?case 0: ?? ??? ??? ?b = false; ?? ??? ??? ?break; ?? ??? ?case 1: ?? ??? ??? ?cout << "添加信息:\n" ?? ??? ??? ??? ?<< "姓名:"; ?? ??? ??? ?cin >> name; ?? ??? ??? ?cout << "號(hào)碼:"; ?? ??? ??? ?cin >> tele; ?? ??? ??? ?id = I++; ?? ??? ??? ?{ ?? ??? ??? ??? ?p.m_id = id; ?? ??? ??? ??? ?p.m_name = name; ?? ??? ??? ??? ?p.m_tele = tele; ?? ??? ??? ?} ?? ??? ??? ?addr.Add(p); ?? ??? ??? ?break; ?? ??? ?case 2: ?? ??? ??? ?cout << "刪除信息:\n"; ?? ??? ??? ?addr.Delete(); ?? ??? ??? ?break; ?? ??? ?case 3: ?? ??? ??? ?cout << "修改信息:\n"; ?? ??? ??? ?addr.Modify(); ?? ??? ??? ?break; ?? ??? ?case 4: ?? ??? ??? ?cout << "搜索信息\n" ?? ??? ??? ??? ?<< "1.Id\n" ?? ??? ??? ??? ?<< "2.Name\n" ?? ??? ??? ??? ?<< "3.Telephone\n"; ?? ??? ??? ?cout << "Chosse:"; ?? ??? ??? ?cin >> cho2; ?? ??? ??? ?addr.Search(cho2); ?? ??? ??? ?break; ?? ??? ?case 5: ?? ??? ??? ?cout << "查看信息\n"; ?? ??? ??? ?addr.Print(); ?? ??? ??? ?break; ?? ??? ?default:break; ?? ??? ?} ?? ??? ?cout << endl; ?? ?} ?? ?return 0; }
截圖:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言的常量,字符串,轉(zhuǎn)義字符,注釋你都了解嗎
這篇文章主要為大家詳細(xì)介紹了C語言的常量,字符串,轉(zhuǎn)義字符,注釋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02Qt自定義控件實(shí)現(xiàn)進(jìn)度儀表盤
這篇文章主要介紹了Qt自定義控件實(shí)現(xiàn)進(jìn)度儀表盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12C語言實(shí)現(xiàn) 數(shù)據(jù)類型占多少字節(jié)指針占多少字節(jié)
這篇文章主要介紹了 C語言 數(shù)據(jù)類型占多少字節(jié)指針占多少字節(jié)的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09詳解c/c++鏈?zhǔn)蕉褩C枋鲞M(jìn)制轉(zhuǎn)換問題示例
這篇文章主要為大家介紹了c/c++鏈?zhǔn)蕉褩C枋鲞M(jìn)制轉(zhuǎn)換問題示例解析有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11C語言 結(jié)構(gòu)體數(shù)組詳解及示例代碼
本文主要介紹C語言 結(jié)構(gòu)體數(shù)組,這里整理了相關(guān)資料及簡單示例代碼,以便大家學(xué)習(xí)參考,有興趣的小伙伴可以看下2016-08-08C++使用sort對(duì)容器排序的實(shí)現(xiàn)
C++ STL 標(biāo)準(zhǔn)庫中的sort()函數(shù)專門用來對(duì)容器或普通數(shù)組中指定范圍內(nèi)的元素進(jìn)行排序,本文就詳細(xì)的介紹一下怎么實(shí)現(xiàn),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05