c++实现通讯录管理系统
作者:一个十分弱的c++入门程序员
1、首先我们来实现系统页面(这里我们就做一个最简单的实现)
show.h
#include<iostream>
using namespace std;
void show();
#pragma once
show.cpp
#include"show.h"
void ::show()
{
cout << "*************************************************************" << endl;
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;
cout << "*************************************************************" << endl;
}
2、开始编写添加联系人的部分
添加联系人.h
#include<iostream>
#include<string>
#pragma once
using namespace std;
#define MAX 1000//利用宏常量来通讯录最大储存数据数量
struct person
{
string m_name;//姓名
int m_sex;//性别
int m_age;//年龄
string m_phone;//手机号码
string m_addr;//家庭住址
};
struct addressbooks
{
person personarr[MAX];
int m_size;
};
void addperson(addressbooks *abs);
添加联系人.cpp
#include"添加联系人.h"
//全程利用指针来实现
void :: addperson(addressbooks *abs)
{
if (abs->m_size == MAX)
{
cout << "通讯录已满" << endl;
return;
}
else
{
string name;
cout << "请输入姓名" << endl;
cin >> name;
abs->personarr[abs->m_size].m_name = name;
int sex = 0;
cout << "请输入年龄" << endl;
cout << "1、男" << endl;
cout << "2、女" << endl;
while (true)
{
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarr[abs->m_size].m_sex = sex;
break;
}
else
{
cout << "请重新输入" << endl;
}
}
cout << "请输入年龄" << endl;
int age = 0;
cin >> age;
abs->personarr[abs->m_size].m_age = age;
cout << "请输入电话" << endl;
string phone;
cin >> phone;
abs->personarr[abs->m_size].m_phone = phone;
cout << "请输入住址" << endl;
string adress;
cin >> adress;
abs->personarr[abs->m_size].m_addr = adress;
abs->m_size++;
cout << "恭喜你添加成功" << endl;
}
system("pause");
system("cls");
}
3、显示联系人
显示联系人.h
#include<iostream>
#include"添加联系人.h"
using namespace std;
void showperson(addressbooks *abs);
#pragma once
显示联系人.cpp
#include"显示联系人.h"
void ::showperson(addressbooks *abs)
{
if (abs->m_size == 0)
{
cout << "空" << endl;
}
else
{
for (int i = 0; i < abs->m_size; i++)
{
cout << "姓名:" << abs->personarr[i].m_name<<" ";
cout << "性别" << (abs->personarr[i].m_sex == 1?":男":":女")<<" ";
cout << "年龄" << abs->personarr[i].m_age<<" ";
cout << "电话" << abs->personarr[i].m_phone<<" ";
cout << "住址" << abs->personarr[i].m_addr << endl;
}
}
system("pause");
system("cls");
}
4、删除联系人
删除联系人.h
#include<iostream>
#include"添加联系人.h"
using namespace std;
int isexist(addressbooks *abs,string name);
void deleteperson(addressbooks *abs);
#pragma once
删除联系人.cpp
#include"删除联系人.h"
int :: isexist(addressbooks *abs, string name)
{
for (int i = 0; i < abs->m_size; i++)
{
if (abs->personarr[i].m_name == name)
{
return i;
}
}
return -1;
}
void::deleteperson(addressbooks *abs)
{
cout << "请输入您要删除的联系人" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret == -1)
{
cout << "查无此人" << endl;
system("pause");
system("cls");
}
else
{
for (int i = ret; i < abs->m_size; i++)
{
//数据前移
abs->personarr[i] = abs->personarr[i + 1];
abs->m_size--;
cout << "已删除" << endl;
}
system("pause");
system("cls");
}
}
5、查找联系人
查找联系人.h
#pragma once
#include"添加联系人.h"
#include"删除联系人.h"
void findperson(addressbooks *abs);
查找联系人.cpp
#include"查找联系人.h"
void::findperson(addressbooks *abs)
{
cout << "请输入您要查找的联系人" << endl;
string name;
cin >> name;
int ret=isexist(abs, name);
if (ret != -1)
{
cout << "姓名:" << abs->personarr[ret].m_name << endl;
cout << "性别:" << abs->personarr[ret].m_sex << endl;
cout << "年龄:" << abs->personarr[ret].m_age << endl;
cout << "电话:" << abs->personarr[ret].m_phone << endl;
cout << "住址:" << abs->personarr[ret].m_addr << endl;
}
else
{
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
6、修改联系人
修改联系人.h
#include"添加联系人.h"
#pragma once
void modifyperson(addressbooks *abs);
修改联系人.cpp
#include"修改联系人.h"
#include"删除联系人.h"
void modifyperson(addressbooks *abs)
{
cout << "请输入您要修改的联系人" << endl;
string name;
cin >> name;
int ret = isexist(abs, name);
if (ret != -1)
{
string name;
cout << "请输入您的姓名" << endl;
cin >> name;
abs->personarr[ret].m_name = name;
cout << "请输入您的性别" << endl;
cout << "1、男" << endl;
cout << "2、女" << endl;
while (true)
{
int sex = 0;
cin >> sex;
if (sex == 1 || sex == 2)
{
abs->personarr[ret].m_sex = sex;
break;
}
else
{
cout << "输入有误,请重新输入" << endl;
}
}
cout << "请输入您的年龄" << endl;
int age = 0;
cin >> age;
abs->personarr[ret].m_age = age;
cout << "请输入您的电话" << endl;
string phone;
cin >> phone;
abs->personarr[ret].m_phone = phone;
cout << "请输入您的住址" << endl;
string addr;
cin >> addr;
abs->personarr[ret].m_addr = addr;
cout << "修改成功" << endl;
system("pause");
system("cls");
}
else
{
cout << "查无此人" << endl;
system("pause");
system("cls");
}
}
7、清空联系人
清空联系人.h
#include"添加联系人.h"
void cls(addressbooks *abs);
#pragma once
清空联系人.cpp
#include"清空联系人.h"
void cls(addressbooks *abs)
{
abs->m_size = 0;
cout << "通讯录已清空" << endl;
system("pause");
system("cls");
}
8、主体部分
主体部分.cpp
#include<iostream>
#include"清空联系人.h"
#include"查找联系人.h"
#include"删除联系人.h"
#include"添加联系人.h"
#include"show.h"
#include"addperson.h"
#include"显示联系人.h"
#include"修改联系人.h"
using namespace std;
int main()
{
addressbooks abs;
abs.m_size = 0;
int a = 0;
while (true)
{
show();
cin >> a;
switch (a)
{
case 1:
addperson(&abs);
break;
case 2:
showperson(&abs);
break;
case 3:
deleteperson(&abs);
break;
case 4:
findperson(&abs);
break;
case 5:
modifyperson(&abs);
break;
case 6:
cls(&abs);
break;
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
}
OK现在这个系统就写好了,主要是运用了指针的知识,还请各位指教。
有任何疑问可以私信我QQ:1297849125
谢谢