通信录系统--B站黑马程序员C++教程

1、

在这里插入图片描述
哔哩哔哩: https://www.bilibili.com/video/BV1et411b73Z?p=72

2、代码(Ubuntu、win10均可)

#include<iostream>
#include<string>
#include<vector>
#include<limits.h>  //INT_MAX
#include<algorithm> //find

using namespace std;

const int maxAddressBook = 2000;

struct ContactPerson
{
	ContactPerson(string name_ = "", string sex_ = "", int age_ = 0, int telnumber_ = 0, string address_ = "")
	{
		name = name_;
		sex = sex_;
		age = age_;
		telnumber = telnumber_;
		address = address_;
	}
	// ContactPerson():name(""),sex(""),age(0),telnumber(0),address(""){}
	string name;
	string sex;
	int age;
	long telnumber;
	string address;
	void printCP() const
	{
		std::cout << "姓名:" << name << "\t性别:" << sex << "\t年龄:" << age << "\t电话:" << telnumber << "\t住址:" << address << endl;
	}
	//用于查找
	bool operator == (const string &fname)
	{
		return (this->name == fname);
	}
	bool operator ==(const ContactPerson &cp)
	{
		return (this->name == cp.name) && (this->sex == cp.sex) && (this->age == cp.age) && (this->address == cp.address);
	}
};

/*
showMenu(),显示菜单界面
*/
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;
}

int calcuNumDigits(long long digits)
{
	int numDigits = 0;
	do
	{
		numDigits++;
		digits /= 10;
	} while (digits>0);
	return numDigits;
}
void clear()//清屏函数
{
#if defined(_WIN32)
	system("pause");
	system("cls");
#else 
	// char buf[1024]={0};
	cout << "请输入回车键继续……" << endl;
	// fgets(buf,1024,stdin);
	char c;
	cin.get();
	while ((c = getchar()) != '\n' && c != EOF);
	system("clear");

#endif
}
/*
1、增加联系人
*/
void addPerson(vector<ContactPerson> &cps)
{
	if (cps.size() == maxAddressBook)
	{
		cout << "通讯录已满!" << endl;
		clear();
		return;
	}

	string name;
	int sex;
	long long age;
	long long telnumber;
	string address;
	ContactPerson addp;
	cout << "请输入姓名:" << endl;
	cin >> name;
	addp.name = name;
	cout << "请输入性别:" << endl;
	cout << "1--男" << endl;
	cout << "2--女" << endl;
	for (size_t i = 0; i < 5; i++)
	{
		cin >> sex;
		if (sex == 1)
		{
			addp.sex = "男";
			break;
		}
		else if (sex == 2)
		{
			addp.sex = "女";
			break;
		}
		cout << "请输入性别?(1/2)" << endl;
	}

	cout << "请输入年龄:" << endl;
	for (size_t i = 0; i < 5; i++)
	{
		cin >> age;
		if ((age>INT_MAX || age<(int)0))
		{
			cout << "数字不符合要求!请输入年龄:" << endl;
			continue;
		}
		else
		{
			addp.age = age;
			break;
		}
	}

	cout << "请输入电话:" << endl;
	for (size_t i = 0; i < 5; i++)
	{
		cin >> telnumber;

		if ((telnumber<LLONG_MAX  && telnumber>0))
		{
			int nums = calcuNumDigits(telnumber);
			if (nums == 11)
			{
				addp.telnumber = telnumber;
				break;
			}
			else
			{
				cout << "电话号码长度=" << nums << " 异常!" << "请输入电话:" << endl;//按键盘方向键程序直接抽筋-.-
				continue;
			}
		}
		else
		{
			cout << "电话号码异常!" << "请输入电话:" << endl;
			continue;
		}
	}
	cout << "请输入家庭住址:" << endl;
	cin >> address;
	addp.address = address;
	cps.push_back(addp);
	cout << "添加成功~" << endl;
	clear();
	return;
}
/*
2、显示联系人
*/
void dispCP(const vector<ContactPerson> &cps)
{
	if (cps.empty())
	{
		cout << "通讯录为空!" << endl;
		clear();
		return;
	}

	for (auto cp : cps)
	{
		cp.printCP();
	}
	cout << "通讯总人数为:" << cps.size() << endl;
	clear();
	return;
}

/*
3、删除联系人
*/
void deleCP(vector<ContactPerson> &cps)
{
	if (cps.empty())
	{
		cout << "通讯录为空!" << endl;
		clear();
		return;
	}
	cout << "请输入要删除人的姓名:" << endl;
	string findName;
	cin >> findName;
	auto beg = cps.begin();//vector <ContactPerson>::iterator
	auto end = cps.end();//vector <ContactPerson>::iterator
	while (beg != end)
	{
		auto result = find(beg, end, findName);//=需要重载
		if (result != cps.end())
		{
			cout << "找到:" << findName << endl;
			result->printCP();
			cout << "确认删除(y/n): ";
			char yesorno = 'n';
			cin >> yesorno;
			if (yesorno == 'y')
			{
				beg =cps.erase(result);//这种写法Ubuntu和win10都不报错
				cout << "已删除" << endl;
				//接着往后查找
				//beg = result;//这种写法Ubuntu不报错,win10报错vector iterators incompatible
				end = cps.end();
			}
			else
			{
				//接着往后查找
				beg = result + 1;
			}
			// beg->printCP();
			continue;
		}
		else
		{
			cout << "查无此人!" << endl;
			break;
		}
	}
	clear();
	return;
}
/*
4、查找联系人
*/
void findCP(vector<ContactPerson> &cps)
{
	if (cps.empty())
	{
		cout << "通讯录为空!" << endl;
		clear();
		return;
	}
	cout << "请输入要查找的姓名:" << endl;
	string findName;
	cin >> findName;
	auto beg = cps.begin();//vector <ContactPerson>::iterator
	auto end = cps.end();//vector <ContactPerson>::iterator
	while (beg != end)
	{
		auto result = find(beg, end, findName);//=需要重载
		if (result != cps.end())
		{
			result->printCP();
			//接着往后查找
			beg = result + 1;
			// beg->printCP();
			continue;
		}
		else
		{
			cout << "查无此人!" << endl;
			break;
		}
	}
	clear();
	return;
}

