文章目录
- C/C++语言实现简易通讯录
- 概要
- 基本功能
- 运行截图展示
- 主要代码展示
🎖 博主的CSDN主页:Ryan.Alaskan Malamute
📜 博主的代码仓库主页 [ Gitee ]:@ryanala [GitHub]: Ryan-Ala
C/C++语言实现简易通讯录
⚠⚠⚠ 如果需要代码可以前往这里下载 简易通讯录(最终版)
概要
本采用c++语言实现的简易通讯录,具体功能包括添加、删除,查找和修改联系人信息,显示和清空联系人列表等基本功能
该简易通讯录采用了循环双链表的数据结构,以及文件操作等技术,能够实现对通讯录联系人基本信息的修改与保存
基本功能
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除联系人
- 查找联系人:按照姓名进行查找联系人
- 修改联系人:根据姓名重新修改联系人
- 清空联系人:清空通讯录中所有信息
- 保存通讯录:将本次录入的联系人基本信息保存至文件中,以便在下一次打开时查阅
运行截图展示
登录
录入基本信息
显示联系人
查找联系人
修改联系人
主要代码展示
双链表实现部分
注意:这里只给出部分代码,如有需要全部源码链接已在文章开头给出链接
ListNode.cpp
#pragma once#include"contact.h"typedef struct ListNode
{Datatype data;struct ListNode* next;struct ListNode* prev; //指向下一个节点
} *pListNode, contact;//初始化循环双链表
pListNode Init_List();//创建一个新的节点
pListNode Buy_newNode(); pListNode Buy_newNode(peoinform* x); //L前插入
void Insert_List(pListNode L); //尾插
void Insert_End(pListNode L);
void Insert_End(pListNode L, pListNode x); //头插
void Insert_Head(pListNode L); //删除
int Delete(pListNode s); //尾删
void Delete_End(pListNode L); //头删
void Delete_Head(pListNode L); //打印
void Printf_List(pListNode L); //任意位置插入
//int Insert_List(pListNode L, Datatype x, int i); //查找
pListNode Find_List(pListNode L, const char* x); //删除某个元素
int Delete_List(pListNode L, const char* x); //销毁
int Destory_List(pListNode L); //判空
int Empty_List(pListNode L); //长度
int Length_List(pListNode L);
ListNode.cpp
#include"ListNode.h"pListNode Init_List() //初始化循环双链表
{pListNode s = (pListNode)malloc(sizeof(ListNode));if (!s){cerr << ":) ERROR! Dynamic allocation of memory failed" << endl;exit(1);}s->next = s;s->prev = s;return s;
}pListNode Buy_newNode() //创建一个新的节点
{pListNode newnode = (pListNode)malloc(sizeof(ListNode));if (!newnode){cerr << ": ERROR! Dynamic allocation of memory failed" << endl;exit(1);}Scanf_peoinform(&newnode->data);newnode->prev = NULL;newnode->next = NULL;return newnode;
}pListNode Buy_newNode(peoinform* x)
{pListNode newnode = (pListNode)malloc(sizeof(ListNode));if (!newnode){cerr << ":) ERROR! Dynamic allocation of memory failed" << endl;exit(1);}strcpy(newnode->data.name, x->name);strcpy(newnode->data.sex, x->sex);strcpy(newnode->data.tele, x->tele);strcpy(newnode->data.address, x->address);strcpy(newnode->data.realation, x->realation);strcpy(newnode->data.birthday, x->birthday);strcpy(newnode->data.notes, x->notes);newnode->prev = NULL;newnode->next = NULL;return newnode;
}void Insert_List(pListNode L)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}system("cls");pListNode prev = L->prev;pListNode newNode = Buy_newNode();prev->next = newNode;newNode->prev = prev;newNode->next = L;L->prev = newNode;cout << "输入完成" << endl;system("pause");system("cls");
}
void Insert_End(pListNode L)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}Insert_List(L);
}void Insert_End(pListNode L, pListNode x)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}if (!x){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}pListNode prev = L->prev;prev->next = x;x->prev = prev;x->next = L;L->prev = x;
}void Insert_Head(pListNode L)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}Insert_List(L->next);
}void Delete_End(pListNode L)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}if (Empty_List(L)){exit(1);}Delete(L->prev);
}void Delete_Head(pListNode L)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}if (Empty_List(L)){exit(1);}Delete(L->next);
}void Printf_List(pListNode L)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}pListNode s = L->next;cout << "表中元素为:";while (s != L){cout << "姓名:" << s->data.name << " ";cout << "性别:" << s->data.sex << " ";cout << "生日:" << s->data.birthday << " ";cout << "电话:" << s->data.tele << " ";cout << "地址:" << s->data.address << " ";cout << "关系:" << s->data.realation << " ";s = s->next;}cout << endl << "打印完毕!" << endl;
}pListNode Find_List(pListNode L, const char* x)
{if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;return NULL;}pListNode s = L->next;while (s != L){if (!strcmp(s->data.name,x)){return s;}else{s = s->next;}}if (s = L){return NULL;}return NULL;
}int Delete(pListNode s)
{if (!s){cerr << "Delete_L_NULL" << endl;exit(1);}s->prev->next = s->next;s->next->prev = s->prev;free(s);s = NULL;return 1;
}int Delete_List(pListNode L, const char* x)
{if (!L){cerr << "Delete_L_NULL" << endl;exit(1);}pListNode s = Find_List(L, x);if (!s){cerr << "删除失败!" << endl;return 0;}return Delete(s);
}int Destory_List(pListNode L)
{if (!L){cerr << "Destory_L_NULL" << endl;exit(1);}pListNode s = L->next, r = s;while (s != L){s = s->next;free(r);r = s;}free(L);L = NULL;return 1;
}int Empty_List(pListNode L)
{if (!L){cerr << "Empty_List_L_NULL" << endl;exit(1);}return L->next == L;
}int Length_List(pListNode L)
{if (!L){cerr << "Length_L_NULL" << endl;exit(1);}int count = 0;pListNode s = L->next;while (s != L){count++;s = s->next;}return count;
}
contact.cpp
#include"ListNode.h"
#include"Heap.h"
#include"contact.h"pListNode Init_contact()
{pListNode Head = Init_List();return Head;
}void Menu(){cout << endl;cout << "*******************************" << endl;cout << "***** 1.添加 **** 2.删除 ******" << endl;cout << "***** 3.查找 **** 4.修改 ******" << endl;cout << "***** 5.显示 **** 6.清空 ******" << endl;cout << "***** 0.退出(保存) ************" << endl;cout << "*******************************" << endl;}void Menu_revise(){cout << endl;cout << "*******************************" << endl;cout << "***** 1.姓名 **** 2.性别 ******" << endl;cout << "***** 3.生日 **** 4.电话 ******" << endl;cout << "***** 5.地址 **** 6.关系 ******" << endl;cout << "***** 7.备注 **** 0.退出(保存)*" << endl;cout << "*******************************" << endl;}void Menu_select(){cout << endl;cout << "*******************************" << endl;cout << "***** 1.继续修改当前联系人 ****" << endl;cout << "***** 2.修改其他联系人 ********" << endl;cout << "***** 0.退出(保存) ************" << endl;cout << "*******************************" << endl;}void Menu_serch(){cout << endl;cout << "*******************************" << endl;cout << "***** 1.按姓名查找 ************" << endl;cout << "***** 2.按电话号码查找 ********" << endl;cout << "*******************************" << endl;}void Menu_ashore(){cout << endl;cout << "*******************************" << endl;cout << "******* 1.登录**2.注册 ********" << endl;cout << "******* 3.注销**0.退出 ********" << endl;cout << "*******************************" << endl;}int Detect(){char size[MAX_SCANF] = { 0 };cin >> size;while (strlen(size) > 1){loop:cout << "输入格式错误!请重新输入:" << endl;cin.ignore(1024, '\n');cin >> size;}if (!isdigit(size[0]))goto loop;return size[0] - '0';}void Scanf_peoinform(Datatype*x){if (!x){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}cout << endl << "请输入联系人信息:" << endl;cout << endl << "姓名:" << endl;cin >> x->name;cout << endl << "性别:" << endl;cin >> x->sex;cout << endl << "生日:" << endl;cin >> x->birthday;cout << endl << "电话:" << endl;cin >> x->tele;cout << endl << "地址:" << endl;cin >> x->address;cout << endl << "关系:" << endl;cin >> x->realation;cout << endl << "备注:" << endl;cin >> x->notes;}void Printf_peoinform(Datatype* x){if (!x){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}cout << "姓名: " << x->name << endl;cout << "性别: " << x->sex << endl;cout << "生日: " << x->birthday << endl;cout << "电话: " << x->tele << endl;cout << "地址: " << x->address << endl;cout << "关系:" << x->realation << endl;cout << "备注:" << x->notes << endl;cout << "_______________________________" << endl;}void Display_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}if (con->next == con){cout << "通讯录中无联系人!" << endl;return;}pListNode p = con->next;while (p && p != con){Printf_peoinform(&(p->data));p = p->next;}cout << endl;}void Add_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}Insert_End(con);}void Delete_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}cout << endl << "请输入需要删除的联系人的姓名:";char name[MAX_NAME];cin >> name;pListNode pos = Find_List(con, name);if (pos){ Delete(pos);cout << "删除成功!" << endl;}else cout << "未找到名为:" << name << "的联系人,删除失败!" << endl;}void Serch_contact(contact* con){system("cls");Menu_serch();LABEL:int num = 0;num = Detect();if (num == 1)Serch_contact_name(con);else if (num == 2)Serch_contact_tele(con);else{cout << "未找到对应的命令,请重新输入! " << endl;goto LABEL;}}void Serch_contact_name(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}system("cls");cout << "请输入需要查询的联系人: ";char name[MAX_NAME];cin >> name;pListNode s = con->next;int flag = 0;while (s != con){if (strstr(s->data.name, name)){flag = 1;Printf_peoinform(&(s->data));}s = s->next;}if (!flag){cout << "未找到联系电话包含: " << name << " 的联系人!" << endl;}system("pause");system("cls");}void Serch_contact_tele(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}system("cls");cout << "请输入需要查询联系人的电话号码: ";char tele[MAX_TELE];cin >> tele;pListNode s = con->next;int flag = 0;while (s != con){if (strstr(s->data.tele, tele)){flag = 1;Printf_peoinform(&(s->data));}s = s->next;}if (!flag){cout << "未找到联系电话包含: " << tele << " 的联系人!" << endl;}system("pause");system("cls");}void Revise_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}pListNode pos = NULL;char name[MAX_NAME];int num = 0;int flag = 0;char size[MAX_SCANF] = { 0 };LABEL1:if (flag){Menu_select();cout << "Please Input a number: " << endl;num = Detect();if (num == 1)goto LABEL3;else if (num == 2)goto LABEL2;else{cout << "程序退出,保存成功!" << endl;return;}}else{LABEL2:cout << "请输入需要修改的联系人姓名: ";}cin >> name;pos = Find_List(con, name);if (pos){cout << "查询成功!该联系人信息为:" << endl;Printf_peoinform(&pos->data);do{flag = 1;LABEL3:Menu_revise();cout << "请选择需要修改的信息:";switch (Detect()){case NAME:cout << "请重新输入姓名:" << endl;cin >> pos->data.name;cout << "修改成功!" << endl;goto LABEL1;break;case SEX:cout << "请重新输入性别:" << endl;cin >> pos->data.sex;cout << "修改成功!" << endl;goto LABEL1;break;case BIRTHDAY:cout << "请重新输入生日:" << endl;cin >> pos->data.birthday;cout << "修改成功!" << endl;goto LABEL1;break;case TELE:cout << "请重新输入电话:" << endl;cin >> pos->data.tele;cout << "修改成功!" << endl;goto LABEL1;break;case ADRESS:cout << "请重新输入地址:" << endl;cin >> pos->data.address;cout << "修改成功!" << endl;goto LABEL1;break;case REALATION:cout << "请重新输入关系:" << endl;cin >> pos->data.realation;cout << "修改成功!" << endl;goto LABEL1;break;case NOTES:cout << "请重新输入备注:" << endl;cin >> pos->data.notes;cout << "修改成功!" << endl;goto LABEL1;break;case EXIT:cout << "程序退出,保存成功!" << endl;goto LAB;default:cout << "无效的命令,请重新输入: " << endl;}} while (num);}else{cout << "未找到名为: " << name << " 的联系人,无法修改!" << endl;}LAB:;}void Desstory_contact(contact*& con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}Destory_List(con);con = Init_List();cout << "清空成功!" << endl;}int Empty_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}return Empty_List(con);}void Sort_name(contact*& con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}int length = Length_List(con), i = 0;if (length > 1){contact** ret = (contact**)malloc(sizeof(contact*) * length), * p = con->next;while (p && p != con){ret[i++] = p;p = p->next;}HeapSort(ret, length);for (int i = 0; i < length - 1; i++){ret[i]->next = ret[i + 1];ret[i + 1]->prev = ret[i];}con->next = ret[0];ret[0]->prev = con;con->prev = ret[length - 1];ret[length - 1]->next = con;}cout << "已将通讯录联系人按姓名首字母排序!" << endl;}//写入通讯录void Save_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}FILE* fin = fopen("contact.txt", "wb");if (!fin){cerr << "fopen_error!" << endl;exit(1);}else{pListNode p = con->next;while (p && p != con){fwrite(&(p->data), sizeof(peoinform), 1, fin);p = p->next;}fclose(fin);fin = NULL;cout << "保存成功!" << endl;}}//退出通讯录,释放内存void Exit_contact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}Save_contact(con);Destory_List(con); //销毁循环双链表}//加载文件信息至通讯录void Loadcontact(contact* con){if (!con){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}FILE* fin = fopen("contact.txt", "rb");if (fin == NULL){return;}else{peoinform temp = { 0 };while (fread(&temp, sizeof(peoinform), 1, fin)){contact* newNode = Buy_newNode(&temp);Insert_End(con, newNode);}}fclose(fin);}// 比较bool Compare(Ashore * L, char* x, char* y){if (!strcmp(L->account, x)){if (!strcmp(L->aode, y))return true;}return false;}//登录bool Logon(Ashore* L){char account[MAX_account];char aode[MAX_code];cout << "请输入账号" << endl;cin >> account;cout << "请输入密码" << endl;cin >> aode;return !strcmp(L->account, account) && !strcmp(L->aode, aode);}void Scan(Ashore* L, char* x, char* y){strcpy(L->account, x);strcpy(L->aode, y);Save_(L);}//注册void Registe(Ashore* L){char account[MAX_account] = { 0 };char aode[MAX_code] = { 0 };cout << "请输入账号" << endl;cin >> account;cout << "请输入密码" << endl;cin >> aode;Scan(L, account, aode);}//写入void Save_(Ashore* L){if (!L){cerr << ":) ERROR! Function argument is nullptr!" << endl;exit(1);}FILE* fp = fopen("Ashore.txt", "wb");if (!fp){cerr << "fopen_error!" << endl;exit(1);}else{fwrite(L, sizeof(Ashore), 1, fp);fclose(fp);fp = NULL;cout << "保存成功" << endl;system("pause");system("cls");}}//加载密码Ashore* Loadcontact_(){FILE* fp = fopen("Ashore.txt", "rb");if (fp == NULL){return NULL;}else{Ashore temp = { 0 };if (fread(&temp, sizeof(Ashore), 1, fp)){Ashore* newNode = Buy_(&temp);fclose(fp);return newNode;}else{return NULL;}}return NULL;}Ashore* Buy_(Ashore* L){Ashore* newnode = (Ashore*)malloc(sizeof(Ashore));strcpy(newnode->account, L->account);strcpy(newnode->aode, L->aode);return newnode;}Ashore* In_(){Ashore* first = (Ashore*)malloc(sizeof(Ashore));return first;}