C++实现简易通讯录系统
2017-05-14 15:32
661 查看
C++实现简易通讯录系统
最近为了学年论文,写了一份为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); } els 4000 e { 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; a8f5 cout << endl << endl; cout << "\t\t原先通讯录按照:"; if (contach.GetTag() == 1) cout << "姓名排序" << endl << endl; else cout << "年龄排序" << 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; else cout << "年龄" << endl; cout << "\t\t操作成功" << endl << endl; } else cout << "\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; }
相关文章推荐
- C++实现简易log日志系统
- 01-基于C++的简易技能系统实现
- C++实现简易log日志系统
- c++链表实现学生成绩管理系统(简易版)
- 习题10:参照Windows系统“附件”中的“计算器”,自行编写一个简易的计算器。要求:可以实现由0~4构成的整数的加减运算。
- struts+SmartUpload实现的一个简易上传下载系统
- 简易学生信息管理系统(c++源代码)
- C++ 快速实现开源日志系统-Log4cplus
- 简易学生成绩管理系统(c++)
- 模拟实现简易的移动用户资费统计系统
- 通讯录:C++实现
- c++实现系统托盘图标
- 金融系统中PBOC/EMV的TLV的算法实现(含C++/C#)
- 视频监控笔记——用C++实现的远程监控系统概述
- 一道面试题--模拟实现简易的移动用户资费统计系统逻辑
- Windows平台下C++插件系统实现的几个关键技术问题及其解决思路
- C++控制台版简易仓库管理系统
- 成绩管理系统的C++实现
- “基于关键字匹配的文本过滤系统”配置文件的设计和实现(C/C++源码)
- 个人通讯录管理系统的设计与实现