• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

黑马程序员c++通讯录

互联网 diligentman 4小时前 1次浏览

以下代码为本人所作,因水平所限及时间仓促,疏忽之处在所难免。

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
#define max 100
struct person
{
	string name;
	string sex;
	string address;
	int phonenumber;
	int age;
};
struct phonebook
{
	person personarr[max];
	int personnum;

};
void showmenu()
{
	cout << "请输入您要选择的功能" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |  FOREGROUND_GREEN | FOREGROUND_BLUE);
	cout << "1-添加联系人" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
	cout << "2-显示所有联系人" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
	cout << "3-删除联系人" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
	cout << "4-查找联系人" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);//设置蓝色
	cout << "5-修改联系人" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);//设置绿色
	cout << "6-清空通讯录" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);//设置红色
	cout << "7-退出通讯录" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED|FOREGROUND_BLUE);//设置红色
}
void addperson(phonebook* p)
{
	if (p->personnum == max)
	{
		cout << "联系人已满" << endl;
		return;
	}
	else
	{
		string name;
		cout << "请输入您要添加的联系人的名字" << endl;
	
		cin >> name;
		p->personarr[p->personnum].name = name;
		cout << "请输入您要添加的联系人的地址" << endl;
		string address;
		cin >> address;
		p->personarr[p->personnum].address = address;
		cout << "请输入您要添加的联系人的年龄" << endl;
		int age;
		cin >> age;
		p->personarr[p->personnum].age = age;
		cout << "请输入您要添加的联系人的电话号码" << endl;
		int phonenumber;
		cin >> phonenumber;
		p->personarr[p->personnum].phonenumber = phonenumber;
		cout << "请输入您要添加的联系人的性别" << endl;
		string sex;
		cin >> sex;
		p->personarr[p->personnum].sex = sex;
		p->personnum++;
	}
	cout << "输入完成" << endl;
	system("pause");
	system("cls");
}
void showphonebook(phonebook* p)
{
	if (p->personnum == 0)
	{
		cout << "联系人为空" << endl;
		system("pause");
		system("cls");
	}
	else
	{
		for (int i = 0; i < p->personnum; i++)
		{
			cout << "姓名:" << p->personarr[i].name << "t";
			cout << "年龄:" << p->personarr[i].age << "t";
			cout << "住址:" << p->personarr[i].address << "t";
			cout << "电话:" << p->personarr[i].phonenumber << "t";
			cout << "性别:" << p->personarr[i].sex << "t";
			cout << endl;
		}
		system("pause");
		system("cls");
	}
}
int  isexit(phonebook*p)
{
	if (p->personnum == 0)
	{
		cout << "通讯录为空" << endl;
		system("pause");
		system("cls");
		return -1;

	}
	else 
	{  
		string name;
		cout << "请输入要删除的名字" << endl;
		cin >> name;
		for (int i = 0; i < p->personnum; i++)
		{
			if (name == p->personarr[i].name)
			{
				return i;
			}
		}
	}
}
void deleteinperson(phonebook* p,int ssd)
{
	if (ssd >= 0)
	{
		
		for(int i=ssd;i<p->personnum;i++)
		{ 
			p->personarr[i] = p->personarr[i + 1];
		}
		cout << "删除成功" << endl;
		system("pause");
		system("cls");
	}
	
}
void searchinperson(phonebook*p)
{
	string name;
	cout << "请输入要查找的联系人名字:" << endl;
	cin >> name;

	for (int i = 0; i <= p->personnum; i++)
	{
		if (p->personarr[i].name == name)
		{
			cout << "以下为联系人信息" << endl;
			cout << p->personarr[i].address << 't'; cout << p->personarr[i].age << 't'; cout << p->personarr[i].name << 't'; cout << p->personarr[i].phonenumber << 't'; cout << p->personarr[i].address << 't'; cout << p->personarr[i].sex << endl;
		}
		
	}
		cout << "若未显示联系人信息,则是您未将该联系人添加到通讯录" << endl;
	system("pause");
	system("cls");
}
void exchangeinperson(phonebook*p)
{
	string name;
	cout << "请输入要修改的联系人的名字" << endl;
	cin >> name;
	for(int i=0;i<=p->personnum;i++)
	{ 
		if (name == p->personarr[i].name)
		{
			int num;
			cout << "请输入你要修改的手机号" << endl;
			cin >> num;
			p->personarr->phonenumber = num;
			cout << "修改完成" << endl;
		}
		
	}
	system("pause");
	system("cls");
}
void blankalldata(phonebook* p)
{
	p->personnum = 0;
	cout << "清空完成" << endl;
	system("pause");
	system("cls");
}
int main()
{
	phonebook inperson;
	inperson.personnum = 0;
	int a = 0;
	while (true)
	{
		showmenu();
		cin >> a;
		switch (a)
		{
		case 1:addperson(&inperson);
			break;
		case 2:
			showphonebook(&inperson);
			break;
		case 3:
		deleteinperson(&inperson, isexit(&inperson));
		break;
		case 4:
			searchinperson(&inperson);
			break;
		case 5:
			exchangeinperson(&inperson);
			break;
		case 6:
			blankalldata(&inperson);
			break;
		case 7:cout << "祝您生活愉快,欢迎下次使用。" << endl;
			system("pause");
			return 0;
			break;
		default:
			break;
		}
		}
	}


程序员灯塔
转载请注明原文链接:黑马程序员c++通讯录
喜欢 (0)