C++实现简易通讯录系统
来源:互联网 发布:淘宝一个月能赚多少钱 编辑:程序博客网 时间:2024/05/16 17:13
C++实现简易通讯录系统
最近为了学年论文,写了一份为C++实现简易通讯录的代码
分享一下代码
struct PeoInfo{friend class Contach;char _name[10];int _age;char _sex[5];char _tele[15];char _addr[50];PeoInfo* _next;PeoInfo(char* name=NULL, int age=NULL, char *sex=NULL, char *tele=NULL,char *addr=NULL){if (name){strcpy(_name, name);}else{strcpy(_name, "");}if (age){_age = age;}else{_age = NULL;}if (sex){strcpy(_sex, sex);}else{strcpy(_sex,"");}if (tele){strcpy(_tele,tele);}else{strcpy(_tele,"");}if (addr){strcpy(_addr, addr);}else{strcpy(_addr, "");}_next = NULL;}void SetName(const char* name){strcpy(_name, name);}void SetAge(const int age){_age = age;}void SetSex(const char* sex){strcpy(_sex,sex);}void SetTele(const char* tele){strcpy(_tele,tele);}void SetAddr(const char* addr){strcpy(_addr, addr);}char* GetName(){return _name;}int GetAge(){return _age;}char* GetSex(){return _sex;}char* GetTele(){return _tele;}char* GetAddr(){return _addr;}PeoInfo* GetNext(){return _next;}};class Contach{typedef PeoInfo Node;public:Contach(Node* head = NULL, Node* tail = NULL, int tag = 1){_head = _tail = new Node;_tag = tag;}void AddTail( Node* x) //将x插入链尾{if (_head == NULL){_head =_tail=x;x->_next = NULL;}else{_tail->_next = x;x->_next = NULL;_tail = x;}}void AddSort(Node* x) //将x按_tag指定的顺序插入{Node* prev = _head;Node* next = _head->_next;while (_tail != prev){if (_tag == 1){if (strcmp(x->_name, next->_name) < 0)break;}else{if (x->_age < next->_age)break;}prev = next;next = next->_next;}prev->_next = x;x->_next = next;if (_tail == prev)_tail = x;}Node* FindName(char* name){Node* cur;cur = _head;while (cur != NULL){if (strcmp(cur->_name, name) == 0)return cur;cur = cur->_next;}return NULL;}void DeleteName(char* name){Node* cur = _head;Node* next = _head->_next;while (next!=NULL&&strcmp(next->_name,name)==0){cur = next;next = next->_next;}if (next == NULL)return;cur->_next = next->_next;if (_tail == next)_tail = cur;delete next;}void Exchange(Node* a, Node* b){strcpy(a->_name, b->_name);a->_age = b->_age;strcpy(a->_sex,b->_sex);strcpy(a->_tele,b->_tele);strcpy(a->_addr, b->_addr);}void sort(int t){Node *i;Node *j;Node *k;i = _head->_next;while (i != NULL){j = i->_next;k = i;while (j != NULL){if (t == 1 && strcmp(j->_name, k->_name) < 0)k = j;if (t == 2 && j->_age < k->_age)k = j;j = j->_next;}if (i != k){Exchange(i, k);}i = i->_next;}_tag = t;}void ShowAll(){Node* cur = _head->_next;int a = 0;system("cls");cout << "\t\t 联系人信息 \t\t" << endl;cout << "\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~\t\t" << endl<< endl;while (cur != NULL){a++;cout <<"\t\t序号:"<< a << endl;cout << "\t\t姓名: " << cur->_name<_age<< endl;cout << "\t\t性别: " << cur->_sex << endl;cout << "\t\t手机号码: " << cur->_tele << endl;cout << "\t\t住址: " << cur->_addr << endl;cur = cur->_next;}}void SetTag(int t){_tag = t;}int GetTag(){return _tag;}Node* GetHead(){return _head;}~Contach(){Node* del;if (_head==NULL)while (_head){del = _head;_head = _head->_next;delete del;}}void load() //从文件中读取{char filename[20];cout << "请输入文件名:";cin >> filename;ifstream fin(filename, ios::in);if (fin.fail())cout << "文件无法打开!!!" << endl;return;Node* newNode;int i, n;fin.read((char*)&_tag,sizeof(int));fin.read((char*)&n, sizeof(int));for (i = 1; i <= n; ++i){newNode = new Node;fin.read((char*)newNode, sizeof(Node));AddTail(newNode);}}void save(){char filename[20];cout << "请输入文件名" << endl;cin >> filename;ofstream fout(filename, ios::app);if (!fout){cout << "文件打开失败!!!" << endl;return;}Node* cur = _head->_next;int n = 0;while (cur != NULL){n++;cur = cur->_next;}fout.write((char*)&_tag, sizeof(int));fout.write((char*)&n, sizeof(int));cur = _head->_next;while (cur){fout.write((char*)cur, sizeof(Node));cur = cur->_next;}}protected:Node* _head;Node* _tail;int _tag; //默认按姓名排序,tag=1};void DisplayMenu(){cout << endl;cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\t\t" << endl;cout << endl;cout << "~~~~~~~~~~~~~~~欢迎使用通讯录管理系统~~~~~~~~~~~~~~~~~~~~\t\t" << endl;cout << endl;cout << "********************* 简易通讯录 ********************\t\t" << endl;cout << endl;cout << "** 1.增加记录 2.删除记录 **\t\t" << endl;cout << endl;cout << "** 3.显示所有 4.查找记录 **\t\t" << endl;cout << endl;cout << "** 5.修改记录 6.文件导入 **\t\t" << endl;cout << endl;cout << "** 7.库表导出 8.库表排序 **\t\t" << endl;cout << endl;cout << "** 9.退出系统 **\t\t" << endl;cout << endl;cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\t\t" << endl;cout << "\t请选择(1-9)" ;cout << endl;}void ADD(Contach& contach){system("cls");cout << "\t\t**************************************\t\t" << endl;cout << "\t\t** **\t\t" << endl;cout << "\t\t** 增加记录 **\t\t" << endl;cout << "\t\t** **\t\t" << endl;cout << "\t\t**************************************\t\t" << endl;char name[10];int age;char sex[5];char tele[15];char addr[50];cout << "\t\t请输入联系人姓名:";cin >> name;cout << "\t\t请输入联系人年龄:";cin >> age;cout << "\t\t请输入联系人性别:";cin >> sex;cout << "\t\t请输入联系人电话:";cin >> tele;cout << "\t\t请输入联系人住址:";cin >> addr;PeoInfo* newR = new PeoInfo(name, age, sex, tele, addr);contach.AddSort(newR);cout << "\t\t添加联系人成功" << endl;cout << endl;cout << "\t\t是否继续添加记录?(Y/N)";char ch;cin >> ch;if (ch == 'Y' || ch == 'y'){ADD(contach);}else{system("pause");}}void Del(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 删除记录 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;char name[10];cout << "\t\t请输入要删除的联系人的姓名:" ;cin >> name;cout << endl << endl;PeoInfo* newR = contach.FindName(name);if (newR == NULL){cout << "\t\t没有所查找的联系人!!!";return;}cout << " \t\t该联系人信息 " << endl;cout << "\t\t*****************" << endl;cout << "\t\t姓名: " << newR->GetName() << endl;cout << "\t\t年龄: " << newR->GetAge() << endl;cout << "\t\t性别: " << newR->GetSex() << endl;cout << "\t\t手机号码: " << newR->GetTele() << endl;cout << "\t\t住址: " << newR->GetAddr() << endl;char O;cout << "\t\t确定删除此人记录?(Y/N)";cin >> O;cout << endl;cout << endl;if (O == 'Y'){contach. DeleteName(name);cout << "\t\t删除成功!!!" << endl << endl;}else{cout << "\t\t记录仍存在" << endl;system("pause");}}void ShowAll(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 显示记录 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;contach.ShowAll();cout << endl;cout << "\t\t已显示所有记录" << endl << endl;system("pause");}void Find(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 查找记录 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;char name[10];cout << "\t\t请输入要查找的联系人的姓名:";cin >> name;cout << endl << endl;PeoInfo* newR = contach.FindName(name);if (newR == NULL){cout << "\t\t没有所查找的联系人!!!";return;}cout << " \t\t该联系人信息" << endl;cout << "\t\t*****************" << endl;cout << "\t\t姓名: " << newR->GetName() << endl;cout << "\t\t年龄: " << newR->GetAge() << endl;cout << "\t\t性别: " << newR->GetSex() << endl;cout << "\t\t手机号码: " << newR->GetTele() << endl;cout << "\t\t住址: " << newR->GetAddr() << endl;cout << endl;system("pause");}void modify(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 修改记录 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;char name[10];cout << "\t\t请输入要修改的联系人的姓名:" ;cin >> name;cout << endl << endl;PeoInfo* newR = contach.FindName(name);if (newR == NULL){cout << "\t\t没有所查找的联系人!!!";return;}cout << " \t\t该联系人信息" << endl;cout << "\t\t*****************" << endl;cout << "\t\t姓名: " << newR->GetName() << endl;cout << "\t\t年龄: " << newR->GetAge() << endl;cout << "\t\t性别: " << newR->GetSex() << endl;cout << "\t\t手机号码: " << newR->GetTele() << endl;cout << "\t\t住址: " << newR->GetAddr() << endl;cout << endl;cout << "\t\t请选择以下功能:" << endl;cout << "\t\t***********************" << endl;cout << "\t\t 1.修改姓名 " << endl;cout << "\t\t 2.修改年龄" << endl;cout << "\t\t 3.修改性别" << endl;cout << "\t\t 4.修改电话号码" << endl;cout << "\t\t 5.修改住址" << endl;cout << "\t\t 0.放弃修改" << endl;cout << "\t\t请选择";int C;cin >> C;switch (C){case 1:{ char name[10]; cout << "\t\t请输入修改后的姓名"; cin >> name; newR->SetName(name); cout << "\t\t修改成功" << endl; system("pause"); break;}case 2:{ int age; cout << "\t\t请输入修改后的年龄:"; cin >> age; newR->SetAge(age); cout << "\t\t修改成功" << endl; system("pause"); break;}case 3:{ char sex[5]; cout << "\t\t请输入修改后的性别:"; cin >> sex; newR->SetSex(sex); cout << "\t\t修改成功" << endl; system("pause"); break;}case 4:{ char tele[15] = { 0 }; cout << "\t\t请输入修改后的电话号码:" ; cin >> tele; newR->SetTele(tele); cout << "\t\t修改成功" << endl; system("pause"); break;}case 5:{ char addr[50]; cout << "\t\t请输入修改后的住址:" ; cin >> addr; newR->SetAddr(addr); cout << "\t\t修改成功" << endl; system("pause"); break;}case 0:DisplayMenu();break;}contach.sort(contach. GetTag());}void AddFrontTxt(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 文件导入 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;char filename[128];cout << "\t\t请输入需要导入的文件名:";cin >> filename;cout << endl << endl;fstream fin(filename, ios::in);if (fin.fail()){cout << "\t\t文件打开失败!!!" << endl;}int i, n=0;char name[10];int age;char sex[5];char tele[15];char addr[50];fin >> n;PeoInfo* newR;for (i = 1; i <= n; ++i){if (fin.eof())break;fin >> name >> age >> sex >> tele >> addr;newR = new PeoInfo(name, age, sex, tele, addr);contach.AddTail(newR);}if (i < n)n = i;cout << "\t\t操作成功" << endl << n << "组联系人" << endl<< endl;system("pause");}void WriteToText(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 库表导出 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;char filename[128];cout << "\t\t请输入需要导入的文件名:";cin >> filename;cout << endl << endl;fstream fout(filename, ios::out);PeoInfo *newR = contach.GetHead()->GetNext();int n = 0;while (newR != NULL){n++;newR = newR->GetNext();}fout << n << endl;newR = contach.GetHead()->GetNext();while (newR != NULL){fout << newR->GetName() << setw(10);fout << newR->GetAge() << setw(10);fout << newR->GetSex() << setw(10); fout << newR->GetTele() << setw(15);fout << newR->GetAddr() << setw(20);fout << endl;newR = newR->GetNext();}cout << "\t\t操作成功" << endl;cout << n << "组联系人保存" << endl << endl;system("pause");}void Sort(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 排序库表 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;cout << "\t\t原先通讯录按照:";if (contach.GetTag() == 1)cout << "姓名排序" << endl << endl;elsecout << "年龄排序" << endl << endl;cout << "\t\t请确定你想要的排序方案:" << endl;cout << "\t\t1.按姓名" << endl;cout << "\t\t2.按年龄" << endl;cout << "\t\t请选择";int c;cin >> c;cout << endl;if (c != 1 && c != 2)c = contach.GetTag();if (c != contach.GetTag()){contach.sort(c);cout << "\t\t重新排,按:";if (contach.GetTag() == 1)cout << "姓名" << endl;elsecout << "年龄" << endl;cout << "\t\t操作成功" << endl << endl;}elsecout << "\t\t保持原有顺序" << endl << endl;system("pause");}void quit(Contach& contach){system("cls");cout << "\t\t**************************************" << endl;cout << "\t\t** **" << endl;cout << "\t\t** 退出系统 **" << endl;cout << "\t\t** **" << endl;cout << "\t\t**************************************" << endl;cout << endl << endl;cout << "\t\t是否需要存入文件?(Y/N)";char ch;cin >> ch;if (ch == 'Y' || ch == 'y'){contach.save();cout << "\t\t保存成功,谢谢使用" << endl;exit(1);}else{cout << "\t\t谢谢使用" << endl;exit(1);}system("pause");}
#define _CRT_SECURE_NO_WARNINGS 1#include#include #include #include #include #include #include using namespace std;#include "contach.h"int main(){int ch = 0, state;Contach contach;char a;cout << "\t*************************************** \n";cout << "\t** **\n";cout << "\t** Hello,欢迎使用通讯录管理系统 **\n";cout << "\t** **\n";cout << "\t*************************************** \n";cout << "\t是否打开菜单?(T/F)\n";//欢迎系统cin >> a;system("cls");if (a == 'T' || a == 't'){do{DisplayMenu();cin >> ch;state = cin.rdstate();if (state){char str[80];cin.clear();cin.getline(str, 80);ch = 10;}switch (ch){case 1:ADD(contach);break;case 2:Del(contach);break;case 3:ShowAll(contach);break;case 4:Find(contach);break;case 5:modify(contach);break;case 6:AddFrontTxt(contach);break;case 7:WriteToText(contach);break;case 8:Sort(contach);break;case 9:quit(contach);break;default:break;}} while (1);}else if (a == 'F' || a == 'f'){exit(1);}else{cout << "~~~~~Warning: 输入不合法,请重新输入。~~~~~\n";main();}return 0;}
0 0
- C++实现简易通讯录系统
- C语言实现简易通讯录
- 【简易通讯录】-----C语言实现
- C语言实现简易通讯录
- c语言模拟实现简易通讯录
- c语言实现通讯录系统
- 【C语言】 简易通讯录
- C语言简易通讯录
- c语言-简易通讯录
- 实现一个简易通讯录
- 黑马程序员——C基础---简易通讯录的实现
- C语言——简易的静态通讯录实现
- c语言实现的通讯录系统
- C语言实现通讯录管理系统
- C语言的简易通讯录
- C语言编写简易通讯录
- C语言实现简易的信贷系统
- C语言通讯录系统
- AdaBoost算法在文本分类中的总结
- 【JavaEE】The implement of JPA by "ElipseLink"
- 在恢复管理器(RMAN)创建恢复目录测试(解决报错'对表空间 XX无权限'和'名称已由现有对象使用')
- PAT_1049. Counting Ones
- Facebook为母亲节再度推出紫色小花Reaction按钮
- C++实现简易通讯录系统
- node入门(一)--内置模块
- 第6章 用例
- 超详细的Python实现新浪微博模拟登陆(小白都能懂)
- 百度地图API的简单应用
- 封装表单验证解决一站式项目需求
- C语言名企面试题
- 基数排序
- java分页 Struts+Ibatis