若代码有用,请各位客官留下您宝贵的关注,收藏和点赞,谢谢啦!!!
前言
该系统——通讯录管理系统,适合练手C++项目,感受第一个CURD项目,适用于课程设计和实训作业。
一、通讯录源代码
#include<iostream>
#include<string>
#define max 1000
using namespace std;
//联系人结构体
struct person
{
string m_name;
int m_sex;
int m_age;
string m_phone;
string m_address;
};
//通讯录结构体
struct Addressbook
{
struct person ammaryperson[max];
int m_size;
};
//展示通讯录
void showmenu()
{
cout << "********************" << endl;
cout << "****1.添加联系人****" << endl;
cout << "****2.显示联系人****" << endl;
cout << "****3.删除联系人****" << endl;
cout << "****4.查找联系人****" << endl;
cout << "****5.修改联系人****" << endl;
cout << "****6.清空联系人****" << endl;
cout << "****0.退出通讯录****" << endl;
cout << "********************" << endl;
}
//添加联系人
void addperson(Addressbook *abd)
{
if (abd->m_size == max)
{
cout << "通讯录已满" << endl;
return;
}
else
{
//添加联系人姓名
string name;
cout << "请输入姓名;" << endl;
cin >> name;
abd->ammaryperson[abd->m_size].m_name = name;
//添加性别
int sex;
cout << "请输入性别:" << endl;
cout << "1--男 2--女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abd->ammaryperson[abd->m_size].m_sex == sex;
break;
}
cout << "请重新输入:" << endl;
}
//添加年龄
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abd->ammaryperson[abd->m_size].m_age=age;
//添加号码
string phone;
cout << "请输入号码:" << endl;
cin >> phone;
abd->ammaryperson[abd->m_size].m_phone=phone;
//添加地址
string address;
cout << "请输入地址:" << endl;
cin >> address;
abd->ammaryperson[abd->m_size].m_address = address;
//更新通讯录
abd->m_size++;
cout << "添加成功" << endl;
system("pause"); //请按任意键继续
system("cls"); //清屏操作
}
}
//显示联系人
void showperson(Addressbook *abd)
{
if (abd->m_size == 0)
{
cout << "通讯录为空" << endl;
}
else
{
for (int i = 0; i < abd->m_size; i++)
{
cout << "姓名:" << abd->ammaryperson[i].m_name <<"\t";
cout << "性别:" << (abd->ammaryperson[i].m_sex == 1 ? "男": "女" )<< "\t";
cout << "年龄:" << abd->ammaryperson[i].m_age << "\t";
cout << "号码:" << abd->ammaryperson[i].m_phone << "\t";
cout << "地址:" << abd->ammaryperson[i].m_address << endl;
}
}
system("pause");
system("cls");
}
//查看通迅录有无此人,返回值后执行
int func(Addressbook *abd, string name)
{
for (int i = 0; i < abd->m_size; i++)
{
if (abd->ammaryperson[i].m_name == name)
return i;
}
return -1;
}
//删除联系人
void delectperson(Addressbook *abd)
{
cout << "请输入您要删除的联系人:" << endl;
string name;
cin >> name;
int ret = func(abd, name);
if (ret != -1)
{
for (int i = ret; i < abd->m_size; i++)
{
abd->ammaryperson[i] = abd->ammaryperson[i + 1];
}
abd->m_size--;
cout << "删除成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//查找联系人
void findpreson(Addressbook *abd)
{
cout << "请输入您要查找的联系人:" << endl;
string name;
cin >> name;
int ret = func(abd, name);
if (ret != -1)
{
cout << "姓名:" << abd->ammaryperson[ret].m_name << "\t";
cout << "性别" << (abd->ammaryperson[ret].m_sex == 1?"男":"女")<< "\t";
cout << "年龄" << abd->ammaryperson[ret].m_age << "\t";
cout << "号码" << abd->ammaryperson[ret].m_phone << "\t";
cout << "地址" << abd->ammaryperson[ret].m_address << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//修改联系人
void modify(Addressbook *abd)
{
cout << "请输入你要修改的联系人:" << endl;
string name;
cin >> name;
int ret = func(abd, name);
if (ret!=-1)
{
string name;
cout << "请输入姓名:";
cin >> name;
abd->ammaryperson[ret].m_name = name;
int sex = 0;
cout << "请输入性别:" << endl;
cout << "1--男 2--女" << endl;
cin >>sex;
while (true)
{
if (sex == 1 || sex == 2)
{
abd->ammaryperson[ret].m_sex = sex;
break;
}
else
cout << "输入有误,请重新输入" << endl;
}
int age;
cout << "请输入年龄:" << endl;
cin >> age;
abd->ammaryperson[ret].m_age = age;
string phone;
cout << "请输入号码:" << endl;
cin >> phone;
abd->ammaryperson[ret].m_phone = phone;
string address;
cout << "请输入地址:" << endl;
cin >> address;
abd->ammaryperson[ret].m_address = address;
cout << "修改成功" << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
//清空联系人
void clearperson(Addressbook *abd)
{
int choose;
cout << "是否要清除联系人" << endl;
while (true)
{
cout << "确认:1" << "取消:2" << endl;
cin >> choose;
if (choose == 1)
{
abd->m_size = 0;
cout << "已清空联系人" << endl;
break;
}
else if (choose == 2)
{
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
system("pause");
system("cls");
}
int main()
{
Addressbook abd;
abd.m_size = 0;
int select = 0;
while (true)
{
showmenu();
cout << "请输入功能序号:" ;
cin >> select;
switch (select)
{
case 1:
addperson(&abd);
break;
case 2:
showperson(&abd);
break;
case 3:
/*{
cout << "请输入删除联系人姓名" << endl;
string name;
cin >> name;
if (func(&abd, name) == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "找到此人" << endl;
}
}*/
delectperson(&abd);
break;
case 4:
findpreson(&abd);
break;
case 5:
modify(&abd);
break;
case 6:
clearperson(&abd);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}
总结
若代码有用,请各位客官留下您宝贵的关注,收藏和点赞,谢谢啦!!!