冒泡排序、 结构体使用(通讯录管理系统)

@冒泡排序、通讯录管理系统

冒泡排序

#include<iostream>
using namespace std;
void sort(int *arr,int len)
{
	for (int i = 0; i < len - 1; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (arr[j]>arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}

		}
	}
}
void print(int *arr, int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << arr[i]<<"\t";
	}
}
int main()
{
	int arr[10] = { 1, 1, 5, 2, 5, 1, 2, 3, 1, 2 };
	int len = sizeof(arr) / sizeof(arr[0]);
	sort(arr,len);
	print(arr, len);
	system("pause");
}

结构体使用(通讯录管理系统)

#include<iostream>
#include<string>
using namespace std;
struct Person{
	string m_name;
	int m_Sex;
	int m_Age;
	string m_Phone;
};
struct Addressbook{
	struct Person stu[1000];
	int m_size = 1000;
};
void add(Addressbook *abs)
{
	string name;
	cout << "姓名:";
	cin >> name;
	abs->stu[abs->m_size].m_name = name;
	cout << "性别:";
	int sex=0;
	cin >> sex;
	abs->stu[abs->m_size].m_Sex = sex;
	int age=0;
	cout << "年龄:";
	cin >> age;
	abs->stu[abs->m_size].m_Age = age;
	string phone;
	cout << "电话:";
	cin >> phone;
	abs->stu[abs->m_size].m_Phone = phone;
	abs->m_size++;

	cout << "添加成功"<<endl;
	
	system("pause");system("cls");
}
void display(Addressbook *abs)
{
	if (abs->m_size == 0)
	{
		cout << "当前没人";
	}
	else{
		for (int i=0; i < abs->m_size; i++)
		{
			cout << "姓名:" << abs->stu[i].m_name << "\t";
			cout << "性别:" << abs->stu[i].m_Sex << "\t";
			cout << "年龄:" << abs->stu[i].m_Age << "\t";
			cout << "电话:" << abs->stu[i].m_Phone<<endl;
		}
	}
	system("pause");
	system("cls");
}

//检测是否存在
int isExist(Addressbook *abs, string name)
{
	for (int i = 0; i < abs->m_size; i++)
	{
		if (abs->stu[i].m_name == name)
		{
			return i;
		}
	}
	return -1;
}
void Delete(Addressbook *abs)
{
	cout << "输入要删除的联系人";
	string name;
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1)
	{
		cout << "查无此人";
	}
	if (ret != -1)
	{
		for (int i = ret; i < abs->m_size; i++)
		{
			abs->stu[i] = abs->stu[i + 1];
		}
		abs->m_size--;
		cout << "删除成功";

	}
	system("pause");
	system("cls");
}
void find(Addressbook *abs){
	string name;
	cout << "请输入查找的人:";
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1)
	{
		cout << "查无此人";
	}
	if (ret != -1)
	{
		cout << "姓名:" << abs->stu[ret].m_name << "\t";
		cout << "性别:" << abs->stu[ret].m_Sex << "\t";
		cout << "年龄:" << abs->stu[ret].m_Age << "\t";
		cout << "电话:" << abs->stu[ret].m_Phone << endl;
		}
	
		
	system("pause");
	system("cls");
}
void modif(Addressbook *abs)
{
	string name;
	cout << "请输入修改的人:";
	cin >> name;
	int ret = isExist(abs, name);
	if (ret == -1)
	{
		cout << "查无此人";
	}
	if (ret != -1)
	{
		string name;
		cout << "姓名:";
		cin >> name;
		abs->stu[ret].m_name = name;
		cout << "性别:";
		int sex = 0;
		cin >> sex;
		abs->stu[ret].m_Sex = sex;
		int age = 0;
		cout << "年龄:";
		cin >> age;
		abs->stu[ret].m_Age = age;
		string phone;
		cout << "电话:";
		cin >> phone;
		abs->stu[ret].m_Phone = phone;
		
	}
	system("pause");
	system("cls");
}
void clear(Addressbook *abs)
{
	abs->m_size = 0;
	cout << "通讯录已清空"<<endl;
	system("pause");
	system("cls");
}
void show(void){
	cout<<"     通讯录管理系统          \n";
	cout << "----------------------------\n";
	cout << "|1、添加联系人            |\n";
	cout << "|2、显示联系人            |\n";
	cout << "|3、删除联系人            |\n";
	cout << "|4、查找联系人            |\n";
	cout << "|5、修改联系人            |\n";
	cout << "|6、清空联系人            |\n";
	cout << "|0、退出通讯录            |\n";
	cout << "----------------------------\n";

}

int main()
{
	Addressbook abs;
	abs.m_size = 0;
	
	
	while (1){
	show();
	int select = 0;
	cin >> select;
	switch (select)
	{
	case 1:            //1、添加联系人
		add(&abs);
		break;
	case 2:            //2、显示联系人 
		display(&abs);
		break;
	case 3:            //3、删除联系人
		Delete(&abs);
		break;
	case 4:            //4、查找联系人
		find(&abs);
	break;
	case 5:            //5、修改联系人
		modif(&abs);
		break;
	case 6:            //6、清空联系人
		clear(&abs);
		break;
	case 0:
		cout << "欢迎下次使用"<<endl;
		system("pause");
		break;
	}
	}
	system("pause");
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值