C ++ implementation simple address book

Function and effect diagram

Functional
address book contacts to achieve search, add contacts, delete contacts, modify contact information
renderings
Here Insert Picture Description

Implementation code

Creating structures

typedef struct News 
{
	string name;		//姓名
	string sex;			//性别
	string address;		//地址
	int tell;			//电话
}News;

Find Contact

int search(News *a) 
{
	string name;
	int x;
	cout << "请输入要查找人的姓名:" << endl;
	cin >> name;
	for (int i = 0; i <= number; i++)
	{
		if (a[i].name == name)								//查找成功
		{
			cout << "查找结果为:" << endl;
			cout <<"姓名为:"<< a[i].name <<"  性别为:"<< a[i].sex << "  地址为:" << a[i].address << "  电话为:" << a[i].tell << endl;
			cout << "按任意数字键后,按enter结束" << endl;
			cin >> x;
			system("cls");    //清屏,返回主界面
			return 1;
		}

	}
	cout << "没有该联系人,查找结束" << endl;				//查找失败
	cout << "按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");    //清屏,返回主界面
	return -1;
}

add contact

void  Add(News *a)
{
	int x;
	int i = number;
	int tell1;
	cout << "请输入要插入联系人的姓名:" << endl;   //输入添加联系人的信息
	cin >> a[i].name;
	cout << "请输入要插入联系人的性别:" << endl;
	cin >> a[i].sex;
	cout << "请输入要插入联系人的地址:" << endl;
	cin >> a[i].address;
	cout << "请输入要插入联系人的电话:" << endl;
	cin >> a[i].tell;
	number++;
	cout << "信息已经保存,按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");     //清屏,返回主界面

}

Delete Contact

int Delete(News *a)
{
	int i = number;
	int x;
	string name;
	cout<<"请输入要删除联系人的姓名"<<endl;
	cin >> name;
	for (int i = 0; i <= number; i++)
	{
		if (a[i].name == name)						//根据姓名查找需要删除的联系人
		{
			number--;
			cout << "删除成功!" << endl;
			cout << "按任意数字键后,按enter结束" << endl;
			cin >> x;
			system("cls");    //清屏,返回主界面

			return 1;
		}
	}
	cout << "联系人不存在" << endl;
	cout << "按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");    //清屏,返回主界面

	return 0;
}

Modify contact information

int Alter(News *a)
{
	int x;
	int i = number;
	string oldname;
	string newname;
	string newsex;
	string newaddress;
	int newtell;
	cout<<"请输入需要修改联系人的姓名:"<<endl;
	cin >> oldname;
	for (int i = 0; i <= number; i++)
	{
		if (a[i].name == oldname)
		{
			cout<<"请输入该联系人新信息"<<endl;
			cout << "请输入联系人的姓名:" << endl;   //输入联系人的新信息
			cin >> newname;
			a[i].name = newname;
			cout << "请输入联系人的性别:" << endl;
			cin >> newsex;
			a[i].sex = newsex;
			cout << "请输入联系人的地址:" << endl;
			cin >> newaddress;
			a[i].address = newaddress;
			cout << "请输入联系人的电话:" << endl;
			cin >> newtell;
			a[i].tell = newtell;
			cout << "联系人信息已经更改!" << endl;
			cout << "按任意数字键后,按enter结束" << endl;
			cin >> x;
			system("cls");   //清屏,返回主界面
			return 1;
		}
	}
	cout << "联系人不存在" << endl;
	cout << "按任意数字键后,按enter结束" << endl;
	cin >> x;
	system("cls");    //清屏,返回主界面
	return -1;
}

Service Selection

int choose()
{
	int choice;
	cout << "				**********简易通讯录**********" << endl;
	cout << "					1.按姓名查找联系人" << endl;
	cout << "					2.添加联系人" << endl;
	cout << "					3.删除联系人" << endl;
	cout << "					4.修改联系人" << endl;
	cout << "					5.退出" << endl;
	cout << "				  请输入数字1-5选择服务" << endl;
	cin >> choice;
	system("cls");    //清屏
	return choice;
}

Test function

int main()
{
	News a[50];					//初始化允许最大输入联系人为50
	int choice = 0;
	while (choice != 5)
	{
		choice = choose();
		switch (choice)			//选择服务
		{
		case 1: search(a); break;
		case 2: Add(a); break;
		case 3: Delete(a); break;
		case 4: Alter(a); break;
		case 5: exit(0); break;
		default:break;
		}

	}

[Note] In this program, contact information is stored in an array inside, so each time you open the address book is a new (temporary file is not written inside), so called simple address book

Released nine original articles · won praise 10 · views 3553

Guess you like

Origin blog.csdn.net/weixin_44480968/article/details/104402537