黑馬程序員匠心之做-通信錄

 

#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
void showMenu()
{
	cout << "***********************" << endl;
	cout << "*****一、添加聯繫人*****" << endl;
	cout << "*****二、顯示聯繫人*****" << endl;
	cout << "*****三、刪除聯繫人*****" << endl;
	cout << "*****四、查找聯繫人*****" << endl;
	cout << "*****五、修改聯繫人*****" << endl;
	cout << "*****六、清空聯繫人*****" << 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;
}
相關文章
相關標籤/搜索