c++实现通讯录管理系统
用于实现通讯录的管理,通过链表实现。联系人修改函数省略未写。
addressbook.cpp
#include <iostream>
#include <string.h>
#include "subfunction.h"
using namespace std;
//显示菜单
//***************
//*** 1.添加 ***
//*** 2.显示 ***
//*** 3.删除 ***
//*** 4.查找 ***
//*** 5.修改 ***
//*** 6.清空 ***
//*** 7.退出 ***
//***************
void menudisplay()
{
cout << "****************" << endl;
cout << "*** 1.添加 ***" << endl;
cout << "*** 2.显示 ***" << endl;
cout << "*** 3.删除 ***" << endl;
cout << "*** 4.查找 ***" << endl;
cout << "*** 5.修改 ***" << endl;
cout << "*** 6.清空 ***" << endl;
cout << "*** 7.退出 ***" << endl;
cout << "****************" << endl;
}
//选择框架
subfunction.cpp
int select()
{
struct people *head = NULL;//创建通讯录
int select;
while (1)
{
menudisplay();
cout << "请输入您要选择的操作编号: " << endl;
cin >> select;
cout << "您选择的操作编号为: " << select << endl<<endl;
switch (select)
{
case 1://*** 1.添加 ***
head=addpeople(head);
break;
case 2://*** 2.显示 ***
printpeople(head);
break;
case 3://*** 3.删除 ***
head=deletpeople(head);
break;
case 4://*** 4.查找 ***
findpeople( head);
break;
case 5://*** 5.修改 ***
break;
case 6://*** 6.清空 ***
head= deletlist(head);
break;
case 7://*** 7.退出 ***
cout << "再见!" << endl<<endl;
system("pause");
return 0;
break;
default:
break;
}
}
}
int main()
{
select();
return 0;
}
subfunction.h
#include "subfunction.h"
#include <iostream>
#include <string>
using namespace std;
//*** 1.添加 ***
struct people * addpeople(struct people * head)
{
struct people *newpeople = new(struct people);
cout << "请输入联系人姓名(拼音):" << endl;
cin >> newpeople->name;
cout << "请输入联系人年龄:" << endl;
cin >> newpeople->age;
cout << "请输入联系人电话:" << endl;
cin >> newpeople->phonenumber;
cout << "请输入联系人性别(M or W):" << endl;
cin >> newpeople->sex;
newpeople->p=NULL;
struct people * pt=head;
struct people * q = head;
if (head == NULL)
{
head = newpeople;
}
else
{
while (pt != NULL)
{
q = pt;
pt = pt->p;
}
q->p = newpeople;
}
return head;
}
//*** 2.显示 ***
void printpeople(struct people *head)
{
struct people *ptb = head;
if (ptb == NULL)
{
cout << "通讯录无联系人" << endl;
}
else
{
while (ptb != NULL)
{
cout << "联系人姓名(拼音):" << ptb->name << endl;
cout << "联系人年龄:" << ptb->age << endl;
cout << "联系人电话:" << ptb->phonenumber << endl;
cout << "联系人性别(M or W):" << ptb->sex << endl << endl;
ptb = ptb->p;
}
}
}
//*** 3.删除 ***
struct people * deletpeople(struct people * head)
{
struct people delpeople; //待删除对象
cout << "请输入联系人姓名(拼音):" << endl;
cin >> delpeople.name;
cout << "请输入联系人年龄:" << endl;
cin >> delpeople.age;
cout << "请输入联系人电话:" << endl;
cin >> delpeople.phonenumber;
cout << "请输入联系人性别(M or W):" << endl;
cin >> delpeople.sex;
delpeople.p = NULL;
struct people *ptb = head;
struct people *qtb = head;
if (ptb == NULL)
{
cout << "通讯录为空" << endl;
}
else
{
if (ptb->phonenumber == delpeople.phonenumber)
{
head = head->p;
cout << "已删除该联系人" << endl;
delete ptb;
}
else
{
qtb = ptb;
ptb = ptb->p;
while (ptb != NULL)
{
if (ptb->phonenumber == delpeople.phonenumber)
{
qtb->p = ptb->p;
cout << "已删除该联系人" << endl;
delete ptb;
break;
}
ptb = ptb->p;
}
if (ptb == NULL)
cout << "无此联系人" << endl;
}
}
return head;
}
//*** 4.查找 ***
void findpeople(struct people * head)
{
string name;
cout << "请输入联系人姓名:" << endl;
cin >> name;
struct people *ptb = head;
if (head != NULL)
{
while (ptb != NULL)
{
if (ptb->name == name)
{
cout << "联系人姓名(拼音):" << ptb->name << endl;
cout << "联系人年龄:" << ptb->age << endl;
cout << "联系人电话:" << ptb->phonenumber << endl;
cout << "联系人性别(M or W):" << ptb->sex << endl << endl;
break;
}
ptb = ptb->p;
}
if (ptb == NULL)
{
cout<<"无此联系人"<<endl;
}
}
else cout << "无此联系人" << endl;
}
//*** 5.修改 ***
void changepeople(struct people * head);
//*** 6.清空 ***
struct people *deletlist(struct people * head)
{
delete head;
head = NULL;
return head;
}
#pragma once
#ifndef subfunction_h
#define subfunction_h
#include <iostream>
#include <string>
#define max 1000//通讯录最大含量
using namespace std;
//定义结构体类型--链表,用于存放联系人
struct people {
string name;
int age;
string phonenumber;
char sex;
struct people *p = NULL;
};
//*** 1.添加 ***
struct people * addpeople(struct people *head);
//*** 2.显示 ***
void printpeople(struct people *head);
//*** 3.删除 ***
struct people * deletpeople(struct people * head);
//*** 4.查找 ***
void findpeople(struct people * head);
//*** 5.修改 ***
void changepeople(struct people * head);
//*** 6.清空 ***
struct people * deletlist(struct people * head);
#endif // !subfunction_h