C++实现通讯录小功能

 更新时间:2022年06月20日 14:08:02   作者:糯米团子鸭  
这篇文章主要为大家详细介绍了C++实现通讯录小功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现通讯录功能的具体代码,供大家参考,具体内容如下

思路:

1.显示菜单栏

void menu() {
 
    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;
}

2.退出

int main() {
 
    int select = 0;
    cin >> select;
    switch (select) {
        
        case 0://退出通讯录
            cout << "欢迎下次使用" << endl;
            system("pause");
            return 0;
            break;
        
    }
    
    system("pause");
    return 0;
}

3.创建结构体

3.1创建联系人结构体
3.2创建通讯录结构体

//定义联系人结构体
//姓名、电话号码、邮箱、地址
struct person {
    string name;
    string number;
    string Email;
    string address;
};
// 定义通讯录结构体
struct contacts {
    int people_num;
    struct person personarr[MAX];//子结构体:联系人信息
};

4.添加联系人

void addperson(struct contacts* p) {
    if (p->people_num == MAX ) {
        cout << "通讯录已满" << endl;
    }
    else {//添加联系人信息
        string name, number, Email, address;
        cout << "请输入姓名:" << endl;
        cin >> name;
        cout << "请输入电话:" << endl;
        cin >> number;
        cout << "请输入邮箱:" << endl;
        cin >> Email;
        cout << "请输入地址:" << endl;
        cin >> address;
        p->personarr[p->people_num].name = name;
        p->personarr[p->people_num].number = number;
        p->personarr[p->people_num].Email = Email;
        p->personarr[p->people_num].address = address;
        
        p->people_num++;
        cout << "添加成功!" << endl;
    }
}

5.删除联系人

判断联系人是否存在

int existperson(struct contacts* p,string name) {
    
    for (int i = 0; i < p->people_num; i++) {
        if ( p->personarr[i].name == name ) {
            return i;
        }
    }
    return -1;
}

若存在,获取联系人在通讯录位置,将position后面的都往前移动一个位置,覆盖之前的值

//删除联系人
void delperson(struct contacts* p,int position) {
    while (position < (p->people_num)) {
        p->personarr[position] = p->personarr[position + 1];
        position++;
    }
    p->people_num--;
    cout << "删除成功!" << endl;
}

6.修改

检查要修改联系人是否存在,并获取当前位置

void modifyperson(struct contacts* p, int position) {
    string  number, Email, address;
    
    cout << "请输入修改电话:" << endl;
    cin >> number;
    cout << "请输入修改邮箱:" << endl;
    cin >> Email;
    cout << "请输入修改地址:" << endl;
    cin >> address;
 
    p->personarr[position].number = number;
    p->personarr[position].Email = Email;
    p->personarr[position].address = address;
    cout << "修改成功!" << endl;
}

7.查找

void searchperson(struct contacts* p, int position) {
    cout << "姓名:" << p->personarr[position].name << "    "
        << "电话:" << p->personarr[position].number << "    "
        << "邮箱:" << p->personarr[position].Email << "    "
        << "地址:" << p->personarr[position].address << "    ";
}

8.显示通讯录

void showcontact(struct contacts* p) {
    for (int i = 0; i < (p->people_num); i++) {
        cout <<"姓名:" << p->personarr[i].name << "    "
            <<"电话:" << p->personarr[i].number << "    "
            <<"邮箱:" << p->personarr[i].Email << "    "
            <<"地址:" << p->personarr[i].address << "    "<< endl;
 
    }        
}

9.清空通讯录

void cleancontact(struct contacts* p) {
    p->people_num = 0;
    cout << "已清空!" << endl;
}

全部代码

#include<iostream>
using namespace std;
#define MAX 200 
 
//1.菜单栏显示
void menu() {
 
    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;
}
 
//2.定义结构体
//2.1定义联系人结构体
//姓名、电话号码、邮箱、地址
struct person {
    string name;
    string number;
    string Email;
    string address;
};
//2.2 定义通讯录结构体
struct contacts {
    int people_num;
    struct person personarr[MAX];//子结构体:联系人信息
};
 
//3.添加联系人
void addperson(struct contacts* p) {
    if (p->people_num == MAX ) {
        cout << "通讯录已满" << endl;
    }
    else {//添加联系人信息
        string name, number, Email, address;
        cout << "请输入姓名:" << endl;
        cin >> name;
        cout << "请输入电话:" << endl;
        cin >> number;
        cout << "请输入邮箱:" << endl;
        cin >> Email;
        cout << "请输入地址:" << endl;
        cin >> address;
        p->personarr[p->people_num].name = name;
        p->personarr[p->people_num].number = number;
        p->personarr[p->people_num].Email = Email;
        p->personarr[p->people_num].address = address;
        
        p->people_num++;
        cout << "添加成功!" << endl;
    }
}
//4.删除联系人
//4.1检测联系人是否存在
int existperson(struct contacts* p,string name) {
    
    for (int i = 0; i < p->people_num; i++) {
        if ( p->personarr[i].name == name ) {
            return i;
        }
    }
    return -1;
}
//删除联系人
void delperson(struct contacts* p,int position) {
    while (position < (p->people_num)) {
        p->personarr[position] = p->personarr[position + 1];
        position++;
    }
    p->people_num--;
    cout << "删除成功!" << endl;
}
//5.修改联系人
void modifyperson(struct contacts* p, int position) {
    string  number, Email, address;
    
    cout << "请输入修改电话:" << endl;
    cin >> number;
    cout << "请输入修改邮箱:" << endl;
    cin >> Email;
    cout << "请输入修改地址:" << endl;
    cin >> address;
 
    p->personarr[position].number = number;
    p->personarr[position].Email = Email;
    p->personarr[position].address = address;
    cout << "修改成功!" << endl;
}
 
