通讯录管理系统
1.头文件
#include<iostream>
#include<string.h>
#include<stdlib.h>
#define maxsize 10
using namespace std;
2.定义数据类型
typedef struct node {
int age;
int ID;
string name;
string tel;
node* next;
}node,*linknode;
typedef struct book {
linknode list;
linknode tag;
int length;
} book;
3.菜单函数
void showthemeau(){
printf("**************菜单**************\n");
printf("* 1.添加联系人 *\n");
printf("* 2.删除联系人 *\n");
printf("* 3.查看通讯录 *\n");
printf("* 4.检索联系人 *\n");
printf("* 5.统计联系人 *\n");
printf("* 0.退出通讯录 *\n");
printf("********************************\n");
}
4.初始化通讯录
void initbook(book &Book) {
Book.list = new node;
Book.tag = Book.list;
Book.length = 0;
Book.list->next = NULL;
}
5.统计联系人
void counttheperson(book Book){
cout << "现在联系人共有" <<Book.length<<"人!\n"<< endl;
}
6.增加联系人
void addtheperson(book &Book) {
linknode person;
person = new node;
cout << "请输入联系人姓名:" << endl;
cin >> person->name;
cout << "请输入联系人年龄:" << endl;
cin >> person->age;
cout << "请输入联系人电话号码:" << endl;
cin >> person->tel;
cout << "请输入联系人身份ID:" << endl;
cin >> person->ID;
person->next = NULL;
Book.tag->next=person;
Book.tag = person;
Book.length++;
cout << "联系人" << person->name << "已添加成功!" << endl;
}
7.显示所有联系人
void readthebook(book Book) {
int i;
cout << "联系人清单如下:" << endl;
linknode ptr = Book.list->next;
for (i = 0; i < Book.length; i++) {
cout <<"序号:" << i+1 << "\t\t姓名:" << ptr->name <<"\t\t 年龄:" << ptr->age << "\t\t电话号码:" << ptr->tel << endl;
ptr = ptr->next;
}
}
8.删除联系人
void deletetheperson(book& Book) {
if (Book.length == 0) {
cout << "当前通讯录为空,删除失败!" << endl;
return;
}
int index;
cout << "所有人员如下,请输入所要求删除的序号:" << endl;
readthebook(Book);
cin >> index;
if (index > Book.length||index<1) {
cout << "输入的序号不正确,序号范围为:1~" << Book.length << endl;
deletetheperson(Book);
}
else {
linknode p=Book.list,s=NULL;
for (int i = 0; i < Book.length - 1; i++) {
p = p->next;
}
s = p->next;
p->next = s->next;
delete s;
Book.length--;
cout << "删除成功!" << endl;
}
}
9.检索联系人
void select_the_phone_number(book Book) {
int id;
cout << "请输入身份信息:id=" ;
cin >> id;
linknode p = Book.list->next;
while (p) {
if (p->ID == id) {
cout << "身份信息如下:" << endl;
cout << "\t姓名:" << p->name << "\t\t 年龄:" << p->age << "\t\t电话号码:" << p->tel << endl;
return;
}
p = p->next;
}
cout << "查无此人!" << endl;
}
10.主函数
int main() {
int meau;
bool break_the_program = true;
book Book;
initbook(Book);
cout << " ***************************************" << endl;
cout << " *******欢迎使用通讯录管理系统!********" << endl;
cout << " ***************************************" << endl;
cout << "\n\n\n\n\n" << endl;
system("pause");
system("cls");
while (break_the_program) {
showthemeau();
cin >> meau;
switch (meau) {
case 0:
break_the_program = false;
break;
case 1:
addtheperson(Book);
system("pause");
break;
case 2:
deletetheperson(Book);
system("pause");
break;
case 3:
readthebook(Book);
system("pause");
break;
case 4:
select_the_phone_number(Book);
system("pause");
break;
case 5:
counttheperson(Book);
system("pause");
break;
default:
cout << "输入无效 请重新输入:" << endl;
break;
}
system("cls");
}
cout << "系统已退出,任意键继续!" << endl;
system("pause");
}