您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C++怎么實(shí)現(xiàn)簡(jiǎn)單通訊錄管理系統(tǒng)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
#include <iostream> #include <string> #include <cstring> #include <fstream> using namespace std; //自定義一個(gè)聯(lián)系人結(jié)點(diǎn)類型 typedef struct node1 { string name; //姓名 string tel; //手機(jī)號(hào) string email; //郵箱 string address; //地址 struct node1 *next; //指向下一節(jié)點(diǎn)的指針 } Node; //自定義一個(gè)二叉排序樹結(jié)點(diǎn)類型 typedef struct node2 { Node data; struct node2 *lchild, *rchild; } BSTNode; //構(gòu)造結(jié)點(diǎn)通訊錄類 class Fnode { public: Fnode(); //構(gòu)造函數(shù),用于初始化一些變量 ~Fnode(); //析構(gòu)函數(shù),用于程序結(jié)束之后對(duì)分配的內(nèi)存進(jìn)行清理 void CreateInfo(); //錄入聯(lián)系人信息 void InsertInfo(); //插入聯(lián)系人信息 void FindInfoName(); //按姓名查找聯(lián)系人信息 void FindInfoTel(); //按手機(jī)號(hào)查找聯(lián)系人信息 void DeleteInfoTel(); //按手機(jī)號(hào)刪除聯(lián)系人信息 void DispInfo(); //打印所有聯(lián)系人信息 void DispInfoSort(); //按姓名排序輸出聯(lián)系人信息 void SaveInfoToFile(); //保存聯(lián)系人信息至文件 void ReadInfoFromFile(); //從文件讀取聯(lián)系人信息 void Run(); //功能函數(shù),包含菜單和相關(guān)函數(shù)的入口 bool InsertBST(BSTNode *&bt, Node key); //插入二叉排序樹結(jié)點(diǎn),遞歸實(shí)現(xiàn) void CreateBST(); //由鏈表創(chuàng)建二叉排序樹 void InOrder(BSTNode *b); //中序遍歷輸出二叉排序樹,遞歸實(shí)現(xiàn) void DestroyBST(BSTNode *&b); //釋放二叉排序樹,遞歸實(shí)現(xiàn) private: bool opened; //用于表明文件是否被讀取過,未讀為0,已讀為1 Node *L, *p, *q; //定義通訊錄結(jié)點(diǎn)的頭指針和其他成員函數(shù)中可能用到的指針 BSTNode *btree; //二叉排序樹頭節(jié)點(diǎn) }; //構(gòu)造函數(shù),用于初始化一些變量 Fnode::Fnode() { L = new Node; L->next = NULL; p = q = NULL; btree = NULL; opened = 0; } //析構(gòu)函數(shù),用于程序結(jié)束之后對(duì)分配的內(nèi)存進(jìn)行清理 Fnode::~Fnode() { p = L; while (p != NULL) { delete p; p = p->next; } } void Fnode::CreateInfo() //創(chuàng)建結(jié)點(diǎn) { int n; q = L, p = NULL; while (q->next != NULL) q = q->next; cout << "請(qǐng)輸入您要錄入的聯(lián)系人個(gè)數(shù):"; cin >> n; for (int i = 0; i < n; i++) { p = new Node; cout << "請(qǐng)輸入第" << i + 1 << "位聯(lián)系人的姓名、手機(jī)號(hào)、郵箱和地址(用空格隔開):" << endl; cin >> p->name >> p->tel >> p->email >> p->address; p->next = q->next; q->next = p; q = p; } } //插入聯(lián)系人信息 void Fnode::InsertInfo() { q = L, p = NULL; while (q->next != NULL) q = q->next; p = new Node; cout << "請(qǐng)輸入您要插入的聯(lián)系人的姓名、手機(jī)號(hào)、郵箱和地址(用空格隔開):" << endl; cin >> p->name >> p->tel >> p->email >> p->address; p->next = q->next; q->next = p; cout << "插入成功!" << endl; } //打印所有聯(lián)系人信息 void Fnode::DispInfo() { p = L->next; if (p == NULL) cout << "數(shù)據(jù)為空,無法打印" << endl; else { cout << "姓名 \t\t" << "手機(jī)號(hào) \t\t" << "郵箱 \t\t" << "地址" << endl; while (p != NULL) { cout << p->name << "\t\t" << p->tel << "\t\t" << p->email << "\t\t" << p->address << endl; p = p->next; } } } //按姓名查找聯(lián)系人信息 void Fnode::FindInfoName() { string name; p = L->next; if (p == NULL) cout << "數(shù)據(jù)為空,無法查找" << endl; else { cout << "請(qǐng)輸入要查找的聯(lián)系人的姓名:"; cin >> name; while (p->name != name) { p = p->next; if (p == NULL) { cout << "沒有找到相關(guān)聯(lián)系人信息" << endl; return; } } cout << "該聯(lián)系人的信息如下" << endl; cout << "姓名 \t\t" << "手機(jī)號(hào) \t\t" << "郵箱 \t\t" << "地址" << endl; cout << p->name << "\t\t" << p->tel << "\t\t" << p->email << "\t\t" << p->address << endl; } } //按手機(jī)號(hào)查找聯(lián)系人信息 void Fnode::FindInfoTel() { string tel; p = L->next; if (p == NULL) cout << "數(shù)據(jù)為空,無法查找" << endl; else { cout << "請(qǐng)輸入要查找的聯(lián)系人的手機(jī)號(hào):"; cin >> tel; while (p->tel != tel) { p = p->next; if (p == NULL) { cout << "沒有找到相關(guān)信息" << endl; return; } } cout << "該聯(lián)系人的信息如下" << endl; cout << "姓名 \t\t" << "手機(jī)號(hào) \t\t" << "郵箱 \t\t" << "地址" << endl; cout << p->name << "\t\t" << p->tel << "\t\t" << p->email << "\t\t" << p->address << endl; } } //按手機(jī)號(hào)刪除聯(lián)系人信息 void Fnode::DeleteInfoTel() { string tel; p = L->next, q = L; if (p == NULL) cout << "數(shù)據(jù)為空,無法刪除" << endl; else { cout << "請(qǐng)輸入要?jiǎng)h除的聯(lián)系人的手機(jī)號(hào)" << endl; cin >> tel; while (p->tel != tel) { p = p->next; q = q->next; if (p == NULL) { cout << "沒有找到相關(guān)信息" << endl; return; } } q->next = p->next; delete p; cout << "手機(jī)號(hào)為" << tel << "的聯(lián)系人刪除成功!" << endl; } } //按姓名排序輸出聯(lián)系人信息 void Fnode::DispInfoSort() { CreateBST(); cout << "姓名 \t\t" << "手機(jī)號(hào) \t\t" << "郵箱 \t\t" << "地址" << endl; InOrder(btree); DestroyBST(btree); } //保存聯(lián)系人信息至文件 void Fnode::SaveInfoToFile() { p = L->next; if (p == NULL) cout << "數(shù)據(jù)為空,無法寫入!" << endl; else { ofstream outfile("./聯(lián)系人.csv"); if (!outfile) { cout << "無法打開文件!"; return; } outfile << "姓名," << "手機(jī)號(hào)," << "郵箱," << "地址" << endl; while (p->next != NULL) { outfile << p->name << "," << p->tel << "," << p->email << "," << p->address << endl; p = p->next; } outfile << p->name << "," << p->tel << "," << p->email << "," << p->address; outfile.close(); cout << "保存聯(lián)系人信息成功!請(qǐng)查看當(dāng)前目錄下的“聯(lián)系人.csv文件”" << endl; } } //從文件讀取聯(lián)系人信息 void Fnode::ReadInfoFromFile() { ifstream infile("./聯(lián)系人.csv"); if (!infile) { cout << "無法打開文件!"; return; } else if (opened == 1) { cout << "文件已經(jīng)被讀取,不能夠重復(fù)讀取!" << endl; return; } else { char str[500]; q = L, p = NULL; while (q->next != NULL) q = q->next; infile.getline(str, sizeof(str)); while (!infile.eof()) { p = new Node; infile.getline(str, sizeof(str)); p->name = strtok(str, ","); p->tel = strtok(NULL, ","); p->email = strtok(NULL, ","); p->address = strtok(NULL, ","); p->next = q->next; q->next = p; q = p; } infile.close(); cout << "讀取數(shù)據(jù)成功!" << endl; opened = 1; } } //插入二叉排序樹結(jié)點(diǎn) bool Fnode::InsertBST(BSTNode *&bt, Node key) { if (bt == NULL) { bt = new BSTNode; bt->data = key; bt->lchild = bt->rchild = NULL; return true; } else if (key.name == bt->data.name) return false; else if (key.name < bt->data.name) return InsertBST(bt->lchild, key); else return InsertBST(bt->rchild, key); } //由鏈表創(chuàng)建二叉排序樹 void Fnode::CreateBST() { btree = NULL; p = L->next; while (p != NULL) { InsertBST(btree, *p); p = p->next; } } //中序遍歷輸出二叉排序樹 void Fnode::InOrder(BSTNode *b) { if (b != NULL) { InOrder(b->lchild); cout << b->data.name << "\t\t" << b->data.tel << "\t\t" << b->data.email << "\t\t" << b->data.address << endl; InOrder(b->rchild); } } //釋放二叉排序樹 void Fnode::DestroyBST(BSTNode *&b) { if (b != NULL) { DestroyBST(b->lchild); DestroyBST(b->rchild); delete b; } } //菜單 void Fnode::Run() { int item; do { cout << "\t\t\t==================通訊錄管理系統(tǒng)==================" << endl; cout << "\t\t\t# \t #" << endl; cout << "\t\t\t# \t1)聯(lián)系人信息的逐條錄入。 #" << endl; cout << "\t\t\t# \t2)插入某個(gè)聯(lián)系人的信息。 #" << endl; cout << "\t\t\t# \t3)按手機(jī)號(hào)查找某個(gè)聯(lián)系人的信。 #" << endl; cout << "\t\t\t# \t4)按姓名查詢某個(gè)聯(lián)系人。 #" << endl; cout << "\t\t\t# \t5)按手機(jī)號(hào)刪除某個(gè)聯(lián)系人的信息 #" << endl; cout << "\t\t\t# \t6)所有聯(lián)系人信息的輸出顯示。 #" << endl; cout << "\t\t\t# \t7)所有聯(lián)系人按姓名排序并輸出顯示。 #" << endl; cout << "\t\t\t# \t8)所有聯(lián)系人信息的文件保存。 #" << endl; cout << "\t\t\t# \t9)所有聯(lián)系人信息的文件讀取。 #" << endl; cout << "\t\t\t# \t0)退出管理系統(tǒng)。 #" << endl; cout << "\t\t\t==================================================" << endl; cout << "請(qǐng)輸入相應(yīng)的命令,執(zhí)行相應(yīng)的功能:"; cin >> item; system("cls"); switch (item) { case 1: CreateInfo(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 2: InsertInfo(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 3: FindInfoTel(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 4: FindInfoName(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 5: DeleteInfoTel(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 6: DispInfo(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 7: DispInfoSort(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 8: SaveInfoToFile(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 9: ReadInfoFromFile(); getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); break; case 0: cout << "即將退出通訊錄管理系統(tǒng)......" << endl; cout << "謝謝您的使用!"; exit(0); break; default: cout << "您輸入的指令錯(cuò)誤,請(qǐng)重新輸入!" << endl; getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); } } while (item); } //主函數(shù) int main() { Fnode f; system("cls"); f.Run(); return 0; }
“C++怎么實(shí)現(xiàn)簡(jiǎn)單通訊錄管理系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(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)容。