C++實現簡單通訊錄管理系統
本文實例為大傢分享瞭C++實現簡單的通訊錄管理系統的具體代碼,供大傢參考,具體內容如下
一、代碼
#include <iostream> #include <string> #include <cstring> #include <fstream> using namespace std; //自定義一個聯系人結點類型 typedef struct node1 { string name; //姓名 string tel; //手機號 string email; //郵箱 string address; //地址 struct node1 *next; //指向下一節點的指針 } Node; //自定義一個二叉排序樹結點類型 typedef struct node2 { Node data; struct node2 *lchild, *rchild; } BSTNode; //構造結點通訊錄類 class Fnode { public: Fnode(); //構造函數,用於初始化一些變量 ~Fnode(); //析構函數,用於程序結束之後對分配的內存進行清理 void CreateInfo(); //錄入聯系人信息 void InsertInfo(); //插入聯系人信息 void FindInfoName(); //按姓名查找聯系人信息 void FindInfoTel(); //按手機號查找聯系人信息 void DeleteInfoTel(); //按手機號刪除聯系人信息 void DispInfo(); //打印所有聯系人信息 void DispInfoSort(); //按姓名排序輸出聯系人信息 void SaveInfoToFile(); //保存聯系人信息至文件 void ReadInfoFromFile(); //從文件讀取聯系人信息 void Run(); //功能函數,包含菜單和相關函數的入口 bool InsertBST(BSTNode *&bt, Node key); //插入二叉排序樹結點,遞歸實現 void CreateBST(); //由鏈表創建二叉排序樹 void InOrder(BSTNode *b); //中序遍歷輸出二叉排序樹,遞歸實現 void DestroyBST(BSTNode *&b); //釋放二叉排序樹,遞歸實現 private: bool opened; //用於表明文件是否被讀取過,未讀為0,已讀為1 Node *L, *p, *q; //定義通訊錄結點的頭指針和其他成員函數中可能用到的指針 BSTNode *btree; //二叉排序樹頭節點 }; //構造函數,用於初始化一些變量 Fnode::Fnode() { L = new Node; L->next = NULL; p = q = NULL; btree = NULL; opened = 0; } //析構函數,用於程序結束之後對分配的內存進行清理 Fnode::~Fnode() { p = L; while (p != NULL) { delete p; p = p->next; } } void Fnode::CreateInfo() //創建結點 { int n; q = L, p = NULL; while (q->next != NULL) q = q->next; cout << "請輸入您要錄入的聯系人個數:"; cin >> n; for (int i = 0; i < n; i++) { p = new Node; cout << "請輸入第" << i + 1 << "位聯系人的姓名、手機號、郵箱和地址(用空格隔開):" << endl; cin >> p->name >> p->tel >> p->email >> p->address; p->next = q->next; q->next = p; q = p; } } //插入聯系人信息 void Fnode::InsertInfo() { q = L, p = NULL; while (q->next != NULL) q = q->next; p = new Node; cout << "請輸入您要插入的聯系人的姓名、手機號、郵箱和地址(用空格隔開):" << endl; cin >> p->name >> p->tel >> p->email >> p->address; p->next = q->next; q->next = p; cout << "插入成功!" << endl; } //打印所有聯系人信息 void Fnode::DispInfo() { p = L->next; if (p == NULL) cout << "數據為空,無法打印" << endl; else { cout << "姓名 \t\t" << "手機號 \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; } } } //按姓名查找聯系人信息 void Fnode::FindInfoName() { string name; p = L->next; if (p == NULL) cout << "數據為空,無法查找" << endl; else { cout << "請輸入要查找的聯系人的姓名:"; cin >> name; while (p->name != name) { p = p->next; if (p == NULL) { cout << "沒有找到相關聯系人信息" << endl; return; } } cout << "該聯系人的信息如下" << endl; cout << "姓名 \t\t" << "手機號 \t\t" << "郵箱 \t\t" << "地址" << endl; cout << p->name << "\t\t" << p->tel << "\t\t" << p->email << "\t\t" << p->address << endl; } } //按手機號查找聯系人信息 void Fnode::FindInfoTel() { string tel; p = L->next; if (p == NULL) cout << "數據為空,無法查找" << endl; else { cout << "請輸入要查找的聯系人的手機號:"; cin >> tel; while (p->tel != tel) { p = p->next; if (p == NULL) { cout << "沒有找到相關信息" << endl; return; } } cout << "該聯系人的信息如下" << endl; cout << "姓名 \t\t" << "手機號 \t\t" << "郵箱 \t\t" << "地址" << endl; cout << p->name << "\t\t" << p->tel << "\t\t" << p->email << "\t\t" << p->address << endl; } } //按手機號刪除聯系人信息 void Fnode::DeleteInfoTel() { string tel; p = L->next, q = L; if (p == NULL) cout << "數據為空,無法刪除" << endl; else { cout << "請輸入要刪除的聯系人的手機號" << endl; cin >> tel; while (p->tel != tel) { p = p->next; q = q->next; if (p == NULL) { cout << "沒有找到相關信息" << endl; return; } } q->next = p->next; delete p; cout << "手機號為" << tel << "的聯系人刪除成功!" << endl; } } //按姓名排序輸出聯系人信息 void Fnode::DispInfoSort() { CreateBST(); cout << "姓名 \t\t" << "手機號 \t\t" << "郵箱 \t\t" << "地址" << endl; InOrder(btree); DestroyBST(btree); } //保存聯系人信息至文件 void Fnode::SaveInfoToFile() { p = L->next; if (p == NULL) cout << "數據為空,無法寫入!" << endl; else { ofstream outfile("./聯系人.csv"); if (!outfile) { cout << "無法打開文件!"; return; } outfile << "姓名," << "手機號," << "郵箱," << "地址" << 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 << "保存聯系人信息成功!請查看當前目錄下的“聯系人.csv文件”" << endl; } } //從文件讀取聯系人信息 void Fnode::ReadInfoFromFile() { ifstream infile("./聯系人.csv"); if (!infile) { cout << "無法打開文件!"; return; } else if (opened == 1) { cout << "文件已經被讀取,不能夠重復讀取!" << 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 << "讀取數據成功!" << endl; opened = 1; } } //插入二叉排序樹結點 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); } //由鏈表創建二叉排序樹 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==================通訊錄管理系統==================" << endl; cout << "\t\t\t# \t #" << endl; cout << "\t\t\t# \t1)聯系人信息的逐條錄入。 #" << endl; cout << "\t\t\t# \t2)插入某個聯系人的信息。 #" << endl; cout << "\t\t\t# \t3)按手機號查找某個聯系人的信。 #" << endl; cout << "\t\t\t# \t4)按姓名查詢某個聯系人。 #" << endl; cout << "\t\t\t# \t5)按手機號刪除某個聯系人的信息 #" << endl; cout << "\t\t\t# \t6)所有聯系人信息的輸出顯示。 #" << endl; cout << "\t\t\t# \t7)所有聯系人按姓名排序並輸出顯示。 #" << endl; cout << "\t\t\t# \t8)所有聯系人信息的文件保存。 #" << endl; cout << "\t\t\t# \t9)所有聯系人信息的文件讀取。 #" << endl; cout << "\t\t\t# \t0)退出管理系統。 #" << endl; cout << "\t\t\t==================================================" << endl; cout << "請輸入相應的命令,執行相應的功能:"; 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 << "即將退出通訊錄管理系統......" << endl; cout << "謝謝您的使用!"; exit(0); break; default: cout << "您輸入的指令錯誤,請重新輸入!" << endl; getchar(); cout << "\n按任意鍵返回主菜單" << endl; getchar(); system("cls"); } } while (item); } //主函數 int main() { Fnode f; system("cls"); f.Run(); return 0; }
二、功能展示
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。