通讯录c++基础版

通讯录c++基础版,第1张

#include
#include

using namespace std;
//输出菜单函数
void printf()
{
	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;
}

struct person//联系人结构体
{
	string m_name;
	int m_phone;
	int m_age;
	int m_sex;
	string m_addr;
};
#define max 1000
struct addressbooks//通讯录结构体
{
	struct person addressarr[max];
	int m_size;
};


//功能1函数(添加联系人)
void addperson(struct addressbooks* abs)
{
	//判断通讯录是否已满
	if (abs->m_size==max)
	{
		cout << "抱歉通讯录已满" << endl;
		return;
	}
	else {
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		abs->addressarr[abs->m_size].m_name = name;

		int sex;
		cout << "请输入性别" << endl;
		cout << "1-------男" << endl << "2-------女";
		while (true)
		{
			cin >> sex;
			if (sex == 1 || sex == 2)
			{
				abs->addressarr[abs->m_size].m_sex = sex;
				break;
			}
			cout << "输入有误请重新输入" << endl;
		}

		int age;
		cout << "请输入年龄" << endl;
		cin >> age;
		abs->addressarr[abs->m_size].m_age = age;

		int phone;
		cout << "请输入电话号码" << endl;
		cin >> phone;
		abs->addressarr[abs->m_size].m_phone = phone;

		string addr;
		cout << "请输入联系人地址" << endl;
		cin >> addr;
		abs->addressarr[abs->m_size].m_addr = addr;
		abs->m_size++;
		cout << "恭喜您录入成功" << endl;
		system("pause");//请按任意键继续 
		system("cls");//清屏
	}
}

//展示通讯录函数
void showperson(struct addressbooks* abs)
{

	if (abs->m_size == 0)
	{
		cout << "联系人名单为空" << endl;
	}
	else
	{
		for (int i = 0; i < abs->m_size; i++)
		{
			cout <<"名字" << abs->addressarr[i].m_name << endl;
			cout <<"性别" << abs->addressarr[i].m_sex << endl;
			cout << "年龄" << abs->addressarr[i].m_age << endl;
			cout << "电话号码" << abs->addressarr[i].m_phone << endl;
			cout << "家庭住址" << abs->addressarr[i].m_addr << endl;
		}
	}
	system("pause");//请按任意键继续 
	system("cls");//清屏
}

//联系人是否存在如果存在返回联系人数组下标编号,不存在返回-1;
int isexist(addressbooks* abs,string name)//参数1通讯录,参数2对比的名字
{
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->addressarr[i].m_name == name)
		{
			return i;
		}
	}
	return -1;
}

//删除联系人函数封装
void delet(addressbooks* abs)
{
	cout << "请输入您要删除的联系人姓名" << endl;
	string name;
    cin >> name;
	int ren=isexist(abs, name);
	if (ren == -1)
	cout << "查无此人" << endl;
	else
	{
		for (int i = ren; i < abs->m_size; i++)
		{
			abs->addressarr[i] = abs->addressarr[i+1];
		}
		abs->m_size--;
		cout << "删除成功" << endl;
	}
	system("pause");
	system("cls");
}

//查找联系人函数
void find(addressbooks* abs)
{
	cout << "输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	//判断联系人是否存在
	   if (isexist(abs, name) == -1)
		cout << "查无此人" << endl;
	   else
	   {
		  
		   cout <<"名字:" << abs->addressarr->m_name << "\t";
		   cout <<"性别:" << abs->addressarr->m_sex << "\t";
		   cout <<"年龄:" << abs->addressarr->m_age << "\t";
		   cout <<"电话:" << abs->addressarr->m_phone << "\t";
		   cout <<"住址:" << abs->addressarr->m_addr << endl;
	   }
	   system("pause");
	   system("cls");
}

//修改联系人函数
void change(addressbooks* abs)
{
	cout << "请输入您要修改的联系人" << endl;
	string name;
	cin >> name;
	int ren = isexist(abs, name);
	if (ren == -1)
		cout << "查无此人" << endl;
	else
	{
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		abs->addressarr[ren].m_name = name;

		int age;
		cout << "请输入年龄" << endl;
		cin >> age;
		abs->addressarr[ren].m_age = age;

		int phone;
		cout << "请输入电话号码" << endl;
		cin >> phone;
		abs->addressarr[ren].m_phone = phone;

		string addr;
		cout << "请输入联系人地址" << endl;
		cin >> addr;
		abs->addressarr[ren].m_addr = addr;

		int sex;
		cout<<"请输入性别"<> sex;
			if (sex == 1 || sex == 2)
			{
				abs->addressarr[ren].m_sex = sex;
				break;
			}
			cout << "输入有误请重新输入" << endl;
		}
	}
	system("pause");
	system("cls");
}

void clear(addressbooks*abs)
{
	abs->m_size = 0;
	cout << "联系人以清空" << endl;
	system("pause");
	system("cls");
}

int main()
{

	struct addressbooks abs;
	abs.m_size = 0;
	while (true)
	{
		printf();
		int a = 0;
		cin >> a;
		switch (a) {
		case 1:     addperson(&abs);                               //添加联系人
			break;

		case 2:           showperson(&abs);                         //显示联系人 
			break;

		case 3:                      //删除联系人
			delet(&abs);
		//{cout << "请输入您要删除的联系人姓名" << endl;
		//string name;
		//cin >> name;
		//if (isexist(&abs, name) == -1)
		//	cout << "查无此人" << endl;
		//else
		//	cout << "找到此人" << endl;
		//}
			break;

		case 4:                                    //查找联系人
			find(&abs);
			break;

		case 5:                                    //修改联系人
			change(&abs);
			break;

		case 6:                                    //清空联系人
			clear(&abs);
			break;

		case 0:                                   //返回
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;

		default:
			break;
		}
	}
	system("pause");
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/569782.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存