这个项目也是本人刚学习c++时的第一个起手练习,直接没有难度。在这里是用的c风格的方式码的代码
不过问题不大。
具体操作都在代码注有解释,话不多说上代码~
#include<iostream>
#include<string>
using namespace std;
#define MAX 100 //联系人个数最大值
void showmend() //菜单类的封装
{
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;
}
//设计联系人结构体
struct person
{
string m_name;//姓名
int m_sex;//性别
int m_age;//年龄
string m_phone;//电话
string m_addr;//住址
};
//设计通讯录结构体
struct addressbooks
{
struct person personarrs[MAX];//保存的联系人数组
int m_size;//通讯录中当前人员的个数
};
//添加联系人:
void addperson(addressbooks* abs)
{
if (abs->m_size >= MAX) //如果人数超过存储最大值
{
cout << "联系人已满无法再存入" << endl;
return;
}
else //否则添加具体联系人
{
string name;
cout << "请输入姓名:" << endl;
cin >> name;
abs->personarrs[abs->m_size].m_name = name; //将名字保存到通讯录数组中
int sex;
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
cin >> sex;
while (true)
{
if (sex == 1 || sex == 2)
{
abs->personarrs[abs->m_size].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->personarrs[abs->m_size].m_age = age;
cout << "请输入电话" << endl;
string phone;
cin >> phone;
abs->personarrs[abs->m_size].m_phone = phone;
cout << "请输入家庭地址" << endl;
string address;
cin >> address;
abs->personarrs[abs->m_size].m_addr = address;
//更新通讯录中的人数:
abs->m_size++;
cout << "添加成功" << endl;
//清屏操作:
system("pause");
system("cls");
}
}
//显示联系人:
void showperson(addressbooks* abs)
{
//判断通讯录中的人员是否为0:提示为空,如不为0,显示联系人
if (abs->m_size == 0)
{
cout << "当前记录为空" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名" << abs->personarrs[i].m_name << "\t";//制表符能让信息显示的更规范
cout << "性别" << (abs->personarrs[i].m_sex == 1 ? "男" : "女") << "\t";
cout << "年龄" << abs->personarrs[i].m_age << "\t";
cout << "电话" << abs->personarrs[i].m_phone << "\t";
cout << "地址" << abs->personarrs[i].m_addr << endl;
}
}
//清屏操作:
system("pause");
system("cls");
}
//删除联系人:
1//检查联系人是否在,如在则返回其所在数组中的位置,不在则返回-1
int isExis(addressbooks* abs, string name) //参数1是通讯录,参数2是姓名
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personarrs[i].m_name == name)//找到了这个名字
{
return i;//返回这个值得下标
}
else
{
return -1;//没找到
}
}
}
//2
void hrp(addressbooks* abs)
{
cout << "请输入你要删除人的姓名" << endl;
string name;
cin >> name;
int root = isExis(abs, name);//传入isExis的类容,要么root为-1及未找到,root为1则找到了
if (root != -1)
{
for (int i = root; i < abs->m_size; i++)删除操作
{
abs->personarrs[i] = abs->personarrs[i + 1];//数据前移覆盖找到的值等价于删除!
}
abs->m_size--;//更新通讯录中的人数
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
//清屏操作:
system("pause");
system("cls");
}
//查找联系人:
void findhrp(addressbooks* abs)
{
cout << "请输出你要查找的联系人" << endl;
string name;
cin >> name;//判断联系人是否存在于通讯录中
int root = isExis(abs, name);//同删除中查找一样
if (root != -1)
{
cout << "姓名" << abs->personarrs[root].m_name << "\t";
cout << "性别" << abs->personarrs[root].m_sex << "\t";
cout << "年龄" << abs->personarrs[root].m_age << "\t";
cout << "电话" << abs->personarrs[root].m_phone << "\t";
cout << "住址" << abs->personarrs[root].m_addr << endl;
}
else
{
cout << "查无此人" << endl;
}
//清屏操作:
system("pause");
system("cls");
}
//修改联系人:
void xiuhrp(addressbooks* abs)
{
cout << "请输入你要修改的联系人" << endl;
string name;
cin >> name;
int root = isExis(abs, name);
if (root != -1)
{
//如果找到了便提示修改新的信息
cout << "请输入新姓名" << endl;
cin >> name;
abs->personarrs[root].m_name = name;//将新名字信息替换为通讯录中的名字信息
cout << "请输入性别:" << endl;
cout << "1---男" << endl;
cout << "2---女" << endl;
int sex = 0;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarrs[root].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->personarrs[root].m_age = age;
cout << "请输入电话" << endl;
string phone;
cin >> phone;
abs->personarrs[root].m_phone = phone;
cout << "请输入家庭地址" << endl;
string address;
cin >> address;
abs->personarrs[root].m_addr = address;
cout << "修改成功!" << endl;
}
else
{
cout << "查无此人" << endl;
}
//清屏操作:
system("pause");
//按任意键结束
system("cls");
//清屏
}
//清空联系人:
void wjjs(addressbooks* abs)
{
cout << "是否清空通讯录?" << endl;
int root = 0;
cout << "1---是" << endl;
cout << "2---否" << endl;
while (true)
{
cin >> root;
if (root == 1)
{
//将当前联系人数量置为零,做逻辑上的清空操作
//只能做到掩饰“显示通讯录”人数,若做修改联系人.查找联系人的操作则会导致系统崩溃!!!
abs->m_size = 0;
cout << "通讯录已清空" << endl;
break;
}
else(root == 2);
{
cout << "已取消清空" << endl;
break;
}
}
//清屏操作:
system("pause");
system("cls");
}
int main()
{
//创建通讯录结构体变量:
addressbooks abs;
abs.m_size = 0;//初始化通讯录人员个数为0
int select = 0;//创建用户选择的变量!如添加,显示等
//菜单的调用:
while (true)
{
showmend();
cin >> select;//输入要选择的项目,如添加,显示等
switch (select)
{
case 1:
addperson(&abs);//利用地址传递可以修饰形参;
break;
case 2:
showperson(&abs);
break;
case 3:
hrp(&abs);
break;
case 4:
findhrp(&abs);
break;
case 5:
xiuhrp(&abs);
break;
case 6:
wjjs(&abs);
break;
case 0:cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
return 0;
}
运行结果如下:
很简单的一个小项目,目的也只是为了巩固学习到的基础知识。