/*
5、修改联系人
*/
void reviseCP(vector<ContactPerson> &cps)
{
	if (cps.empty())
	{
		cout << "通讯录为空!" << endl;
		clear();
		return;
	}
	cout << "请输入要修改人的姓名:" << endl;
	string findName;
	cin >> findName;
	auto beg = cps.begin();//vector <ContactPerson>::iterator
	auto end = cps.end();//vector <ContactPerson>::iterator
	int numofFindName = 0;
	while (beg != end)
	{
		auto result = find(beg, end, findName);//=需要重载
		if (result != cps.end())
		{
			++numofFindName;
			cout << "开始修改" << numofFindName << "(第" << numofFindName << "个)" << endl;
			cout << "修改前:" << endl;
			result->printCP();
			while (1)
			{
				int select;
				cout << "输入要修改的选项:" << endl << "\t1、姓名\n" << "\t2、性别\n" << "\t3、年龄\n" << "\t4、电话\n" << "\t5、住址\n" << "\t6、修改完毕" << endl;
				cin >> select;
				if (select == 6)
				{
					break;
				}
				switch (select)
				{
				case 1:
				{
					string newName;
					cout << "输入修改后的姓名:";
					cin >> newName;
					result->name = newName;
					break;
				}
				case 2:
				{
					string newSex;
					cout << "输入修改后的性别(男/女):";
					cin >> newSex;
					result->name = newSex;
					break;
				}
				case 3:
				{
					cout << "输入修改后的年龄:";
					long newAge;
					cin >> newAge;
					if ((newAge>INT_MAX || newAge<(int)0))
					{
						cout << "数字不符合要求!无法修改" << endl;
						break;
					}
					else
					{
						result->age = newAge;
						break;
					}
					break;
				}
				case 4:
				{
					long long newtel;
					cout << "输入修改后的电话:";
					cin >> newtel;
					if ((newtel<LLONG_MAX  && newtel>0))
					{
						int nums = calcuNumDigits(newtel);
						if (nums == 11)
						{
							result->telnumber = newtel;
							break;
						}
						else
						{
							cout << "电话号码长度=" << nums << " 异常!" << "无法修改" << endl;
							break;
						}
					}
					else
					{
						cout << "电话号码异常!" << "无法修改" << endl;
						break;
					}
				}
				case 5:
				{
					string newAddre;
					cout << "输入修改后的地址:";
					cin >> newAddre;
					result->address = newAddre;
					break;
				}
				default:
				{
					cout << "错误输入" << endl;
					break;
				}
				}//end switch  
			}// endl while
			cout << "修改后:" << endl;
			result->printCP();
			//接着往后查找
			beg = result + 1;
			continue;
		}// end if
		else
		{
			cout << "查无此人!" << endl;
			break;
		}
	}//end while
	clear();
	return;
}

/*
6、清空联系人
*/
void clearCP(vector<ContactPerson> &cps)
{
	if (cps.empty())
	{
		cout << "通讯录为空!" << endl;
		clear();
		return;
	}
	cout << "确认删除(y/n)" << endl;
	char yesorno = 'n';
	for (size_t i = 0; i < 5; i++)
	{
		cin >> yesorno;
		if (yesorno == 'y')
		{
			cps.clear();
			cout << "通讯录已清空!" << endl;
			clear();
			return;
		}
		cout << "确认删除?(y/n)" << endl;
	}
	cout << "times out" << endl;
	clear();
	return;
}

int main()
{
	int select = 0;
	vector<ContactPerson> CP;
	ContactPerson p1("han", "男");
	ContactPerson p2("han", "女");
	CP.push_back(p1);
	CP.push_back(p1);
	CP.push_back(p2);
	CP.push_back(p2);
	while (true)
	{
		showMenu();
		cout << "输入数字,选择菜单: ";
		cin >> select;
		switch (select)
		{
		case 0:     //0、退出通讯录
			cout << "bye~" << endl;
			return 0;
			break;
		case 1:     //1、增加联系人
			cout << "增加联系人" << endl;
			addPerson(CP);
			break;
		case 2:     //2、显示联系人
			cout << "显示联系人" << endl;
			dispCP(CP);
			break;
		case 3:     //3、删除联系人
			deleCP(CP);
			break;
		case 4:     //4、查找联系人
			findCP(CP);
			break;
		case 5:     //5、修改联系人
			reviseCP(CP);
			break;
		case 6:     //6、清空联系人
			clearCP(CP);
			break;
		default:
			break;
		}
	}
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值