简单的通讯录管理系统

来源:互联网 发布:控制网络连接管理 编辑:程序博客网 时间:2024/06/05 08:52

一个简单的通讯录管理系统  采用链表,有待优化 ,如果使用带头结点的链表 代码可以更精简

// 通讯录管理系统.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<stdlib.h>#include<fstream>using namespace std;struct Tonxunlu{string name;string num;Tonxunlu *front;Tonxunlu *next;};class Myclass{public:void add();void del();void find();void xiugai();void print();void save();void load();void mainmenu();Tonxunlu *head = NULL;};void Myclass::add() {Tonxunlu * new_node;new_node = new Tonxunlu;//分配空间cout << "请输入姓名:   ";cin >> new_node->name;cout << "请输入电话号码:   ";cin >> new_node->num;new_node->front = NULL;//新结点没有前驱new_node->next = head;//新节点的后继为原来链表的第一个结点if (!head)//原表为空{head = new_node;}else{head->front = new_node;//原来表中的第一个结点的前驱是新插入的结点head = new_node;//新结点成为链表的第一个结点}}void Myclass::del() {int choice3;string name1, num1;Tonxunlu *q;q = head;if (!head) { cout << "通讯录是空的,请按3结束" << endl; }cout << "按姓名删除请按1,电话号码删除请按2" << endl;cin >> choice3;switch (choice3){case 1:cout << "请输入要删除的联系人姓名" << endl;cin >> name1;//循环结束q指向name值为name1的结点while (q&&q->name != name1){q = q->next;}if (!q) { cout << "没有找到名字为" << name1 << "的联系人" << endl; break; }//被删除的结点是第一个结点且表中不只有一个结点if (q == head&&head->next){   //头结点指向头结点后指针指向的结点head = head->next;//此时头结点的指针为空head->front = NULL;//把name值为name1的结点空间撤销delete q;q = NULL;break;}//被删除的结点是第一个结点并且只有一个结点if (q == head && !head->next){head = NULL;delete q;q = NULL;break;}else {//被删除的结点是双链表中的尾结点if (!q->next) {          q->front->next = NULL;delete q;q = NULL;break;}else {//q是两个以上的结点且不是头结点和尾结点q->front->next = q->next;q->next->front = q->front;delete q;q = NULL;break;}}case 2:cout << "请输入要删除的联系人电话号码" << endl;cin >> num1;while (q&&q->num != num1){q = q->next;}if (!q) { cout << "没有找到电话号码为" << num1 << "的联系人" << endl; break; }if (q == head&&head->next){head = head->next;head->front = NULL;delete q;q = NULL;break;}if (q == head && !head->next){head = NULL;delete q;q = NULL;break;}else {if (!q->next) {q->front->next = NULL;delete q;q = NULL;break;}else {q->front->next = q->next;q->next->front = q->front;delete q;q = NULL;break;}}case 3:break;default:cout << "请输入正确的字符" << endl; getchar();break;}}void Myclass::find() {int choice;string name1, num1;Tonxunlu *q;q = head;cout << "按姓名查找请按1,电话号码查找请按2" << endl;cin >> choice;switch (choice){case 1:cout << "请输入要查找的联系人姓名" << endl;cin >> name1;if (!q) cout << "通讯录是空的,无法找到该联系人!";else{while (q){if (q->name == name1){cout << "姓名:    " << name1 << "电话号码:       " << q->num << endl; getchar();break;}else { q = q->next; }}if (!q) { cout << "没有找到姓名为" << name1 << "的联系人" << endl;getchar(); }}break;case 2:cout << "请输入要查找的联系人电话号码" << endl;cin >> num1;if (!q) cout << "通讯录是空的,无法找到该联系人!" << endl;else{while (q){if (q->num == num1){cout << "姓名:    " << q->name << "电话号码:     " << num1 << endl;break;}else { q = q->next; }}if (!q) { cout << "没有找到电话号码为" << num1 << "的联系人" << endl; getchar(); }}break;default:cout << "请输入正确的字符" << endl; getchar();break;}}void Myclass::xiugai() {int choice;string name1, num1, name2, num2;Tonxunlu *q;q = head;cout << "按姓名修改请按1,电话号码修改请按2" << endl;cin >> choice;switch (choice){case 1:cout << "请输入要修改的联系人姓名" << endl;cin >> name1;if (!q) cout << "通讯录是空的,无法完成修改!";else{while (q){if (q->name == name1){cout << "请输入要修改为的姓名" << endl;cin >> name2;cout << "姓名    " << name2 << "电话号码    " << q->num << endl;q->name = name2;//getchar();break;}else { q = q->next; }}if (!q) { cout << "没有找到姓名为" << name1 << "的联系人" << endl;getchar(); }}break;case 2:cout << "请输入要修改的联系人电话号码" << endl;cin >> num1;if (!q) cout << "通讯录是空的,无法完成修改!" << endl;else{while (q){if (q->num == num1){cout << "请输入要修改为的电话号码" << endl;cin >> num2;cout << "姓名    " << q->name << "电话号码    " << num2 << endl;q->num = num2;//getchar();break;}else { q = q->next; }}if (!q) { cout << "没有找到电话号码为" << num1 << "的联系人" << endl; getchar(); }}break;default:cout << "请输入正确的字符" << endl;break;}}void Myclass::print() {Tonxunlu *q;q = head;if (!q){cout << "这是个空通讯录" << endl;}else{while (q) { cout << "姓名:   " << q->name << "电话号码:    " << q->num << endl;q = q->next; }getchar();}}void Myclass::mainmenu() {int select;cout << "\t\t" << endl<< "\t\t--------------------------------------------------" << endl<< "\t\t|                                                |" << endl<< "\t\t|                                                |" << endl<< "\t\t|             通 迅 录 管 理 系 统               |" << endl<< "\t\t|                                                |" << endl<< "\t\t|    1. 添加联系人          2. 修改联系人        |" << endl<< "\t\t|                                                |" << endl<< "\t\t|    3. 删除联系人          4. 查询联系人        |" << endl<< "\t\t|                                                |" << endl<< "\t\t|    5. 通讯录显示          6. 储存              |" << endl<< "\t\t|                                                |" << endl<< "\t\t|    7. 读取                8. 退出系统          |" << endl<< "\t\t|                                                |" << endl<< "\t\t--------------------------------------------------" << endl<< "\t\t                                                  " << endl<< "\t\t       请选择数字 : ";cin >> select;switch (select){case 1:cout << "添加联系人" << endl;do{add();cout << "是否继续添加联系人信息(Y/N)" << endl;getchar();} while ((getchar() == 'Y') || (getchar() == 'y'));break;case 2:cout << "修改联系人" << endl;do {xiugai();cout << "是否继续修改联系人信息(Y/N)" << endl;getchar();} while ((getchar() == 'Y') || (getchar() == 'y'));break;case 3:cout << "删除联系人" << endl;do {del();cout << "是否继续删除联系人信息(Y/N)" << endl;getchar();} while ((getchar() == 'Y') || (getchar() == 'y'));break;case 4:cout << "查询联系人" << endl;find();break;case 5:cout << "通讯录显示" << endl;print();break;case 6:cout << "储存" << endl;save();break;case 7:cout << "读取" << endl;load();break;case 8:system("cls");cout << "欢迎下次使用" << endl;getchar();exit(0);break;default:system("cls");cout << "请输入正确的操作" << endl;break;}}void Myclass::save() {Tonxunlu *p;p = head;ofstream infile("Tonxunlu.txt", ios::out);while (p){infile << p->name << "        " << p->num << endl;;p = p->next;}cout << "保存成功" << endl;infile.close();}void Myclass::load() {ifstreamoutfile;Tonxunlu *p = head;string Name, Num;outfile.open("Tonxunlu.txt", ios::in);outfile >> Name >> Num;while (!outfile.eof()) {p = new Tonxunlu;p->name = Name;p->num = Num;p->front = NULL;p->next = head;head = p;outfile >> Name >> Num;}outfile.close();}int main(){Myclass mc;while (1) { mc.mainmenu(); getchar(); }return 0;}