C++实现通讯录程序

        在学习中我们最重要的就是不断的锻炼自己,所以就自己编写一个通讯录程序来巩固知识点(不涉及文件处理),只能说全程简单易懂就不过多的解释,完全为了自我锻炼,也可以拿来造福人类

#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>


using namespace std;

struct People
{
	string name;
	string sex;
	int age;
	string phone;
	string add;
	bool operator == (const string& n)
	{
		return (this->name == n);
	}
};
void Muen();
void Print();
bool cmp(struct People a, struct People b)
{
	return a.name > b.name;
}

vector<People>v;
int main ()
{
	int ret = 0;
	do
	{
		Muen();
		cout << "请输入" << endl;
		cin >> ret;
		if (ret == 1)
		{
			cout << "请输入名字 性别 年龄 电话 地址" << endl;
			People a;
			cin >> a.name >> a.sex >> a.age >> a.phone >> a.add;
			v.push_back(a);
			cout << "添加成功" << endl;
			Print();
		}
		else if (ret == 2)
		{
			cout << "输入要删除的人的名字" << endl;
			string x;
			cin >> x;
			if (find(v.begin(), v.end(), x) == v.end())
			{
				cout << "查找不到这个人" << endl;
			}
			else
			{
				v.erase(find(v.begin(), v.end(), x), find(v.begin(), v.end(), x)+1);
				cout << "删除成功" << endl;
				Print();
			}
		}
		else if (ret == 3)
		{
			cout << "输入要找的人的名字" << endl;
			string xx;
			cin >> xx;
			if (find(v.begin(), v.end(), xx) == v.end())
			{
				cout << "查找不到这个人" << endl;
			}
			else
			{
				cout << "找到了" << endl;
				cout << find(v.begin(), v.end(), xx)->name << ' ' << find(v.begin(), v.end(), xx)->sex << ' ';
				cout << find(v.begin(), v.end(), xx)->age << ' ' << find(v.begin(), v.end(), xx)->phone << ' ' << find(v.begin(), v.end(), xx)->add << endl;
			}
		}
		else if (ret == 4)
		{
			cout << "要找的人的名字" << endl;
			string aa;
			cin >> aa;
			if (find(v.begin(), v.end(), aa) == v.end())
			{
				cout << "查找不到这个人" << endl;
			}
			else
			{
				cout << "找到了" << endl;
				cout << "请输入名字 性别 年龄 电话 地址" << endl;
				cin >> find(v.begin(), v.end(), aa)->name >> find(v.begin(), v.end(), aa)->sex;
				cin >> find(v.begin(), v.end(), aa)->age >>find(v.begin(), v.end(), aa)->phone >> find(v.begin(), v.end(), aa)->add;
				Print();
			}
		}
		else if (ret == 5)
		{
			sort(v.begin(), v.end(), cmp);
			Print();
		}
		else if (ret == 6)
		{
			v.clear();
			cout << "清除成功" << endl;
		}
		else if (ret == 0)
		{
			cout << "退出" << endl;
		}
		else
		{
			cout << "输入错误重新输入" << endl;
		}
	} while (ret);
	

	return 0;
}



void Muen()
{
	cout << "*******************************************" << endl;
	cout << "*****1.增加*******2.删除*******************" << endl;
	cout << "*****3.查找*******4.修改*******************" << endl;
	cout << "*****5.排序*******6.清除所有人*************" << endl;
	cout << "*****0.退出********************************" << endl;

}

void Print()
{
	int i = 0;
	for (i = 0; i < v.size(); i++)
	{
		cout << v[i].name << ' ' << v[i].sex << ' ' << v[i].age << ' ' << v[i].phone << ' ' << v[i].add << endl;
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值