注意标题的极简是极为简单,有点C++基础的基本都能写出来,当作复习C++基础的留念
项目需求参考《黑马程序员》通讯录管理系统实战
源码链接C++简易通讯录
- 添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
- 显示联系人:显示通讯录中所有联系人信息
- 删除联系人:按照姓名进行删除指定联系人
- 查找联系人:按照姓名查看指定联系人信息
- 修改联系人:按照姓名重新修改指定联系人
- 清空联系人:清空通讯录中所有信息
- 退出通讯录:退出当前使用的通讯录
头文件
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
#define MAX 1000
//联系人结构体
struct person
{
string name;
int sex;
int age;
int phone;
string addr;
};
//通讯录
struct books
{
struct person personarray[MAX];
int size;
};
//显示菜单
void showMenu();
//添加联系人:向通讯录中添加新人,信息包括(姓名、性别、年龄、联系电话、家庭住址)最多记录1000人
void addPerson(books* abs);
//显示联系人:显示通讯录中所有联系人信息
void showperson(books abs);
//删除联系人:按照姓名进行删除指定联系人
int ifexist(string name, books abs);//查找联系人是否存在
void delperson(books* abs);
//查找联系人:按照姓名查看指定联系人信息
void findperson(books abs);
//修改联系人:按照姓名重新修改指定联系人
void modifyperson(books* abs);
//清空联系人:清空通讯录中所有信息
void clear(books* abs);
源文件(全文在最后)
main
#include"top.h"
int main()
{
//初始化通讯录
books abs;
abs.size = 0;
int menu;
while (1)
{
showMenu();
cin >> menu;
switch (menu)
{
case 1:
addPerson(&abs);
break;
case 2:
showperson(abs);
break;
case 3:
delperson(&abs);
break;
case 4:
findperson(abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
clear(&abs);
break;
case 0://退出通讯录
cout << "thanks" << endl;
system("pause");
return 0;
break;
default:
cout << "wrong!" << endl;
break;
}
}
return 0;
}
显示菜单
void showMenu()
{
cout << "***************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***************************" << endl;
}
添加联系人
void addPerson(books* abs)
{
if (abs->size == MAX)//通讯录满员
{
cout << "MAX" << endl;
return;
}
else
{
string name;
int sex;
int age;
int phone;
string addr;
bool sex_flag = true, age_flag = true;
cout << "name" << endl;
cin >> name;
abs->personarray[abs->size].name = name;
while (sex_flag)//检测输入是否正确
{
cout << "sex, 1->man, 2->woman" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[abs->size].sex = sex;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
while (age_flag)//年龄范围为1-119
{
cout << "age" << endl;
cin >> age;
if (age > 0 && age < 120)
{
abs->personarray[abs->size].age = age;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
cout << "phone" << endl;
cin >> phone;
abs->personarray[abs->size].phone = phone;
cout << "address" << endl;
cin >> addr;
abs->personarray[abs->size].addr = addr;
abs->size++;
cout << "success" << endl;
system("pause");
system("cls");
}
}
显示联系人
void showperson(books abs)
{
if (abs.size == 0)
{
cout << "none" << endl;
}
else
{
for (int i = 0; i < abs.size; i++)
{
cout << "No." << i+1 << "person:" << endl;
cout << "name:" << abs.personarray[i].name << endl;
cout << "age:" << abs.personarray[i].age << endl;
cout << "sex:";//注意这里不能显示1/2,要显示男/女
int sex = abs.personarray[i].sex;
sex == 1 ? cout << "man" << endl : cout << "woman" << endl;
cout << "phone:" << abs.personarray[i].phone << endl;
cout << "addr:" << abs.personarray[i].addr << endl;
cout << endl;
}
}
system("pause");
system("cls");
}
删除联系人
int ifexist(books abs, string name)//查找要找的人是否存在
{
for (int i = 0; i < abs.size; i++)
{
if (abs.personarray[i].name == name)
{
return i;
}
}
return -1;
}
void delperson(books* abs)
{
string d_name;
cout << "name" << endl;
cin >> d_name;
int ret = ifexist(*abs, d_name);
if (ret == -1)
{
cout << "none" << endl;
return;
}
else
{
for (int i = ret; i < abs->size; i++)
{
abs->personarray[i] = abs->personarray[i + 1];
abs->size--;
cout << "success" << endl;
}
}
system("pause");
system("cls");
}
查找联系人
void findperson(books abs)
{
string f_name;
cout << "name" << endl;
cin >> f_name;
int i = ifexist(abs, f_name);
if (i == -1)
{
cout << "can't find" << endl;
}
else
{
cout << "name:" << abs.personarray[i].name << endl;
cout << "age:" << abs.personarray[i].age << endl;
cout << "sex:";
int sex = abs.personarray[i].sex;
sex == 1 ? cout << "man" << endl : cout << "woman" << endl;
cout << "phone:" << abs.personarray[i].phone << endl;
cout << "addr:" << abs.personarray[i].addr << endl;
}
}
修改联系人
void modifyperson(books* abs)
{
string m_name;
int where;
bool flag=true;
cout << "name" << endl;
cin >> m_name;
int i = ifexist(*abs, m_name);
if (i == -1)
{
cout << "can't find" << endl;
}
else
{
while (flag)
{
cout << "why would you want to change?" << endl;
cout << "1-name; 2-sex; 3-age; 4-phone; 5-addr; 0-none" << endl;
cin >> where;
switch (where)
{
case 1:
{
string name;
cout << "name:" << endl;
cin >> name;
abs->personarray[i].name = name;
break;
}
case 2:
{
int sex;
bool sex_flag = true;
while (sex_flag)
{
cout << "sex, 1->man, 2->woman" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[i].sex = sex;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
break;
}
case 3:
{
int age;
bool age_flag = true;
while (age_flag)
{
cout << "age" << endl;
cin >> age;
if (age > 0 && age < 120)
{
abs->personarray[abs->size].age = age;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
break;
}
case 4:
{
int phone;
cout << "phone:" << endl;
cin >> phone;
abs->personarray[i].phone = phone;
break;
}
case 5:
{
string addr;
cout << "addr:" << endl;
cin >> addr;
abs->personarray[i].addr = addr;
break;
}
case 0:
flag = false;
break;
default:
cout << "wrong input!" << endl;
break;
}
}
}
system("pause");
system("cls");
}
清空通讯录
void clear(books* abs)
{
abs->size = 0;//很简单,让通讯录数组大小为0就可以了
cout << "success" << endl;
system("pause");
system("cls");
}
全码
#include"top.h"
int main()
{
books abs;
abs.size = 0;
int menu;
while (1)
{
showMenu();
cin >> menu;
switch (menu)
{
case 1:
addPerson(&abs);
break;
case 2:
showperson(abs);
break;
case 3:
delperson(&abs);
break;
case 4:
findperson(abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
clear(&abs);
break;
case 0:
cout << "thanks" << endl;
system("pause");
return 0;
break;
default:
cout << "wrong!" << endl;
break;
}
}
return 0;
}
void showMenu()
{
cout << "***************************" << endl;
cout << "***** 1、添加联系人 *****" << endl;
cout << "***** 2、显示联系人 *****" << endl;
cout << "***** 3、删除联系人 *****" << endl;
cout << "***** 4、查找联系人 *****" << endl;
cout << "***** 5、修改联系人 *****" << endl;
cout << "***** 6、清空联系人 *****" << endl;
cout << "***** 0、退出通讯录 *****" << endl;
cout << "***************************" << endl;
}
void addPerson(books* abs)
{
if (abs->size == MAX)
{
cout << "MAX" << endl;
return;
}
else
{
string name;
int sex;
int age;
int phone;
string addr;
bool sex_flag = true, age_flag = true;
cout << "name" << endl;
cin >> name;
abs->personarray[abs->size].name = name;
while (sex_flag)
{
cout << "sex, 1->man, 2->woman" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[abs->size].sex = sex;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
while (age_flag)
{
cout << "age" << endl;
cin >> age;
if (age > 0 && age < 120)
{
abs->personarray[abs->size].age = age;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
cout << "phone" << endl;
cin >> phone;
abs->personarray[abs->size].phone = phone;
cout << "address" << endl;
cin >> addr;
abs->personarray[abs->size].addr = addr;
abs->size++;
cout << "success" << endl;
system("pause");
system("cls");
}
}
void showperson(books abs)
{
if (abs.size == 0)
{
cout << "none" << endl;
}
else
{
for (int i = 0; i < abs.size; i++)
{
cout << "No." << i+1 << "person:" << endl;
cout << "name:" << abs.personarray[i].name << endl;
cout << "age:" << abs.personarray[i].age << endl;
cout << "sex:";
int sex = abs.personarray[i].sex;
sex == 1 ? cout << "man" << endl : cout << "woman" << endl;
cout << "phone:" << abs.personarray[i].phone << endl;
cout << "addr:" << abs.personarray[i].addr << endl;
cout << endl;
}
}
system("pause");
system("cls");
}
int ifexist(books abs, string name)
{
for (int i = 0; i < abs.size; i++)
{
if (abs.personarray[i].name == name)
{
return i;
}
}
return -1;
}
void delperson(books* abs)
{
string d_name;
cout << "name" << endl;
cin >> d_name;
int ret = ifexist(*abs, d_name);
if (ret == -1)
{
cout << "none" << endl;
return;
}
else
{
for (int i = ret; i < abs->size; i++)
{
abs->personarray[i] = abs->personarray[i + 1];
abs->size--;
cout << "success" << endl;
}
}
system("pause");
system("cls");
}
void findperson(books abs)
{
string f_name;
cout << "name" << endl;
cin >> f_name;
int i = ifexist(abs, f_name);
if (i == -1)
{
cout << "can't find" << endl;
}
else
{
cout << "name:" << abs.personarray[i].name << endl;
cout << "age:" << abs.personarray[i].age << endl;
cout << "sex:";
int sex = abs.personarray[i].sex;
sex == 1 ? cout << "man" << endl : cout << "woman" << endl;
cout << "phone:" << abs.personarray[i].phone << endl;
cout << "addr:" << abs.personarray[i].addr << endl;
}
}
void modifyperson(books* abs)
{
string m_name;
int where;
bool flag=true;
cout << "name" << endl;
cin >> m_name;
int i = ifexist(*abs, m_name);
if (i == -1)
{
cout << "can't find" << endl;
}
else
{
while (flag)
{
cout << "why would you want to change?" << endl;
cout << "1-name; 2-sex; 3-age; 4-phone; 5-addr; 0-none" << endl;
cin >> where;
switch (where)
{
case 1:
{
string name;
cout << "name:" << endl;
cin >> name;
abs->personarray[i].name = name;
break;
}
case 2:
{
int sex;
bool sex_flag = true;
while (sex_flag)
{
cout << "sex, 1->man, 2->woman" << endl;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarray[i].sex = sex;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
break;
}
case 3:
{
int age;
bool age_flag = true;
while (age_flag)
{
cout << "age" << endl;
cin >> age;
if (age > 0 && age < 120)
{
abs->personarray[abs->size].age = age;
break;
}
else
{
cout << "wrong input!" << endl;
}
}
break;
}
case 4:
{
int phone;
cout << "phone:" << endl;
cin >> phone;
abs->personarray[i].phone = phone;
break;
}
case 5:
{
string addr;
cout << "addr:" << endl;
cin >> addr;
abs->personarray[i].addr = addr;
break;
}
case 0:
flag = false;
break;
default:
cout << "wrong input!" << endl;
break;
}
}
}
system("pause");
system("cls");
}
void clear(books* abs)
{
abs->size = 0;
cout << "success" << endl;
system("pause");
system("cls");
}
运行截图