C++ 个人通讯录管理系统(三)

现在开始第二、三个功能:显示联系人和删除联系人。

1、显示联系人

功能描述:显示通讯录中已有的联系人信息,显示联系人实现步骤:

  • 封装显示联系人函数

  • 测试显示联系人功能

1.1 封装显示联系人函数

思路:判断如果当前通讯录中没有人员,就提示记录为空,人数大于0,显示通讯录中信息。

显示联系人代码:

//2.显示联系人信息
void showPerson(Addressbooks *abs)
{
	//判断通讯录中人数为0,如果为0,提示记录为空
	//如果不为0,显示通讯录中联系人的信息
	if (abs->m_Size == 0)
	{
		cout << "当前记录为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_Size; i++)
		{
			cout << "姓名:" << abs->personArray[i].m_Name << "\t";
			cout << "性别:" << (abs->personArray[i].m_Sex == 1 ? "男" : "女") << "\t";
			cout << "年龄:" << abs->personArray[i].m_Age << "\t";
			cout << "电话:" << abs->personArray[i].m_Phone << "\t";
			cout << "住址:" << abs->personArray[i].m_Addr << endl;
		}
	}

	system("pause");
	system("cls");
}

1.2 测试显示联系人功能

在switch case语句中,case 2 里添加:

#include <iostream>
#include <string>
 
using namespace std;
 
#define MAX 1000 //最大人数
 
int main() {
​
    int select = 0;
​
    while (true)
    {
        showMenu();
​
        cin >> select;
        
        switch (select)
        {
        case 1:  //添加联系人
            addPerson(&abs);
            break;
        case 2:  //显示联系人
            showPerson(&abs);
            break;
        case 3:  //删除联系人
            break;
        case 4:  //查找联系人
            break;
        case 5:  //修改联系人
            break;
        case 6:  //清空联系人
            break;
        case 0:  //退出通讯录
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            break;
        }
    }
​
    system("pause");
​
    return 0;
}

实际测试效果:

2、删除联系人

功能描述:按照姓名进行删除指定联系人。

删除联系人实现步骤:

  • 封装检测联系人是否存在

  • 封装删除联系人函数

  • 测试删除联系人功能

2.1 封装检测联系人是否存在

设计思路:删除联系人前,我们需要先判断用户输入的联系人是否存在,如果存在,则删除;不存在,则提示用户没有要删除的联系人。

因此我们可以把检测联系人是否存在封装成一个函数中,如果存在,返回联系人在通讯录中的位置,不存在返回-1。

检测联系人是否存在代码:

//判断是否存在查询的人员,存在返回在数组中索引位置,不存在返回-1
int isExist(Addressbooks * abs, string name)
{
	for (int i = 0; i < abs->m_Size; i++)
	{
		if (abs->personArray[i].m_Name == name)
		{
			return i;
		}
		else
		{
			//继续查找下一个联系人
		}
	}

	return -1;
}

2.2 封装删除联系人函数

根据用户输入的联系人,判断该通讯录中是否有此人。查找到进行删除,并提示删除成功;查不到提示查无此人。

//3、删除指定联系人信息
void deletePerson(Addressbooks * abs)
{
	cout << "请输入您要删除的联系人: ";
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1)
	{
		//找到此人,删除此人,并将其后的所有人依次前移
		for (int i = ret; i < abs->m_Size; i++)
		{
			abs->personArray[i] = abs->personArray[i + 1];
		}

		abs->m_Size--;   //通讯录人数减1
		cout << "删除成功!" << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}

	system("pause");
	system("cls");
}

2.3  测试删除联系人功能

在switch case 语句中,case3里添加:

#include <iostream>
#include <string>
 
using namespace std;
 
#define MAX 1000 //最大人数
 
int main() {
​
    int select = 0;
​
    while (true)
    {
        showMenu();
​
        cin >> select;
        
        switch (select)
        {
        case 1:  //添加联系人
            addPerson(&abs);
            break;
        case 2:  //显示联系人
            showPerson(&abs);
            break;
        case 3:  //删除联系人
            deletePerson(&abs);	
            break;
        case 4:  //查找联系人
            break;
        case 5:  //修改联系人
            break;
        case 6:  //清空联系人
            break;
        case 0:  //退出通讯录
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        default:
            break;
        }
    }
​
    system("pause");
​
    return 0;
}

实际测试效果:

 

 

转载:【C++零基础入门到实战就业教程|传智教育-哔哩哔哩】 https://b23.tv/joc5Gdb

“第一条”按钮的代码如下: go top thisform.command1.enabled=.f. thisform.command4.enabled=.t. thisform.command3.enabled=.t. thisform.command2.enabled=.f. thisform.dy thisform.refresh “上一条”按钮的代码如下: skip -1 thisform.command3.enabled=.t. thisform.command5.enabled=.t. if bof( ) messagebox("已经到第一个!") thisform.command2.enabled=.f. thisform.command5.enabled=.f. else thisform.dy endif thisform.refresh “下一条”按钮的代码如下: skip thisform.command5.enabled=.t. if eof( ) messagebox("已经到最后一条!") thisform.command3.enabled=.f. thisform.command5.enabled=.f. thisform.command2.enabled=.t. else thisform.command2.enabled=.t. endif thisform.dy thisform.refresh “最后一条”按钮的代码如下: go bottom thisform.command1.enabled=.t. thisform.command4.enabled=.f. thisform.command3.enabled=.f. thisform.command2.enabled=.t. thisform.dy thisform.refresh “修改”按钮的代码如下: go recno( ) thisform.txt姓名.readonly=.f. thisform.txt邮政编码.readonly=.f. thisform.txt性别.readonly=.f. thisform.txt手机.readonly=.f. thisform.txt小灵通.readonly=.f. thisform.txt住宅电话.readonly=.f. thisform.txtqq.readonly=.f. thisform.txte_mail.readonly=.f. thisform.txt办公电话.readonly=.f. thisform.txt家庭地址.readonly=.f. thisform.txt备注.readonly=.f. thisform.command6.enabled=.t. “保存”按钮的代码如下: if empty(xm) messagebox("姓名不能为空!") else replace 通讯录.家庭地址 with jtdz; 通讯录.姓名 with xm,通讯录.办公电话 with bgdh; 通讯录.性别 with xb,通讯录.qq with qq; 通讯录.e_mail with email,; 通讯录.住宅电话 with zzdh,通讯录.备注 with bz; 通讯录.邮政编码 with yzbm,通讯录.手机 with sj; 通讯录.小灵通 with xlt endif thisform.txt姓名.readonly=.t. thisform.txt邮政编码.readonly=.t. thisform.txt性别.readonly=.t. thisform.txt手机.readonly=.t. thisform.txt小灵通.readonly=.t. thisform.txt住宅电话.readonly=.t. thisform.txtqq.readonly=.t. thisform.txte_mail.readonly=.t. thisform.txt办公电话.readonly=.t. thisform.txt家庭地址.readonly=.t. thisform.txt备注.readonly=.t. thisform.command6.enabled=.f. “退出”按钮的代码如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值