本帖最后由 zds212 于 2021-5-7 22:34 编辑
[C++] 纯文本查看 复制代码 #include<iostream>
#include<string>
using namespace std;
#define max 1000
//菜单
void caidan()
{
cout << "\t1.添加联系人\n";
cout << "\t2.查看联系人\n";
cout << "\t3.删除联系人\n";
cout << "\t4.修改联系人\n";
cout << "\t5.查找联系人\n";
cout << "\t6.清空联系人\n";
cout << "\t0(其它).退出\n";
}
//定义属性
struct lianxiren //联系人
{
string name;
string sex;
int haoma;
};
struct tongxunlu
{
struct lianxiren arr[max];
int size;
};
// 1.添加联系人
void addlxr(tongxunlu* abs)
{
cout << "请输入姓名!\n";
string name;
cin >> name;
abs->arr[abs->size].name = name;
cout << "请输入性别!\n\t1.男 2.女\n";
int sex;
cin >> sex;
if (sex == 1)
{
abs->arr[abs->size].sex = "男";
}
else if (sex == 2)
{
abs->arr[abs->size].sex = "女";
}
cout << "请输入手机号!\n";
int id;
cin >> id;
abs->arr[abs->size].haoma = id;
abs->size++;
system("pause");
system("cls");
}
// 2.查看联系人
void cklxr(struct tongxunlu* abs)
{
if (abs->size != 0)
{
for (int i = 0; i < abs->size; i++)
{
cout << "姓名:" << abs->arr[i].name << " 性别:" << abs->arr[i].sex << " 手机号:" << abs->arr[i].haoma << endl;
}
system("pause");
system("cls");
}
else
{
cout << "当前没有联系人,请添加联系人后查看!\n";
system("pause");
system("cls");
}
}
// 3.删除联系人
void sclxr(struct tongxunlu* abs, int xb)
{
if (xb != -1)
{
for (int i = xb; i < abs->size; i++)
{
struct lianxiren temp = abs->arr[i];
abs->arr[i] = abs->arr[i + 1];
abs->arr[i + 1] = temp;
}
system("pause");
system("cls");
abs->size--;
}
else
{
cout << "您输入的联系人不存在!\n";
system("pause");
system("cls");
}
}
// 4.修改联系人
void xglxr(struct tongxunlu* abs, int xb)
{
string name;
cin >> name;
cout << "请输入修改的姓名:\n";
abs->arr[xb].name = name;
cout << "请输入修改的性别!\n\t1.男 2.女\n";
int sex;
cin >> sex;
if (sex == 1)
{
abs->arr[xb].sex = "男";
}
else if (sex == 2)
{
abs->arr[xb].sex = "女";
}
cout << "请输入修改的手机号!\n";
int id;
cin >> id;
abs->arr[xb].haoma = id;
system("pause");
system("cls");
}
// 5.查找联系人
int czlxr(struct tongxunlu* abs)//查找联系人的所作位置
{
string name;
cout << "请输入名称!\n";
cin >> name;
for (int i = 0; i < abs->size; i++)
{
if (name == abs->arr[i].name)
{
return i;
}
}
return -1;
}
void czlxr1(struct tongxunlu* abs, int xb)
{
if (xb != -1)
{
cout << "姓名:" << abs->arr[xb].name << " 性别:" << abs->arr[xb].sex << " 号码:" << abs->arr[xb].haoma << endl;
system("pause");
system("cls");
}
else
{
cout << "查无此人!\n";
system("pause");
system("cls");
}
}
// 6.清空联系人
void qklxr(struct tongxunlu* abs)
{
int select;
cout << "确定清空联系人列表吗?\n\t1.确定 2.按任意键返回\n";
cin >> select;
if (select == 1)
{
abs->size = 0;
cout << "联系人已经清空。\n";
system("pause");
system("cls");
}
else
{
system("cls");
}
}
int main()
{
tongxunlu abs;//定义用户
abs.size = 0;//定义初始用户数量
int select = 0;//定义用户选择
int xb = 0;
while (true)
{
caidan();//调用菜单
cout << "请选择您要操作的选项" << endl;
cin >> select;
switch (select)
{
case 1://添加联系人
addlxr(&abs);
break;
case 2://查看联系人
cklxr(&abs);
break;
case 3://删除联系人
xb = czlxr(&abs);
sclxr(&abs, xb);
break;
case 4://修改联系人
xb = czlxr(&abs);
xglxr(&abs, xb);
break;
case 5://查找联系人
xb = czlxr(&abs);
czlxr1(&abs, xb);
break;
case 6://清空联系人
qklxr(&abs);
break;
default://退出
cout << "欢迎下次使用本程序!\n";
system("pause");
return 0;
break;
}
}
system("pause");
return 0;
} |