通讯录管理系统 - main.c C




#include<iostream>
using namespace std;
#include<string>

#define MAX 1000

typedef struct Person//联系人结构体
{
	string name;
	int sex;//1男 2女
	int age;
	string phone;
	string address;
}P;
typedef struct Addressbooks//通讯录结构体
{
	P personArray[MAX];
	int number;
}A;
void menu()
{
	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;

}
void AddPerson(A*abs)
{
	//姓名
	string name;
	cout << "请输入姓名:"<<endl;
	cin >> name;
	abs->personArray[abs->number].name = name;
	//性别
	int sex = 0;
	cout << "请输入性别:"<<endl;
	cout << "1--男 2--女" << endl;
	while (1)
	{
		cin >> sex;
		if (sex == 1 || sex == 2)
		{
			abs->personArray[abs->number].sex = sex;
			break;
		}
		else
		{
			cout << "输入有误,请重新输入:>" << endl;
		}
	}
	//年龄
	int age = 0;
	cout << "请输入年龄:" << endl;
	cin >> age;
	abs->personArray[abs->number].age = age;
	//电话
	string phone;
	cout << "请输入电话:" << endl;
	cin >> phone;
	abs->personArray[abs->number].phone = phone;
	//住址
	string address;
	cout << "请输入住址:" << endl;
	cin >> address;
	abs->personArray[abs->number].address = address;

	abs->number++;	//人数加1
	cout << "添加成功!" << endl; 
	system("pause");
	system("cls");
}
void ShowPerson(A*abs)
{
	if (abs->number == 0)
	{
		cout << "当前记录为空!" << endl;
		system("pause");
	}
	else
	{
		for (int i = 0; i < abs->number; i++)
		{
			cout << "姓名: " << abs->personArray[i].name <<"    ";
			cout << "性别: " << (abs->personArray[i].sex == 1 ? "男" : "女") << "    ";
			cout << "年龄: " << abs->personArray[i].age << "    ";
			cout << "电话: " << abs->personArray[i].phone << "    ";
			cout << "住址: " << abs->personArray[i].address << endl;
		}
	}
	system("pause");
	system("cls");
}
int IsExist(A* abs, string name)
{
	for (int i = 0; i < abs->number; i++)
	{
		if (abs->personArray[i].name == name)
		{
			return i;
		}
	}
	return -1;
}
void DeletePerson(A* abs)
{
	string name;
	cout << "请输入要删除的联系人姓名:"<<endl; 
	cin >> name;
	int ret = IsExist(abs, name);
	if (ret != -1)
	{
		if (ret == (abs->number-1))
		{
			abs->number--;
		}
		else
		{
			for (int i = ret; i < abs->number; i++)
			{
				abs->personArray[i] = abs->personArray[i + 1];
				abs->number--;
			}
		}
		cout << "删除成功!" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		cout << "查无此人!" << endl;
		system("pause");
		system("cls");
	}
}
void FindPerson(A* abs)
{
	string name;
	cout << "请输入要查找的联系人姓名:" << endl;
	cin >> name;
	int ret = IsExist(abs, name);
	if (ret != -1)
	{
		cout << "姓名: " << abs->personArray[ret].name << "    ";
		cout << "性别: " << (abs->personArray[ret].sex == 1 ? "男" : "女") << "    ";
		cout << "年龄: " << abs->personArray[ret].age << "    ";
		cout << "电话: " << abs->personArray[ret].phone << "    ";
		cout << "住址: " << abs->personArray[ret].address << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}
void ModifyPerson(A*abs)
{
	cout << "请输入要修改的联系人姓名:" << endl;
	string name;
	cin >> name;
	int ret = IsExist(abs, name);
	if (ret != -1)
	{
		cout << "请输入新的姓名:" << endl;
		cin >> abs->personArray[ret].name;
		cout << "请输入新的性别:" << endl;
		cout << "1--男 2--女" << endl;
		cin >> abs->personArray[ret].sex;
		cout << "请输入新的年龄:" << endl;
		cin >> abs->personArray[ret].age;
		cout << "请输入新的电话:" << endl;
		cin >> abs->personArray[ret].phone;;
		cout << "请输入新的住址:" << endl;
		cin >> abs->personArray[ret].address;
		cout << "更改成功!" << endl;
	}
	else
	{
		cout << "查无此人!" << endl;
	}
	system("pause");
	system("cls");
}
void CleanPerson(A* abs)
{
	int input = 0;
	cout << "是否确认清空通讯录?" << endl;
	cout << "1--确认 其他--取消" << endl;
	cin >> input;
	if (input == 1)
	{
		abs->number = 0;
		cout << "通讯录已清空!" << endl;
	}
	else
	{
		cout << "已取成功取消!" << endl;
	}
	system("pause");
	system("cls");
}
int main()
{
	A books;
	books.number = 0;//创建通讯录并初始化人数为0
	int input = 0;
	do
	{
		menu();
		cout << "请选择功能:>";
		cin >> input;
		switch (input)
		{
		case 0://出通讯录
			cout << "欢迎下次使用!" << endl; 
			system("pause");
			return 0;
		case 1://添加联系人
			AddPerson(&books);
			break;
		case 2://显示联系人
			ShowPerson(&books);
			break;
		case 3://删除联系人
			DeletePerson(&books);
			break;
		case 4://查找联系人
			FindPerson(&books);
			break;
		case 5://修改联系人
			ModifyPerson(&books);
			break;
		case 6://清空联系人
			CleanPerson(&books);
			break;
		default:
			cout << "输入有误,请重新输入!" << endl; 
			break;
		}

	} while (input);
	return 0;
}