//6.查找联系人
void searchperson(struct contacts* p, int position) {
    cout << "姓名:" << p->personarr[position].name << "    "
        << "电话:" << p->personarr[position].number << "    "
        << "邮箱:" << p->personarr[position].Email << "    "
        << "地址:" << p->personarr[position].address << "    ";
}
 
//7.显示通讯录
void showcontact(struct contacts* p) {
    for (int i = 0; i < (p->people_num); i++) {
        cout <<"姓名:" << p->personarr[i].name << "    "
            <<"电话:" << p->personarr[i].number << "    "
            <<"邮箱:" << p->personarr[i].Email << "    "
            <<"地址:" << p->personarr[i].address << "    "<< endl;
 
    }        
}
//8.清空联系人
void cleancontact(struct contacts* p) {
    p->people_num = 0;
    cout << "已清空!" << endl;
}
 
int main() {
    //创建通讯录结构体变量
    struct contacts c;
    //初始化通讯录当前联系人个数
    c.people_num = 0;
    int select = 0;
    string name;
    while (1) {
        menu();
        cin >> select;
        switch (select) {
            case 1:// 添加联系人
                addperson(&c);
                system("pause");
                break;
 
            case 2:// 删除联系人
    
                cout << "请输入删除联系人的姓名:";
                cin >> name;
                if (existperson(&c,name)==-1) {
                    cout << "该联系人不存在!";
                }
                else{
                    delperson(&c, existperson(&c, name));
                }
                
                system("pause");
                break;
 
            case 3:// 修改联系人
            
                cout << "请输入要修改联系人的姓名:";
                cin >> name;
                if (existperson(&c,name) == -1) {
                    cout << "该联系人不存在!";
                }
                else {
                    modifyperson(&c, existperson(&c, name));
                }
                
                system("pause");
                break;
 
            case 4:// 查找联系人
                
                cout << "请输入查找联系人的姓名:";
                cin >> name;
                if (existperson(&c,name) == -1) {
                    cout << "该联系人不存在!";
                }
                else {
                    searchperson(&c, existperson(&c, name));
                }
                
                system("pause");
                break;
 
            case 5:// 显示通讯录 
                showcontact(&c);
                system("pause"); 
                break;
            case 6:// 清空联系人
                cleancontact(&c);
                system("pause");
                break;
            case 0://退出通讯录
                cout << "欢迎下次使用" << endl;
                system("pause");//暂停批文件的处理
                return 0;
                break;
        }
        system("cls");//清屏
    }
    
    system("pause");
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C++17结构化绑定的实现

    C++17结构化绑定的实现

    这篇文章主要介绍了C++17结构化绑定的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • C语言之初识指针

    C语言之初识指针

    在C语言中,指针是一种保存变量地址的变量。这篇文章介绍了初识C语言指针,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • C++实现特殊矩阵的压缩存储算法

    C++实现特殊矩阵的压缩存储算法

    在实际存储时,会发现矩阵中有许多值相同的数据或有许多零数据,且分布呈现出一定的规律,称这类型的矩阵为特殊矩阵。本文将利用C++实现特殊矩阵的压缩存储,感兴趣的可以了解一下
    2022-08-08
  • C++继承中的对象构造与析构和赋值重载详解

    C++继承中的对象构造与析构和赋值重载详解

    这篇文章主要为大家详细介绍了C++继承中的对象构造与析构和赋值重载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • C++详解实现Stack方法

    C++详解实现Stack方法

    C++ Stack(堆栈)是一个容器类的改编,为程序员提供了堆栈的全部功能,也就是说实现了一个先进后出(FILO)的数据结构
    2022-06-06
  • C语言自定义类型的保姆级讲解

    C语言自定义类型的保姆级讲解

    这篇文章主要给大家介绍了关于C语言自定义类型的保姆级讲解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 浅谈c++ vector和map的遍历和删除对象

    浅谈c++ vector和map的遍历和删除对象

    下面小编就为大家带来一篇浅谈c++ vector和map的遍历和删除对象。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • C++实现飞机大战

    C++实现飞机大战

    这篇文章主要为大家详细介绍了C++实现飞机大战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • C++单一职责原则示例代码浅析

    C++单一职责原则示例代码浅析

    我们在设计一个类时要学会发现职责,并把那些职责相互分离,其实要去判断是否应该分离出一个类来并不难,前面说过,一个类应该只有一个引起它变化的原因,如果你能想到其它的原因也能去改变这个类,那么这个类就具有多于1个的职责,就应该考虑类的职责分离
    2023-02-02
  • 关于C语言 文件读写 feof 函数

    关于C语言 文件读写 feof 函数

    这篇文章主要给大家分享的是关于C语言文件读写 feof 函数 ,feof 是 C 语言标准库函数,其功能是检测文件结束符,如果文件结束,则返回非 0 值,否则返回 0,感兴趣的小伙伴请跟小编一起来看看下面文章的内容吧
    2021-10-10

最新评论