C++实现简单通讯录系统
本文实例为大家分享了c++实现简单通讯录系统的具体代码,供大家参考,具体内容如下
需求分析:
1.通讯录可以添加联系人。
2.通讯录可以显示所有联系人。
3.通讯录可以查找联系人。
4.通讯录可以删除联系人。
5.通讯录可以修改联系人。
6.可以清空通讯录。
7.退出通讯录。
项目代码:
#include<iOStream>
#include<string>
#include<stdlib.h>
using namespace std;
#define MAX 1000
typedef struct person { //定义一个联系人的结构体
string name;
string sex;
int age;
int telnumber;
string addr;
}person;
typedef struct Addressbooks { //定义一个通讯录,自带一个联系人数组,里面可以存放MAX个联系人
struct person personArray[MAX];
int m_size=0;
}Addressbooks;
void Menu() { //菜单函数
cout << " 通讯录管理系统" << endl;
cout << "" << endl;
cout << "" << endl;
cout << " 1--添加联系人" << endl;
cout << " 2--显示联系人" << endl;
cout << " 3--删除联系人" << endl;
cout << " 4--查找联系人" << endl;
cout << " 5--修改联系人" << endl;
cout << " 6--清空联系人" << endl;
cout << " 7--退出联系人" << endl;
}
void addPerson(Addressbooks* abs) { //添加联系人的函数实现
if (abs->m_size == MAX) {
cout << "此通讯录已满,不能添加\n" << endl;
}
else {
string name;
string sex;
int age;
int telnumber;
string addr;
cout << "请输入联系人姓名:" << endl;
cin >> name;
abs->personArray[abs->m_size].name = name;
cout << "请输入联系人性别:" << endl;
cin >> sex;
abs->personArray[abs->m_size].sex = sex;
cout << "请输入联系人年龄:" << endl;
cin >> age;
abs->personArray[abs->m_size].age = age;
cout << "请输入联系人电话号码:" << endl;
cin >> telnumber;
abs->personArray[abs->m_size].telnumber = telnumber;
cout << "请输入联系人家庭住址:" << endl;
cin >> addr;
abs->personArray[abs->m_size].addr = addr;
abs->m_size++;
cout << "添加成功!!!" << endl;
system("pause"); //利用这行语句达到一个显示按任意键继续的效果
system("cls"); //清空整个屏幕,刷新屏幕
}
}
void showPerson(Addressbooks* abs) { //显示整个通讯录中联系人的函数实现
for (int i = 0; i < abs->m_size; i++) {
cout << abs->personArray[i].name << " " << abs->personArray[i].sex << " " << abs->personArray[i].age << " " << abs->personArray[i].telnumber << " " << abs->personArray[i].addr << endl;
}
system("pause");
system("cls");
}
int selectPerson(Addressbooks* abs,string name) { //查询输入的联系人是否存在,存在返回联系人坐在的下标值,不存在则返回-1
for (int i = 0; i < abs->m_size; i++) {
if (abs->personArray[i].name == name) {
return i;
}
}
return -1;
}
void deletePerson(Addressbooks* abs) { //删除联系人的函数实现
string name;
cout << "请输入删除联系人的名字:" << endl;
cin >> name;
if (selectPerson(abs, name) != -1) {
for (int i = selectPerson(abs, name); i < abs->m_size;i++) {
abs->personArray[i] = abs->personArray[i + 1];
}
abs->m_size--;
cout << "删除成功" << endl;
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void searchPerson(Addressbooks* abs) { //查询联系人并输出相关信息的函数实现
string name;
cout << "请输入联系人姓名:" << endl;
cin >> name;
int ret = selectPerson(abs, name);
if (ret != -1) {
cout << abs->personArray[ret].name << " " << abs->personArray[ret].sex << " " << abs->personArray[ret].age << " " << abs->personArray[ret].telnumber << " " << abs->personArray[ret].addr << endl;
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void revisePerson(Addressbooks* abs) { //修改联系人的函数实现
string name;
cout << "请选择联系人 :" << endl;
cin >> name;
int ret;
ret = selectPerson(abs, name);
if (ret != -1) {
cout << " 1--修改姓名" << endl;
cout << " 2--修改性别" << endl;
cout << " 3--修改年龄" << endl;
cout << " 4--修改电话号码" << endl;
cout << " 5--修改家庭住址" << endl;
cout << " 0--退出" << endl;
int chosen;
while (1) {
cout << "选择:"<<endl;
cin >> chosen;
switch (chosen)
{
case 1:
{
string name2;
cout << "修改名字:" << endl;
cin >> name2;
abs->personArray[ret].name = name2;
break;
}
case 2:
{
string sex;
cout << "修改性别:" << endl;
cin >> sex;
abs->personArray[ret].sex = sex;
break;
}
case 3:
{
int age;
cout << "修改年龄:" << endl;
cin >> age;
abs->personArray[ret].age = age;
break;
}
case 4:
{
int tel;
cout << "修改电话号码:" << endl;
cin >> tel;
abs->personArray[ret].telnumber = tel;
break;
}
case 5:
{
string addr;
cout << "修改住址:" << endl;
cin >> addr;
abs->personArray[ret].addr = addr;
break;
}
case 0:
Goto FLAG1;
default:
break;
}
}
}
else {
cout << "查无此人!!!" << endl;
}
FLAG1:
system("pause");
system("cls");
}
void clear(Addressbooks* abs) {
abs->m_size = 0;
cout << "此通讯录已清空!!!" << endl;
system("pause");
system("cls");
}
int main() {
Addressbooks book1; //先定义一个通讯录,并添加一个联系人hyp
book1.personArray[0].name = "hyp";
book1.personArray[0].sex = "man";
book1.personArray[0].age = 21;
book1.personArray[0].telnumber = 76766767;
book1.personArray[0].addr = "重庆";
book1.m_size = 1;
int chosen;
while (1) {
Menu();
cout << "选择:" ;
cin >> chosen;
switch (chosen)
{
case 1: //添加联系人
addPerson(&book1);
break;
case 2: //显示联系人
showPerson(&book1);
break;
case 3: //删除联系人
deletePerson(&book1);
break;
case 4: //查找联系人
searchPerson(&book1);
break;
case 5: //修改联系人
revisePerson(&book1);
break;
case 6: //清空联系人
clear(&book1);
break;
case 7: //退出通讯录
return 0;
break;
default:
break;
}
}
return 0;
}
效果:
显示联系人
添加联系人
后面效果就不一一显示了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关文章