C++编写通讯录控制台版
作者:互联网
C++编写通讯录控制台版
效果演示:
下载:程序
主要用到的知识点:
结构体
结构体的创建以及使用,利用数组创建多个结构体对象,在本程序中,利用宏define MAX 1000 来制约了数组的大小,从而达到控制通讯录大小的目的。
指针
AddBooks abs 本程序中用实例化了对象,并在函数中传入该对象的指针。
代码部分
#include <iostream>
#include <windows.h>
#define MAX 1000
using namespace std;
struct Person
{
string name;
string m_Sex;
string m_Age;
string m_Phone;
string m_Addr;
};
struct AddBooks
{
Person PersonArray[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(AddBooks* abs) {
if (abs->m_Size==MAX)
{
cout << "通讯录已满,无法添加" << endl;
return;
}
else {
cout << "请输入姓名:" << endl;
cin >> abs->PersonArray[abs->m_Size].name;
cout << "请输入性别:" << endl;
string sex;
while (true)
{
cin >> sex;
if (sex=="男" || sex == "女")
{
abs->PersonArray[abs->m_Size].m_Sex = sex;
break;
}
cout << "输入有误请重新你输入性别..." << endl;
}
cout << "请输入年龄:" << endl;
cin >> abs->PersonArray[abs->m_Size].m_Age;
cout << "请输入电话:" << endl;
cin >> abs->PersonArray[abs->m_Size].m_Phone;
cout << "请输入地址:" << endl;
cin >> abs->PersonArray[abs->m_Size].m_Addr;
abs->m_Size++;
cout << "添加成功!" << endl;
system("pause");
system("cls");
}
}
//显示联系人函数
void showInfo(AddBooks* abs) {
if (abs->m_Size==0)
{
cout << "当前通讯录有【" << abs->m_Size << "】人!" << endl;
cout << "通讯录为空,请先添加联系人!" << endl;
system("pause");
system("cls");
return;
}
else {
cout << "当前通讯录有【" << abs->m_Size << "】人!" << endl;
for (int i = 0; i < abs->m_Size; i++)
{
cout << "姓名:" << abs->PersonArray[i].name << " 性别:" << abs->PersonArray[i].m_Sex<<
" 年龄:" << abs->PersonArray[i].m_Age << " 电话:" << abs->PersonArray[i].m_Phone<< " 地址:" << abs->PersonArray[i].m_Addr<< endl;
}
system("pause");
system("cls");
}
}
//判断是否存在函数 返回下标
int isExits(AddBooks* abs, string name) {
for (int i = 0; i < abs->m_Size; i++)
{
if (abs->PersonArray[i].name==name)
{
return i;
}
else {
return -1;
}
}
}
//删除联系人函数
void deletePerson(AddBooks* abs) {
cout << "请输入要删除的联系人:" << endl;
string name;
cin >> name;
int ret = isExits(abs, name);
if (ret==-1)
{
cout << "查无此人!" << endl;
}
else {
for (int i = ret; i < abs->m_Size; i++)
{
abs->PersonArray[i] = abs->PersonArray[i + 1];
abs->m_Size--;
cout << "姓名为[" << name << "]的联系人删除成功!" << endl;;
}
}
system("pause");
system("cls");
}
//查找联系人函数
void FindPerson(AddBooks* abs) {
string name;
cout << "请输入要查找的联系人姓名:" << endl;
cin >> name;
int ret = isExits(abs, name);
if (ret!=-1)
{
cout << "-----------------查询成功!" << endl;
cout << "\t姓名:" << abs->PersonArray[ret].name << endl;
cout << "\t性别:" << abs->PersonArray[ret].m_Sex << endl;
cout << "\t年龄:" << abs->PersonArray[ret].m_Age << endl;
cout << "\t电话:" << abs->PersonArray[ret].m_Phone << endl;
cout << "\t家庭住址:" << abs->PersonArray[ret].m_Addr << endl;
}
system("pause");
system("cls");
}
//修改联系人函数
void modifyPreson(AddBooks* abs) {
string name;
cout << "请输入要修改的联系人姓名" << endl;
cin >> name;
int ret = isExits(abs,name);
if (ret==-1)
{
cout << "联系人不在!" << endl;
}
else {
cout << "原姓名:" << abs->PersonArray[ret].name << "\t原性别:" << abs->PersonArray[ret].m_Sex << "\t原年龄:"
<< abs->PersonArray[ret].m_Age << "\t原号码:" << abs->PersonArray[ret].m_Phone << "\t原住址:"
<< abs->PersonArray[ret].m_Addr << endl;
cout << "请输入修改后的姓名:" << endl;
cin >> abs->PersonArray[ret].name;
cout << "请输入修改后的性别:" << endl;
cin >> abs->PersonArray[ret].m_Sex;
cout << "请输入修改后的年龄:" << endl;
cin >> abs->PersonArray[ret].m_Age;
cout << "请输入修改后的电话:" << endl;
cin >> abs->PersonArray[ret].m_Sex;
cout << "请输入修改后的住址:" << endl;
cin >> abs->PersonArray[ret].m_Addr;
cout << "修改成功!" << endl;
}
system("pause");
system("cls");
}
//清空通讯录联系人函数
void removePreson(AddBooks* abs) {
abs->m_Size = 0;
cout << "清空通讯录成功!" << endl;
system("pause");
system("cls");
}
int main()
{
system("mode con cols=65 lines=30 ");//设置控制台大小
system("color a"); //设置控制台字体颜色
SetConsoleTitle(L"C++通讯录"); //设置控制台标题
AddBooks abs;
abs.m_Size = 0; //初始化通讯录人数为0
int select{};
while (true)
{
showMenu();
cout << "请输入指令:\\" << endl;
cin >> select;
switch (select)
{
case 1: // 1、添加联系人
addPerson(&abs);
break;
case 2: // 2、显示联系人
showInfo(&abs);
break;
case 3: // 3、删除联系人
deletePerson(&abs);
break;
case 4: // 4、查找联系人
FindPerson(&abs);
break;
case 5: // 5、修改联系人
modifyPreson(&abs);
break;
case 6: // 6、清空联系人
removePreson(&abs);
break;
case 0: // 0、推出通讯录
system("cls");
cout << "欢迎下次使用...." << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
}
标签:cout,C++,name,abs,通讯录,PersonArray,控制台,Size,ret 来源: https://www.cnblogs.com/xinux/p/16273027.html