c++简单通讯录实现

#include<iostream>
#include<string>
using namespace std;
const int MAX = 1001;
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;
}
struct person
{
	string name;//姓名 
	int sex;//0:男 1:女 
	int age;//年龄 
	string phone;//电话号码 
	string addr;//地址 
};

//通讯录结构体
struct menu
{
	person array[MAX];
	int size=0;

	void addperson()
	{
		if (size == MAX)cout << "MAX" << endl;
		else
		{
			cout << "请输入姓名" << endl;
			string name;
			cin >> name;
			array[size].name = name;
			cout << "输入名字成功" << endl;
			
			int sex;
			while (1) {
			cout << "请输入性别" << endl;
			cout << "1-----男性" << endl;
			cout << "0-----女性 " << endl;
				cin >> sex;
				if (sex == 1 || sex == 0) { array[size].sex = sex; break; }
				else cout << "输入错误请重试" << endl;
			}
			//age
			cout << "请输入年龄" << endl;
			int age;
			cin >> age;
			array[size].age = age;
			cout << "请输入电话号码" << endl;
			string pnumber;
			cin >> pnumber;
			array[size].phone = pnumber;
			cout << "请输入地址" << endl;
			string addr;
			cin >> addr;
			array[size].addr = addr;
			size++;
			cout << "添加成功" << endl;
			system("pause");
			system("cls");
		}
	}
	void showperson()
	{
		if (size < 1) { cout << "暂无联系人" << endl; return; }
		else
		{
			for (int i = 0; i < size; i++)
			{
				cout << "姓名" << array[i].name << "\t";
				cout << "性别" <<(array[i].sex==1?"男":"女")<< "\t";
				cout << "年龄" << array[i].age << "\t";
				cout << "电话" << array[i].phone << "\t";
				cout << "住址" << array[i].addr<< endl;
			}
		}
		system("pause");
		system("cls");
	}
	int findname(string name)
	{
		for (int i = 0; i < size; i++)
		{
			if (array[i].name == name)return i;
		}
		return -1;
	}
	void dele(string name1)
	{
		int ret = findname(name1);
		if (ret == -1) { cout << "查无此人"; return; }
		else
		{
			for (int i = ret; i < size; i++)
				array[i] = array[i + 1];
			size--;
			cout << "删除成功" << endl;
		}
	}
	void changep(string name)
	{
		int ret = findname(name);
		if (ret == -1) { cout << "查无此人" << endl; return; }
		else
		{
			cout << "请输入姓名" << endl;
			string name;
			cin >> name;
			array[ret].name = name;
			cout << "输入名字成功" << endl;

			int sex;
			while (1) {
				cout << "请输入性别" << endl;
				cout << "1-----男性" << endl;
				cout << "0-----女性 " << endl;
				cin >> sex;
				if (sex == 1 || sex == 0) { array[ret].sex = sex; break; }
				else cout << "输入错误请重试" << endl;
			}
			//age
			cout << "请输入年龄" << endl;
			int age;
			cin >> age;
			array[ret].age = age;
			cout << "请输入电话号码" << endl;
			string pnumber;
			cin >> pnumber;
			array[ret].phone = pnumber;
			cout << "请输入地址" << endl;
			string addr;
			cin >> addr;
			array[ret].addr = addr;
			cout << "修改成功" << endl;
			system("pause");
			system("cls");

		}
	}
};

int main()
{
	menu abs;
	abs.size = 0;
	int select = 0;
	while (true)
	{
		showmenu();
		cin >> select;
		switch (select)
		{
		case 1:    //添加联系人
			abs.addperson();
			break;
		case 2:  //显示联系人
			abs.showperson();
			break;
		case 3:   //删除联系人
		{
			cout << "请输入联系人名字" << endl;
		   string name1;
		   cin >> name1;
		    abs.dele(name1);
		}
			break;
		  case 4:   //查找联系人
		  {
			  cout << "请输入联系人姓名" << endl;
		     string name;
		     cin >> name;
		      if (abs.findname(name) != -1)cout << abs.findname(name) << endl;
		  }
			break;
		case 5:              //修改联系人
		{
			cout << "请输入修改前的人名" << endl;
			string name;
		cin >> name;
		abs.changep(name);
		system("pause");
		system("cls");
		}
			break;
		case 6:           //清空联系人
		{
			abs.size = 0;
			cout << "清除成功" << endl;
			system("pause");
			system("cls");
		}
			break;
		case 0:       //退出通讯录
			cout << "欢迎继续使用" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小夕晖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值