把之前写的东西慢慢都移到优快云上面,以后方便查看。用C++实现简单的通讯录功能。这个逻辑和数据存储都比较简单,所以就不一一介绍了,代码通俗易懂。
#include<string>
#include<fstream>
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
#define MAX 100
//通讯录人的信息:姓名、电话
//通讯录的功能:添加通讯录名单信息、查找联系人电话号码、删除通讯录
class person {
public:
string name;
string tel;
};
struct Arry_person {
int size=0;
person Array[MAX];
};
void add_info(Arry_person &a) //添加人员信息
{
if (a.size == MAX)
{
cout << "通讯录已满" << endl;
return;
}
else {
string name;
string tel;
cout << "请输入人名" << endl;
cin >> name;
a.Array[a.size].name = name;
cout << "请输入手机号" << endl;
cin >> tel;
a.Array[a.size].tel = tel;
}
a.size++;
}
void lookfor(Arry_person &p) {
string name;
cout << "请输入你要查询的姓名" << endl;
cin >> name;
for (int i = 0; i < p.size; i++)
{
if (p.Array[i].name == name)
cout << "已查询到该人的信息" << endl;
cout << "此人的姓名" << name << " " << "此人的电话" << p.Array[i].tel << endl;
}
}int isExit(Arry_person &p, string name) {
for (int i = 0; i < p.size; i++)
{
if (p.Array[i].name == name)
return i ;
}
}
void display(Arry_person &p) {
for (int i = 0; i < p.size; i++)
{
cout << "姓名" << p.Array[i].name << "电话" << p.Array[i].tel << endl;
}
}
void delete_info(Arry_person &p) {
cout << "请输入要删除人的姓名" << endl;
string name;
cin >> name;
int num = isExit(p, name);
for (int j = num; j < p.size-1; j++)
{
p.Array[j] = p.Array[j+1];
}
p.size--;
}
int main() {
Arry_person a;
while (true)
{
int chioce = 0;
cout << "欢迎进入通讯录系统,请选择你需要的功能" << endl;
cout << " 1.添加人员信息" << endl;
cout << " 2.查找联系人电话号码" << endl;
cout << " 3.删除联系人方式" << endl;
cout << " 4.显示所有联系人信息" << endl;
cout << " 5.退出系统" << endl;
cin >> chioce;
switch (chioce)
{
case 1:
add_info(a);
break;
case 2:
lookfor(a);
break;
case 3:
delete_info(a);
break;
case 4:
display(a);
break;
case 5:
exit(0);
break;
default:
break;
}
}
}