@冒泡排序、通讯录管理系统
冒泡排序
#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");
}