c++项目实战之通讯录管理系统

 

界面展示,可以看到全是简单的增删改查

代码:

# include <iostream>
# include<string>
using namespace std;
struct Person
{
	string name;
	int sex;
	int age;
	string phone;
	string address;
};
#define maxp 1000
struct addressBooks
{
	struct Person parr[maxp];
	int size;
};
void addPerson(addressBooks * ads)
{
	//判断通信录是否已满
	if (ads->size == maxp)
	{
		cout << "通讯录已满,无法添加" << endl;
		return;
	}
	else
	{
		string name;
		cout << "请输入姓名" << endl;
		cin >> name;
		ads->parr[ads->size].name = name;

		cout << "请输入性别【男1,女2】" << endl;
		int sex = 0;
		while (true)
		{
			cin >> sex;
			if (sex == 1 or sex == 2)
			{
				ads->parr[ads->size].sex = sex;
				break;
			}
			
		}
		

		int age;
		cout << "请输入年龄" << endl;
		cin >> age;
		ads->parr[ads->size].age = age;

		string phone;
		cout << "请输入电话" << endl;
		cin >> phone;
		ads->parr[ads->size].phone = phone;

		string address;
		cout << "请输入家庭住址" << endl;
		cin >> address;
		ads->parr[ads->size].address = address;

		ads->size++;
		cout << "添加成功" << endl;
		system("pause"); //按任意键继续
		system("cls"); //清屏
	}

}
void showPerson(addressBooks * ads)
{
	if (ads->size == 0)
	{
		cout << "通讯录为空" << endl;
	}
	else
	{
		cout << "姓名\t" << "性别\t" << "年龄\t" 
			<< "电话\t" << "住址\t" << endl;
		for (int i = 0; i < ads->size; i++)
		{
			cout << ads->parr[i].name<<"\t";
			if (ads->parr[i].sex == 1)
			{
				cout << "男\t";
			}
			else
			{
				cout << "女\t";
			}
			cout << ads->parr[i].age << "\t";
			cout << ads->parr[i].phone << "\t";
			cout << ads->parr[i].address << "\t" << endl;
		}
	}
	system("pause");
	system("cls");
	
}
int isExist(addressBooks* ads,string name)
{
	for (int i = 0; i < ads->size; i++)
	{
		if (ads->parr[i].name == name)
		{
			return i;
		}
	}
	return -1;
}
void deletePerson(addressBooks* ads, int flag)
{
	for (int i = flag; i < ads->size-1; i++)
	{
		ads->parr[i] = ads->parr[i + 1];
	}
	ads->size--;
	cout << "删除成功" << endl;
	system("pause");
	system("cls");
}
void findPerson(addressBooks* ads, int flag)
{
	cout << "姓名\t" << "性别\t" << "年龄\t"
		<< "电话\t" << "住址\t" << endl;
	cout << ads->parr[flag].name << "\t";
	if (ads->parr[flag].sex == 1)
	{
		cout << "男\t";
	}
	else
	{
		cout << "女\t";
	}
	cout << ads->parr[flag].age << "\t";
	cout << ads->parr[flag].phone << "\t";
	cout << ads->parr[flag].address << "\t" << endl;
	system("pause");
	system("cls");
}
void modifyPerson(addressBooks * ads, int flag)
{
	cout << "姓名\t" << "性别\t" << "年龄\t"
		<< "电话\t" << "住址\t" << endl;
	cout << ads->parr[flag].name << "\t";
	if (ads->parr[flag].sex == 1)
	{
		cout << "男\t";
	}
	else
	{
		cout << "女\t";
	}
	cout << ads->parr[flag].age << "\t";
	cout << ads->parr[flag].phone << "\t";
	cout << ads->parr[flag].address << "\t" << endl;

	cout << "请输入要修改的关键字" << endl;
	string key;
	string modify;
	cin >> key;
	cout << "请输入要修改的内容" << endl;
	cin >> modify;
	if (key == "姓名")
	{
		ads->parr[flag].name = modify;
	}
	else if (key == "性别")
	{
		ads->parr[flag].sex = stoi(modify);
	}
	else if (key == "年龄")
	{
		ads->parr[flag].age = stoi(modify);
	}
	else if (key == "电话")
	{
		ads->parr[flag].phone = modify;
	}
	else if (key == "住址")
	{
		ads->parr[flag].address = modify;
	}
	else 
	{
		cout << "关键词输入错误" << endl;
	}
	system("pause");
	system("cls");
	
	
}
void cleanPerson(addressBooks* ads)
{
	ads->size = 0;
	cout << "清空成功" << endl;
	system("pause");
	system("cls");
}
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;
}

int main()
{
	// 创建一个通信录
	addressBooks ads;
	ads.size = 0;
	int select;
	string name;
	int flag;
	while (true)
	{
		showMenu();
		cin >> select;
		switch (select)
		{
		case 1:
			addPerson(&ads);
			break;
		case 2:
			showPerson(&ads);
			break;
		case 3:
			cout << "请输入要删除联系人的名字" << endl;
			cin >> name;
			flag = isExist(&ads,name);
			if (flag == -1)
			{
				cout <<name<< "不存在" << endl;
				system("pause");
				system("cls");
			}
			else
			{
				deletePerson(&ads,flag);
			}
			break;
		case 4:
			cout << "请输入要查找联系人的名字" << endl;
			cin >> name;
			flag = isExist(&ads, name);
			if (flag == -1)
			{
				cout << name << "不存在" << endl;
				system("pause");
				system("cls");
			}
			else
			{
				findPerson(&ads, flag);
			}
			break;
		case 5:
			cout << "请输入要修改联系人的名字" << endl;
			cin >> name;
			flag = isExist(&ads, name);
			if (flag == -1)
			{
				cout << name << "不存在" << endl;
				system("pause");
				system("cls");
			}
			else
			{
				modifyPerson(&ads, flag);
			}
			break;
		case 6:
			cleanPerson(&ads);
			break;
		case 0:
			cout << "欢迎下次使用" << endl;
			return 0;
			break;
		default:
			break;
		}
	}
	
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值