C++ learning basic homework submission [address book management system]

C ++ learning basic homework submission [Address Book Management System]
First introduce the system requirements: 1. Add contacts: Add contact information (name, gender, age, phone number, address) to the address book with no more than
1000 people; 2. Display contacts: 3. Delete contacts
: delete according to name
;





First, judge the number of contacts in the current address book. If it is not empty, use a for loop to output the contacts in the address book; [involving trinocular calculation ] 3. delPerson(): delete the contact. First, you need to use the isExist function to judge the contact to be deleted. If it exists, isExist returns an i














, before proceeding to the next step of deletion;
4. findPerson(): Find contacts. First use the isExist function to determine whether the contact exists, and print it if it exists, otherwise the output will find no such person;
5. modifyPerson(): delete the contact
6. clearPerson(): clear the contact

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

#define MAX 1000  // 定义的宏常量; 最大人数

// 定义  联系人 的结构体
struct Person {
    
    
	string m_Name; // 姓名
	int m_Age;
	int m_Sex; // 性别
	string m_phone;
	string m_Addr; // 地址
};
// 定义通讯录的结构体,这里还使用到了结构体嵌套
struct AddressBooks {
    
    
	//  定义一个数组personArrary,类型为Person,数组最大长度为MAX
	// 通过 abs->personArrary[i] 访问第 i 个联系人的信息;
	struct Person personArrary[MAX]; // 结构体嵌套;功能:通讯录中保存的联系人数组
	int m_Size;  // 通讯录中人员个数
};

// 菜单显示
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;
}
// 1、添加联系人
void addPerson(struct AddressBooks *abs) {
    
    

	// * abs  并不是解引用,只是用来接受传入的地址
	if (abs->m_Size == MAX) // 使用 -> 运算符 获取当前通讯录中人数
	{
    
    
		cout << "通讯录满了,无法添加" << endl;
	}
	else {
    
    
		//姓名
		string name;
		cout << "请输入 姓名:" << endl;
		cin >> name;
		abs->personArrary[abs->m_Size].m_Name = name;

		cout << "请输入性别:" << endl;
		cout << "1 -- 男" << endl;
		cout << "2 -- 女" << endl;

		// 性别
		int Sex = 0;
		while (1) {
    
    
			cin >> Sex;
			if (Sex == 1 || Sex == 2) {
    
    
				abs->personArrary[abs->m_Size].m_Sex = Sex;
				break;
			}
			else {
    
    
				cout << "输入有错误,请重新输入" << endl;
			}
		}
		//  年龄
		cout << "输入年龄" << endl;
		int age = 0;
		cin >> age;
		abs->personArrary[abs->m_Size].m_Age = age;

		// 电话
		cout << "输入电话" << endl;
		string num;
		cin >> num;
		abs->personArrary[abs->m_Size].m_phone = num;

		// 家庭地址
		cout << "输入家庭地址" << endl;
		string address;
		cin >> address;
		abs->personArrary[abs->m_Size].m_Addr = address;

		// 更新通讯录人数
		abs->m_Size++;
		cout << "添加成功" << endl;
		system("cls");
	}
}
// 2、显示联系人
void showPerson(struct AddressBooks *abs) 
{
    
    
	if (abs->m_Size == 0) 
	{
    
    
		cout << "当前记录为空" << endl;
	}
	else 
	{
    
    
		for (int i = 0;i < abs->m_Size;i++) 
		{
    
    
			cout << "姓名:" << abs->personArrary[i].m_Name << "\t";
			// 三目运算符  表达式1 ?表达式2: 表达式3 
			// 当 表达式1 为true, 返回 表达式2 ;否则返回 表达式3
			cout << "性别:" << (abs->personArrary[i].m_Sex==1 ? "男":"女") << "\t";  
			cout << "年龄:" << abs->personArrary[i].m_Age << "\t";
			cout << "电话:" << abs->personArrary[i].m_phone << "\t";
			cout << "住址:" << abs->personArrary[i].m_Addr << endl;
		}
	}
	system("pause");
	system("cls");
}

