控制台显示通讯录管理系统(源码及注释)
主要运用结构体,指针,数组等知识。
实现功能:
1.添加联系人
2.显示联系人
3.删除联系人
4.修改联系人
5.查找联系人
6.清空通讯录
0.退出通讯录
源码如下:
using.h:
#ifndef USING
#define USING
using std::endl;
using std::cout;
using std::cin;
using std::string;
const int MAX = 1000;
#endif // !USING
main.cpp:
#include<iostream>
#include<string>
#include"using.h"
//通讯录管理系统
struct People//通讯录中人物信息结构体
{
string name;
string phonenumber;
};
struct Addressbook//通讯录结构体
{
struct People people[MAX];
int a_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(struct Addressbook *addressbook)
{
if (addressbook->a_size == MAX)//判断是否超过最大人数
{
cout << "通讯录空间不足" << endl;
}
else
{
cout << "添加联系人:" << endl;
cout << "姓名:" ;
cin >> addressbook->people[addressbook->a_size].name;
cout << "电话号码:";
cin >> addressbook->people[addressbook->a_size].phonenumber;
addressbook->a_size++;
}
system("pause");//按任意键继续
system("cls");//清屏
}
void showperson( const struct Addressbook* addressbook)
{
for (int i = 0; i < addressbook->a_size; i++)
{
cout << " " << i + 1 << " 姓名:" << addressbook->people[i].name <<" 电话号码: "<< addressbook->people[i].phonenumber<< endl;
}
system("pause");//按任意键继续
system("cls");//清屏
}
void deleteperson(struct Addressbook* addressbook)
{
//首先要查找通讯录中是否有这个人
int b = 0;//判断数值
cout << "要删除的联系人姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < addressbook->a_size; i++)
{
if (addressbook->people[i].name == name)
{
for (int j = i; j < addressbook->a_size; j++)
{
addressbook->people[j] = addressbook->people[j + 1];
}
b = 0;
break;
}
else
{
b = -1;
}
}
if (b == 0)
{
addressbook->a_size--;//通讯录中人数减一
cout << "删除成功,请刷新后查询" << endl;
}
if (b == -1)
{
cout << "查无此人" << endl;
}
system("pause");//按任意键继续
system("cls");//清屏
}
void searchperson(const struct Addressbook* addressbook)
{
int b = 0;//判断是否存在此人
cout << "输入要查找的人名:" << endl;
string name;
cin >> name;
for (int i = 0; i < addressbook->a_size; i++)
{
if (addressbook->people[i].name == name)
{
cout << " " << i + 1 << " 姓名:" << addressbook->people[i].name << " 电话号码: " << addressbook->people[i].phonenumber << endl;
b = 0;
break;
}
else
{
b = -1;
}
}
if (b == -1)
{
cout << "查无此人" << endl;
}
system("pause");//按任意键继续
system("cls");//清屏
}
void modifyperson(struct Addressbook* addressbook)
{
int b = 0;
cout << "输入要修改的联系人姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < addressbook->a_size; i++)
{
if (addressbook->people[i].name == name)
{
cout << " " << i + 1 << " 姓名:" << addressbook->people[i].name << "电话号码: " << addressbook->people[i].phonenumber << endl;
b = i;
break;
}
else
{
b = -1;
}
}
if (b == -1)
{
cout << "查无此人" << endl;
}
else
{
cout << "Addressbook modify menu:" << endl;
cout << "1.修改姓名 " << endl;
cout << "2.修改电话号码 " << endl;
cout << "3.退出修改系统" << endl;
int h = 0;
cin >> h;//判断条件
string name2;
string phonenumber2;
switch (h)
{
case 1:
cout << "输入修改后的姓名:" << endl;
cin >> name2;
addressbook->people[b].name = name2;
break;
case 2:
cout << "输入修改后的电话号码:" << endl;
cin >> phonenumber2;
addressbook->people[b].phonenumber = phonenumber2;
break;
case 3:
break;
default:
cout << "输入有误,退出修改系统" << endl;
break;
}
}
system("pause");//按任意键继续
system("cls");//清屏
}
void emptyperson(struct Addressbook* addressbook)
{
addressbook->a_size = 0;
cout << "清空完成" << endl;
system("pause");//按任意键继续
system("cls");//清屏
}
int main()
{
int choice = 0;
int a_size = 0;//初始通讯录人数
struct Addressbook addressbook;
addressbook.a_size = a_size;
while (true)
{
showmenu();
cin >> choice;
switch (choice)
{
case 1://添加联系人
addperson(&addressbook);
break;
case 2://显示联系人
showperson(&addressbook);
break;
case 3://删除联系人
deleteperson(&addressbook);
break;
case 4://查找联系人
searchperson(&addressbook);
break;
case 5://修改联系人
modifyperson(&addressbook);
break;
case 6://清空联系人
emptyperson(&addressbook);
break;
case 0://退出通讯录
return 0;
default:
cout << "输入错误,退出通讯录" << endl;
return 0;
}
}
system("pause");
return 0;
}
❤
本文介绍了一个简单的通讯录管理系统,使用C++实现,包含添加、显示、删除、查找和修改联系人等功能。通过结构体、指针和数组等基础知识,构建了一个实用的个人通讯录应用。
961

被折叠的 条评论
为什么被折叠?



