C/C++教程

C++编程实例——一个通讯录的诞生

本文主要是介绍C++编程实例——一个通讯录的诞生,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实现功能:1,可以创建通讯录名单

                  2,可以删除通讯录中的成员

                  3,可以对指定成员进行查找

                  4,可以显示整个通讯录内容

                 5,可以修改通讯录成员信息

#include<iostream>
#include<string>

using namespace std;
void Showmeue()
{
	cout << "**************************" << endl;
	cout << "******** 1.创建 ***********" << endl;
	cout << "******** 2.显示 ***********" << endl;
	cout << "******** 3.删除 ***********" << endl;
	cout << "******** 4.清空 ***********" << endl;
	cout << "******** 5.查找 ***********" << endl;
	cout << "******** 6.修改 ***********" << endl;
	cout << "******** 0.退出 ***********" << endl;
}
struct Person
{
	string name;
	int age;
	string address;
	string tell;
};
#define MAX 1000
struct Personarray
{
	struct Person personarray[MAX];
	int size;
};
void Add(Personarray *ps)
{
	//判断是否超过通讯录最大值
	if (ps->size == MAX)
	{
		cout << "通讯录已满,不能再添加" << endl;
		return;
	}
	else
	{
		string name;
		int age;
		string address;
		string tell;
		cout << "请输入姓名" << endl;
		cin >> name;
		cout << "请输入年龄" << endl;
		cin >> age;
		cout << "请输入家庭住址" << endl;
		cin >> address;
		cout << "请输入电话" << endl;
		cin >> tell;
		ps->personarray[ps->size].name = name;
		ps->personarray[ps->size].age = age;
		ps->personarray[ps->size].address = address;
		ps->personarray[ps->size].tell = tell;
		cout << "创建成功" << endl;
		ps->size++;
	}
	system("pause");
	system("cls");
}
void Show(Personarray *ps)
{
	if (ps->size == 0)
	{ 
		cout << "当前无联系人,请创建" << endl;
	}
		
	else
	{
		for (int i = 0; i < ps->size; i++)
		{
			cout << "姓名 " << ps->personarray[i].name << "\t";
			cout << "年龄 " << ps->personarray[i].age << "\t";
			cout << "地址 " << ps->personarray[i].address << "\t";
			cout << "电话 " << ps->personarray[i].tell<< endl;
		}
	}
	system("pause");
	system("cls");
}
int isExist(Personarray* ps, string name)
{
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->personarray[i].name == name)
		{
			return i;
		}
		
	}
	
	return -1;
}
void Deletperson(Personarray* ps)
{
	cout << "请输入需要删除的人名" << endl;
	string name;
	cin >> name;
	int number = isExist(ps, name);
	if(number==-1)
		cout << "该联系人不存在" << endl;
	else
	{
		cout << "该联系人存在" << endl;
		for (int i = number; i < ps->size; i++)
		{
			ps->personarray[i] = ps->personarray[i + 1];
		}
		ps->size--;
		cout << "删除成功" << endl;
	}
	system("pause");
	system("cls");
}
void Findperson(Personarray* ps)
{
	cout << "请输入需要查找的人名" << endl;
	string name;
	cin >> name;
	int number = isExist(ps, name);
	if (number == -1)
		cout << "该联系人不存在" << endl;
	else
	{
		cout << "该联系人存在" << endl;
		cout << "姓名 " << ps->personarray[number].name << "\t";
		cout << "年龄 " << ps->personarray[number].age << "\t";
		cout << "地址 " << ps->personarray[number].address << "\t";
		cout << "电话 " << ps->personarray[number].tell << endl;
	}
	system("pause");
	system("cls");
}
void Modifyperson(Personarray* ps)
{
	cout << "请输入需要修改的人名" << endl;
	string name;
	cin >> name;
	int number = isExist(ps, name);
	if (number == -1)
		cout << "该联系人不存在" << endl;
	else
	{
		cout << "请输入需要修改的内容,如 地址,电话,年龄" << endl;
		string choice;
		cin >> choice;
		if (choice == "地址")
		{
			cout << "请输入需要将地址修改的内容" << endl;
			string address;
			cin >> address;
			ps->personarray[number].address = address;
		}
		if (choice == "电话")
		{
			cout << "请输入需要将电话修改的内容" << endl;
			string tell;
			cin >> tell;
			ps->personarray[number].tell = tell;
		}
		if (choice == "年龄")
		{
			cout << "请输入需要将年龄修改的内容" << endl;
			int  old;
			cin >> old;
			ps->personarray[number].age = old;
		}
		cout << "修改成功" << endl;
	}
	system("pause");
	system("cls");
}
void Clearperson(Personarray* ps)
{
	ps->size = 0;
	cout << "通讯录已清空" << endl;
	system("pause");
	system("cls");
}
int main()
{
	Personarray ps;
	ps.size = 0;
	while (true)//设置仅输入0,系统退出
	{
		Showmeue();
		int key = 0;
		cout << "请输入您的选择" << endl;
		cin >> key;

		switch (key)
		{
		case 1://创建
			Add(&ps);
			break;
		case 2://显示
			Show(&ps);
			break;
		case 3://删除
			Deletperson(&ps);
			break;
		case 4://清空
			Clearperson(&ps);
			break;
		case 5:
			Findperson(&ps);
			break;
		case 6:
			Modifyperson(&ps);
			break;
		case 0://退出
			cout << "欢迎下次使用" << endl;
			system("pause");
			return 0;
			break;
		}
	}
	system("pause");
	return 0;
}

这篇关于C++编程实例——一个通讯录的诞生的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!