一个适合小白学习C++的简单案例,其中还有很多不完善的地方,新手可以通过本案例学习再对其加以改进。
通讯录管理系统
实现以下功能:
#include
#include
using namespace std;
#define max 1000
void show_Menu()
{
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;
}
//创建个人信息的结构体
struct person
{
string name;
int sex;
int age;
string phone;
string address;
};
//创建通讯录结构体(包含个人信息的结构体)
struct address_book
{
struct person num[max];
int m_size;
};
//创建添加联系人功能
void add(struct address_book* p)
{
int sex;
cout << "请输入姓名:" << endl;
cin >> p->num[p->m_size].name;
cout << "请输入性别(1.男 2.女):" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
p->num[p->m_size].sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入(1.男 2.女):" << endl;
}
}
cout << "请输入年龄:" << endl;
cin >> p->num[p->m_size].age;
cout << "请输入联系电话:" << endl;
cin >> p->num[p->m_size].phone;
cout << "请输入家庭地址:" << endl;
cin >> p->num[p->m_size].address;
p->m_size++;
cout << "输入成功!" << endl;
}
//创建显示联系人功能
void show_person(struct address_book* p, int i)
{
string sex;
cout << "姓名:" << p->num[i].name << "\t";
cout << "性别:" << (p->num[i].sex ? sex = "男" : sex = "女") << "\t";
cout << "年龄:" << p->num[i].age << "\t";
cout << "联系电话:" << p->num[i].phone << "\t";
cout << "家庭地址:" << p->num[i].address << endl;
}
//创建显示通讯录功能
void show_address_book(struct address_book* p)
{
int i;
if (p->m_size == 0)
{
cout << "通讯录为空!" << endl;
}
else
{
for (i = 0; i < p->m_size; i++)
{
show_person(p, i);
}
}
}
//创建删除联系人功能
void delet(struct address_book* p)
{
string name;
int i;
if (p->m_size == 0)
{
cout << "通讯录为空!" << endl;
}
else
{
cout << "请输入姓名:" << endl;
cin >> name;
for (i = 0; i < p->m_size; i++)
{
if (p->num[i].name == name)
{
for (; i < p->m_size; i++)
p->num[i] = p->num[i + 1];//查找到指定联系人后将后面存储的联系人信息向前移一位达到删除(覆盖)联系人目的
}
else if (i == p->m_size - 1)
cout << "未查找到联系人,请重新输入:" << endl;
}
}
}
//创建查找联系人功能
void find(struct address_book* p)
{
string name;
int i;
if (p->m_size == 0)
{
cout << "通讯录为空!" << endl;
}
else
{
cout << "请输入姓名:" << endl;
cin >> name;
for (i = 0; i < p->m_size; i++)
{
if (p->num[i].name == name)
{
show_person(p, i);
break;
}
else if (i == (p->m_size - 1))
{
cout << "未查找到该联系人!" << endl;
}
}
}
}
//创建修改联系人功能
void modify(struct address_book* p)
{
string name;
int i, sex;
if (p->m_size == 0)
{
cout << "通讯录为空!" << endl;
}
else
{
cout << "请输入姓名:" << endl;
cin >> name;
for (i = 0; i < p->m_size; i++)
{
if (p->num[i].name == name)
{
show_person(p, i);
cout << "请输入修改后姓名:" << endl;
cin >> p->num[i].name;
cout << "请输入修改后性别(1.男 2.女):" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
p->num[i].sex = sex;
break;
}
else
{
cout << "输入错误,请重新输入(1.男 2.女):" << endl;
}
}
cout << "请输入修改后年龄:" << endl;
cin >> p->num[i].age;
cout << "请输入修改后联系电话:" << endl;
cin >> p->num[i].phone;
cout << "请输入修改后家庭地址:" << endl;
cin >> p->num[i].address;
cout << "修改成功!" << endl;
break;
}
else if (i == (p->m_size - 1))
{
cout << "未查找到该联系人!" << endl;
}
}
}
}
//创建退出功能
void clean(struct address_book* p)
{
int select;
if (p->m_size == 0)
{
cout << "通讯录为空!" << endl;
}
else
{
cout << "确定清空全部联系人?(1.是/2.否):" << endl;
while (true)
{
cin >> select;
if (select == 1)
{
p->m_size = 0;
cout << "已清空联系人!" << endl;
break;
}
else if (select == 2)
break;
else
{
cout << "输入错误,请重新输入:" << endl;
}
}
}
}
int main()
{
int select;
struct address_book p;
p.m_size = 0;
show_Menu();
while (true)
{
cout << "请选择功能:" << endl;
cin >> select;
switch (select)
{
case 1:
add(&p);
system("pause");
break;
case 2:
show_address_book(&p);
system("pause");
break;
case 3:
delet(&p);
system("pause");
break;
case 4:
find(&p);
system("pause");
break;
case 5:
modify(&p);
system("pause");
break;
case 6:
clean(&p);
break;
case 0:
cout << "欢迎下次使用!" << endl;
system("pause");
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
break;
}
}
system("pause");
return 0;
}