《四海小记c++学习之路》通讯录管理系统
写在前面的话:
这个不是按着黑马程序员写的,我自己写的,有问题欢迎私聊我,或在底下评论!
/**************************************
Copyright 陈四海
Author: created by 陈四海
Date: 2020-12-14
Description: 手机通信录
Version: 2.0
**************************************/
#include<iostream>
using namespace std;
typedef struct person {
string name;
string sex;
int age;
int number;
string site;
}person;
/**************************************
Fuction: add
Description: 添加联系人
Input: person * p,int * temp
Output:
Return: void
Others:
**************************************/
void add(person * p,int * temp)
{
if (*temp < 1000)
{
cout << "姓名:";
cin >> p[*temp].name;
cout << endl << "性别:";
cin >> p[*temp].sex;
cout << endl << "年龄:";
cin >> p[*temp].age;
cout << endl << "手机号:";
cin >> p[*temp].number;
cout << endl << "地址:";
cin >> p[*temp].site;
}
else
cout << "人数超出限制" << endl;
}
/**************************************
Fuction: show
Description: 显示联系人
Input: const person * per, const int temp
Output:
Return: void
Others:
**************************************/
void show(const person * per, const int temp)
{
for (int i = 0; i < temp; i++)
{
cout << "-------------·第" << i << "位·----------------\n";
cout << "姓名:" << per[i].name <<endl;
cout << "性别:" << per[i].sex << endl;
cout << "年龄:" << per[i].age << endl;
cout << "手机号:" << per[i].number << endl;
cout << "地址:" << per[i].site << endl;
}
}
/**************************************
Fuction: seek
Description: 查找联系人
Input: const person * per, const int temp
Output:
Return: void
Others:
**************************************/
//按手机号查询
int seekNumber(const person num, const int temp)
{
int number;
cout << "请输入手机号:";
cin >> number;
for (int i = 0; i < temp; i++)
{
if (num.number == number)return i;
}
cout << "没有这个手机号!" << endl;
return 0;
}
//按姓名查询
int seekName(const person num, const int temp)
{
string name;
cout << "请输入姓名:";
cin >> name;
for (int i = 0; i < temp; i++)
{
if (num.name == name)return i;
}
cout << "没有这个联系人!" << endl;
return 0;
}
bool seek(const person* per, const int temp) {
cout <<
"请选择查询方式:\n"
"1-按姓名查询\n"
"2-按手机号查询\n"
"0-返回"<< endl;
int t,id=-1;
cin >> t;
switch (t)
{
case 0:return false;
case 1:id=seekName(*per, temp); break;
case 2:id = seekNumber(*per, temp); break;
}
if (id != -1) {
cout <<
"-------------查询结果-------------"
"\n姓名:" << per[id].name <<
"\n性别:" << per[id].sex <<
"\n年龄:" << per[id].age <<
"\n手机号:" << per[id].number <<
"\n地址:" << per[id].site <<
"---------------------------------" << endl;
}
else
{
cout << "请输入正确选择!" << endl;
seek(per, temp);
return false;
}
return true;
}
/**************************************
Fuction: deleted
Description: 删除联系人
Input: const person * per, const int temp
Output:
Return: bool
Others:
**************************************/
bool deleted(person* per, int* temp) {
cout <<
"请选择删除方式:\n"
"1-按姓名删除\n"
"2-按手机号删除\n"
"0-返回\n" << endl;
int t, id = -1;
cin >> t;
switch (t)
{
case 0:return false;
case 1:id = seekName(*per, *temp); break;
case 2:id = seekNumber(*per, *temp); break;
}
if (id != -1) {
for (int i = id; i < id; i++) {
per[i].name = per[i + 1].name;
per[i].sex = per[i + 1].sex;
per[i].age = per[i + 1].age;
per[i].number = per[i + 1].number;
per[i].site = per[i + 1].site;
}
cout << "删除成功!" << endl;
return true;
}
else
{
cout << "请输入正确选择!" << endl;
deleted(per, temp);
return false;
}
}
/**************************************
Fuction: change
Description: 修改联系人
Input: person * per, int temp
Output:
Return: bool
Others:
**************************************/
bool change( person* per, int* temp) {
string name;
cout << "请输入联系人姓名:";
cin >> name;
int j = -1;
for (int i = 0; i < *temp; i++)
{
if (per[i].name == name)
{
j = i;
break;
}
}
if (j == -1)
{
cout << "未找到该联系人!"
"请重新输入!" << endl;
change(per, temp);
}
int t = -1;
cout <<
"请选择:"
"1-修改姓名"
"2-修改性别"
"3-修改年龄"
"4-修改手机号"
"5-修改地址"
"0-返回上一级";
cin >> t;
switch (t)
{
case 1:
{
cout << "修改后的姓名:";
cin >> per[j].name;
return true;
}
case 2:
{
cout << "修改后的性别:";
cin >> per[j].sex;
return true;
}
case 3:
{
cout << "修改后的年龄:";
cin >> per[j].age;
return true;
}
case 4:
{
cout << "修改后的手机号:";
cin >> per[j].number;
return true;
}
case 5:
{
cout << "修改后的地址:";
cin >> per[j].site;
return true;
}
case 0:return false;
}
return true;
}
//清空联系人
void close(person* per, int * temp) {
if (*temp == 0){
cout << "通讯录内联系人为 0 !" << endl;
}
else {
for (int i = 0; i < *temp; i++) {
per[i] = per[*temp+1];
}
cout << "联系人清除完毕!" << endl;
}
}
//主函数
int main() {
person per[100] = { {"芜湖","男",12,312314,"???"}, {"芜湖","男",12,312314,"???"} };
//上面原来是1000,但是我改成了100,因为对堆栈使用过多
//在后期可以使用vector<person> 这样的动态数组进行定义
//可以减少对内存的使用
person* p=per;
int temp=2,key;
int* ptemp = &temp;
while(true){
cout <<
"---------·功能表·-------------\n"
"1-添加联系人\n"
"2-显示联系人\n"
"3-删除联系人\n"
"4-查找联系人\n"
"5-修改联系人\n"
"6-清空联系人\n"
"0-退出通讯录\n"
"--------------------------------\n"
"请选择功能:";
cin >> key;
switch (key)
{
case 1:add(p,ptemp); break; //添加联系人
case 2:show(p,temp); break; //显示联系人
case 3:deleted(p,ptemp); break; //删除联系人
case 4:seek(p, temp); break; //查找联系人
case 5:change(p, ptemp); break; //修改联系人
case 6:close(p, ptemp); break; //清空联系人
case 0: {
cout << "\n成功退出通讯录!" << endl;
system("pause");
return 0;
} //退出通讯录
}
}
}