C++实现通讯录管理系统

C++实现通讯录管理系统

#include<iostream>
#include<string>
using namespace std;
#define MAX 1000
//设计联系人结构体
struct Contacts {
	string name;
	int Sex;
	int Age;
	string Phone;
	string Address;
};
//设计通讯录结构体
struct Addressbook {
	Contacts personarray[MAX];
	int Size; 
};
//添加联系人
void Addcontact(Addressbook* ab) {
	if (ab->Size == MAX) {
		cout << "Address book full." << endl;
		return;
	}
	else {

		cout << "Enter name" << endl;
		//string name;
		cin >> ab->personarray[ab->Size].name;
		//ab->personarray[ab->Size].name = name;
		cout << "Enter sex.1--man,2--woman" << endl;
		int sex = 0;
		while (1) {
			cin >> sex;
			if (sex == 1 || sex == 2) {
				ab->personarray[ab->Size].Sex = sex;
				break;
			}
				cout << "Reenter" << endl;
		}
		cout << "Enter age" << endl;
		cin >> ab->personarray[ab->Size].Age;
		cout << "Enter phone number" << endl;
		cin >> ab->personarray[ab->Size].Phone;
		cout << "Enter address" << endl;
		cin >> ab->personarray[ab->Size].Address;
		ab->Size++;
		cout << "Added successfully" << endl;
		system("pause");
		system("cls");
	}
}
//显示联系人
void showPerson(Addressbook* ab) {
	if (ab->Size == 0) {
		cout << "It is currently empty" << endl;
	}
	else {
		for (int i = 0; i < ab->Size; i++) {
			cout << "Name:  " << ab->personarray[i].name << 
				"  Sex:  "<< (ab->personarray[i].Sex==1?"man": "woman")<<
				"  Age:  " << ab->personarray[i].Age<< 
				"  Phone number: " << ab->personarray[i].Phone << 
				"  Address: " << ab->personarray[i].Address << endl; 
		}
	}
	system("pause");
	system("cls");
}
//判断联系人是否存在
int isExist(Addressbook* ab,string Name) {
	for (int i = 0; i < ab->Size; i++) {
		if (ab->personarray[i].name == Name) {
			return i;
		}
			
	}
	return -1;
}
//删除联系人
void deletePerson(Addressbook* ab) {
	cout << "Please enter the contact you want to delete" << endl;
	string name;
	cin >> name;
	int ret = isExist(ab, name);
	if (ret == -1) {
		cout << "There is no such person" << endl;
	}
	else {
		for (int i = ret; i < ab->Size; i++) {
			ab->personarray[ret] = ab->personarray[ret + 1];
			ab->Size--;
			cout << "Successfully deleted" << endl;
		}
	}
	system("pause");
	system("cls");
}
//查找联系人
void findPerson(Addressbook* ab) {
	string name;
	cout << "Enter name" << endl;
	cin >> name;
	int ret = isExist(ab, name);
	if (ret == -1) {
		cout << "There is no such person" << endl;
	}
	else {
		cout << "Name:  " << ab->personarray[ret].name <<
			"  Sex:  " << (ab->personarray[ret].Sex == 1 ? "man" : "woman") <<
			"  Age:  " << ab->personarray[ret].Age <<
			"  Phone number: " << ab->personarray[ret].Phone <<
			"  Address: " << ab->personarray[ret].Address << endl;
	}
	system("pause");
	system("cls");
}
//修改联系人
void modifyPerson(Addressbook* ab) {
	cout << "Enter name" << endl;
	string name;
	cin >> name;
	int ret = isExist(ab, name);
	if (ret == -1) {
		cout << "There is no such man" << endl;
	}
	else {
		cout << "Please change the name." << endl;
		cin >> ab->personarray[ret].name;
		cout << "Please change the sex,1--man,2--woman" << endl;
		while (1) {
			int sex;
			cin >> sex;
			if (sex == 1 || sex == 2) {
				ab->personarray[ret].Sex = sex;
				break;
			}
			else {
				cout << "Please reenter" << endl;
			}			
		}
		cout << "Please reenter the age" << endl;
		cin >> ab->personarray[ret].Age;
		cout << "Please change the phonenumber" << endl;
		cin >> ab->personarray[ret].Phone;
		cout << "Please change the address" << endl;
		cin >> ab->personarray[ret].Address;
		cout << "Modified successfully" << endl;
	}
	system("pause");
	system("cls");
}
//清空
void clearContacts(Addressbook* ab) {
	ab->Size = 0;
	cout << "Empty successfully" << endl;
	system("pause");
	system("cls");
}
//菜单界面
void showMenu() {
	cout << "1.Add a Contact." << endl;
	cout << "2.Show Contacts." << endl;
	cout << "3.Delete Contacts." << endl;
	cout << "4.Find Contacts." << endl;
	cout << "5.Modify Contacts." << endl;
	cout << "6.Clear Contacts." << endl;
	cout << "7.Exit address book." << endl;
}

int main() {
	Addressbook ab1;
	ab1.Size = 0;
	int select = 0;//创建用户输入的变量
	while (1) {
		showMenu();
		cin >> select;
		switch (select) {
		case 1://添加
			Addcontact(&ab1);
			break;
		case 2://显示
			showPerson(&ab1);
			break;
		case 3://删除
			deletePerson(&ab1);
			break;
		case 4://查找
			findPerson(&ab1); 
			break;
		case 5://修改
			modifyPerson(&ab1);
			break;
		case 6://清空
			clearContacts(&ab1);
			break;
		case 7://退出
			cout << "Welcom to use next time.";
			//system("pause");
			return 0;
			break;
		}
	}
	system("pause");
	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VIXeH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值