The ingenuity of dark horse programmers-address book

 

#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
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;
}
struct person {
	string name;
	int m_sex;
	int m_age;
	string m_phone;
	string address;
};
struct contact {
	person per[MAX];
	int max_size;
};
//添加联系人
void addperson(contact *con)
{
	if (con->max_size > 100)
	{
		cout << "联系人已满,不能添加" << endl;
    }
	else
	{
		//请输入姓名;
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		con->per[con->max_size].name = name;
		//请输入性别;
		while (true)
		{
			int sex;
			cout << "1----男" << endl;
			cout << "2----女" << endl;
			cin >> sex;

			if (sex == 1 || sex == 2)
			{
				con->per[con->max_size].m_sex = sex;
				break;
			}
			else
			{
				cout << "输入有误,请重新输入" << endl;
			}
		}

		//请输入年龄;
		int age;
		cout << "请输入年龄:" << endl;
		cin >> age;
		con->per[con->max_size].m_age = age;
		//请输入电话;
		string tel;
		cout << "请输入电话:";
		cin >> tel;
		con->per[con->max_size].m_phone = tel;
		//请输入地址;
		string add;
		cout << "请输入地址:";
		cin >> add;
		con->per[con->max_size].address = add;
		con->max_size++;
		cout << "添加成功" << endl;
		system("pause");
		system("cls");

	}
		
}
//显示联系人
void showperson(contact*con)
{
	if (con->max_size == 0)
	{
		cout << "联系人为空" << endl;
	}
	else
	{
		for (int i = 0; i < con->max_size; i++)
		{
			cout << "姓名:" << con->per[i].name << '\t';
			cout << "性别:" << con->per[i].m_sex << '\t';
			cout << "年龄:" << con->per[i].m_age << '\t';
			cout << "电话:" << con->per[i].m_phone << '\t';
			cout << "地址:" << con->per[i].address << endl;
		}
		system("pause");
		system("cls");
	}
}
//查找联系人
int isExit(contact* con, string name)
{
	for (int i = 0; i < con->max_size; i++)
	{
		if (con->per[i].name == name)
		{
			return i;
		}
	}
	return -1;
}
void del(contact* con)
{
	cout << "请输入要删除的人:" << endl;
	string name;
	cin >> name;
	if (isExit(con, name) != -1)
	{ 
		int ret = isExit(con, name);
		for (int i = ret; i < con->max_size; i++)
		{
			con->per[i] = con->per[i + 1];
		}
		con->max_size--;
		cout << "删除成功" << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
void findPerson(contact*con)
{
	cout << "请输入要查找的人:" << endl;
	string name;
	cin >> name;
	if (isExit(con, name) != -1)
	{
		int ret = isExit(con, name);
		cout << "姓名:" << con->per[ret].name << "\t电话" << con->per[ret].m_phone << endl;
	}
	else
	{
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}

int main()
{
	int select = 0;
	contact con;
	con.max_size = 0;
	while (true)
	{   
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1://添加联系人
			addperson(&con);
			break;
		case 2://显示联系人
			showperson(&con);
			break;
		case 3://删除联系人
			del(&con);
			break;
		case 4://查找联系人
			findPerson(&con);
			break;
		case 5://修改
			break;
		case 6://清空
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	showMenu();
	system("pause");
	return 0;
}

 

Guess you like

Origin blog.csdn.net/yyyllla/article/details/109321657