C++编写简易通讯录系统(详细代码可运行)

功能目录

1、添加联系人
2、显示联系人
3、删除联系人
4、查找联系人
5、修改联系人
6、清空联系人
0、退出联系人

1、添加联系人

在控制台中输入1就表示是添加联系人的功能,随后会依次出现 “姓名:”、“性别:”、“年龄:”、“联系电话:”、“联系地址:”的字样,我们只需正确输入即可,其中“性别:”中只能输入“男”或“女”,如果输入其他的字样就会被要求重新输入。还有这个添加联系人的添加人数是1000人。

void addpeople(peoplebook* abs)
{
    if (abs->size == max)
    {
        cout << "联系人已满,无法添加" << endl;
        return;
    }
    else {
        string name;
        cout << "请输入名字: " << endl;
        cin >> name;
        abs->peoplearray[abs->size].name = name;
        string sex;
        
        while (true)
        {
            cout << "请输入性别: " << endl;
            cin >> sex;
            if (sex == "男" || sex == "女")
            {
                abs->peoplearray[abs->size].sex = sex; break;
            }
            else 
            {
                cout << "输入错误,请重新输入" << endl;
            }
        }
        int age;
        cout << "请输入年龄: " << endl;
        cin >> age;
        abs->peoplearray[abs->size].age = age;
        string phone;
        cout << "请输入联系电话: " << endl;
        cin >> phone;
        abs->peoplearray[abs->size].phone = phone;
        string map;
        cout << "请输入联系地址: " << endl;
        cin >> map;
        abs->peoplearray[abs->size].map = map;
        cout << "添加成功" << endl;
        abs->size++;
        system("pause");
        system("cls");
    }
}

在这里插入图片描述

显示联系人

void showpeople(peoplebook* abs)
{
    if (abs->size == 0)
    {
        cout << "对不起,未添加联系人,请添加联系人" << endl;
    }
    else {
        for(int i=0;i<abs->size;++i)
        { 
        cout << "姓名: " << "\t";
        cout << abs->peoplearray[i].name << "\t";
        cout << "性别: " << "\t";
        cout << abs->peoplearray[i].sex << "\t";
        cout << "年龄: " << "\t";
        cout << abs->peoplearray[i].age << "\t";
        cout << "联系电话: " << "\t";
        cout << abs->peoplearray[i].phone << "\t";
        cout << "联系地址: " << "\t";
        cout << abs->peoplearray[i].map << endl;
        }
    }
    system("pause");
    system("cls");
}

在这里插入图片描述

删除联系人

void delpeople(peoplebook* abs)
{
    string name;
    cin >> name;
    int res = ispeople(name, abs);
    if (res == -1)
    {
        cout << "查无此人" << endl;
    }
    if(res != -1) {
        for (int i = res;i < abs->size;++i)
        {
            abs->peoplearray[i] = abs->peoplearray[i + 1];
        }
        abs->size--;
        cout << "删除成功!!!" << endl;
 
    }
    system("pause");
    system("cls");
}

在这里插入图片描述

查找联系人

void chapeople(peoplebook* abs)
{
    string name;
    cin >> name;
    int res = ispeople(name, abs);
    if (res == -1)
    {
        cout << "查无此人" << endl;
    }
    if (res != -1)
    {
        cout << "姓名: " << "\t";
        cout << abs->peoplearray[res].name << "\t";
        cout << "性别: " << "\t";
        cout << abs->peoplearray[res].sex << "\t";
        cout << "年龄: " << "\t";
        cout << abs->peoplearray[res].age << "\t";
        cout << "联系电话: " << "\t";
        cout << abs->peoplearray[res].phone << "\t";
        cout << "联系地址: " << "\t";
        cout << abs->peoplearray[res].map << endl;
    }
    system("pause");
    system("cls");
}

在这里插入图片描述

修改联系人

void modifypeople(peoplebook* abs)
{
    string name;
    cin >> name;
    int res = ispeople(name, abs);
    if (res == -1)
    {
        cout << "查无此人" << endl;
    }
    if (res != -1)
    {
        string name;
        cout << "请输入名字: " << endl;
        cin >> name;
        abs->peoplearray[res].name = name;
        string sex;
 
        while (true)
        {
            cout << "请输入性别: " << endl;
            cin >> sex;
            if (sex == "男" || sex == "女")
            {
                abs->peoplearray[res].sex = sex; break;
            }
            else
            {
                cout << "输入错误,请重新输入" << endl;
            }
        }
        int age;
        cout << "请输入年龄: " << endl;
        cin >> age;
        abs->peoplearray[res].age = age;
        string phone;
        cout << "请输入联系电话: " << endl;
        cin >> phone;
        abs->peoplearray[res].phone = phone;
        string map;
        cout << "请输入联系地址: " << endl;
        cin >> map;
        abs->peoplearray[res].map = map;
        cout << "添加成功" << endl;
    }
    system("pause");
    system("cls");
}

在这里插入图片描述

清空联系人

void cleanpeople(peoplebook* abs)
{
    abs->size = 0;
    cout << "已清空完毕" << endl;
    system("pause");
    system("cls");
}

在这里插入图片描述

主函数+剩余的代码

#include<iostream>
#include <string>
#define max 1000
using namespace std;
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;
}
struct people
{
    string name;
    string sex;
    int age;
    string phone;
    string map;
};
struct peoplebook
{
    people peoplearray[max];
    int size=0;
};
int main()
{
    int select = 0;
    string name;
    peoplebook abs;
    while (true)
    { 
        showMenu();
      cin >> select;
     
      switch (select)
      {
            case 1://1、添加联系人
                addpeople(&abs);
                break;
            case 2://2、显示联系人
                showpeople(&abs);
                break;
            case 3://3、删除联系人
                cout << "请输入联系人的姓名:" << endl;
                delpeople(&abs);
                break;
            case 4://4、查找联系人
                cout << "请输入联系人的姓名:" << endl;
                chapeople(&abs);
                break;
            case 5://5、修改联系人
                cout << "请输入联系人的姓名:" << endl;
                modifypeople(&abs);
                break;
            case 6://、清空联系人
                cleanpeople(&abs);
                break;
            case 0:
                cout << "欢迎下次使用" << endl;
                return 0;
                break;
      }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值