//(1).判断联系人是否存在
int isExist(AddressBooks *abs, string name) 
{
    
    
	for (int i = 0;i < abs->m_Size; i++) 
	{
    
    
		if (abs->personArrary[i].m_Name == name) 
		{
    
    
			return i;
		}
	}
	return -1;
}
//3、删除联系人
void delPerson(struct AddressBooks *abs) {
    
    
	cout << "请输入您要删除的联系人名字" << endl;
	string name;
	cin >> name;
	// (1). 判断联系人是否存在
	int ret = isExist(abs, name);
	if (ret != -1) {
    
    
	// 利用 for 循环对要删除的联系人的后面数据进行前移,执行完整个通讯录  abs->m_Size
		for (int i = ret;i < abs->m_Size;i++) {
    
    
			abs->personArrary[i] = abs->personArrary[i + 1]; // 将后面的数据依次往前移
		}
		abs->m_Size--;
		cout << "删除成功" << endl;
	}
	else {
    
    
		cout << "没有此联系人" << endl;
	}
	system("pause"); //  如果没有这一行的话,那么执行完函数 delPerson 将会直接清空
	system("cls");
}
//4、查找联系人
void findPerson(struct AddressBooks *abs) {
    
    
	cout << "请输入您要查找的联系人" << endl;
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret != -1) {
    
    
		cout << "姓名" << abs->personArrary[ret].m_Name << "\t";
		cout << "性别" << abs->personArrary[ret].m_Sex << "\t";
		cout << "年龄" << abs->personArrary[ret].m_Age << "\t";
		cout << "电话" << abs->personArrary[ret].m_phone << "\t";
		cout << "住址" << abs->personArrary[ret].m_Addr << endl;
	}
	else {
    
    
		cout << "查无此人" << endl;
	}
	system("pause");
	system("cls");
}
// 5、删除联系人
void modifyPerson(AddressBooks *abs) {
    
    
	cout << "输入您要修改的联系人" << endl;
	string name;
	cin >> name;

	int ret = isExist(abs, name);
	if (ret != -1) {
    
    
		string name;
		cout << "请输入姓名:" << endl;
		cin >> name;
		abs->personArrary[ret].m_Name = name;

		cout << "请输入性别:" << endl;
		cout << "1 --- 男" <<"\t" << "2 -- 女" << endl;
		int Sex;
		cin >> Sex;
		abs->personArrary[ret].m_Sex = Sex;

		cout << "请输入年龄:" << endl;
		int  Age;
		cin >> Age;
		abs->personArrary[ret].m_Age = Age;

		cout << "输入电话" << endl;
		string phone;
		cin >> phone;
		abs->personArrary[ret].m_phone = phone;

		cout << "输入地址" << endl;
		string addre;
		cin >> addre;
		abs->personArrary[ret].m_Addr = addre;
	}
	else 
	{
    
     
		cout << "查无此人" << endl;
	}
	system("psuse");
	system("cls");
}
// 6、联系人清空
void clearPerson(struct AddressBooks* abs) {
    
    
	cout << "该步骤是清空您通讯录的联系人" << endl;
	cout << "输入 0 表示您确认清空" << endl;

	int m_Size;
	cin >> m_Size;
	if (m_Size == 0) {
    
    
		abs->m_Size = m_Size;	
		cout << "联系人已经清空" << endl;
	}
	system("pause");
	system("cls");
}
int main() {
    
    
	AddressBooks abs;  // 定义结构体类型 AddressBooks 的变量 abs
	abs.m_Size = 0;    //初始化通讯录中人数
	int select = 0;
	while (true)
	{
    
    
		showMenu();
		cin >> select;
		switch (select)
		{
    
    
		case 1:  //添加联系人
			addPerson(&abs);
			break;
		case 2:  //显示联系人
			showPerson(&abs);
			break;
		case 3:  //删除联系人
			delPerson(&abs);
			break;
		case 4:  //查找联系人
			findPerson(&abs);
			break;
		case 5:  //修改联系人
			modifyPerson(&abs);
			break;
		case 6:  //清空联系人
			clearPerson(&abs);
			break;
		case 0:  //退出通讯录
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_56847236/article/details/131492425