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++示例讲解观察者设计模式

    C++示例讲解观察者设计模式

    观察者模式是极其重要的一个设计模式,也是我几年开发过程中使用最多的设计模式,本文首先概述观察者模式的基本概念和Demo实现,接着是观察者模式在C++中的应用,最后是对观察者模式的应用场景和优缺点进行总结
    2022-12-12
  • 详解C语言#define预处理宏定义

    详解C语言#define预处理宏定义

    本文主要介绍了C语言#define预处理宏定义,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • OpenCV cv.Mat与.txt文件数据的读写操作

    OpenCV cv.Mat与.txt文件数据的读写操作

    这篇文章主要介绍了OpenCV cv.Mat 与 .txt 文件数据的读写操作,现在分享给大家,也给大家做个参考
    2018-05-05
  • C++获取当前进程IAT的方法

    C++获取当前进程IAT的方法

    这篇文章主要介绍了C++获取当前进程IAT的方法,实例讲述了IAT(导入地址表)的获取方法,在Windows应用程序开发中有着非常实用的应用价值,需要的朋友可以参考下
    2014-10-10
  • C++数据结构与算法之双缓存队列实现方法详解

    C++数据结构与算法之双缓存队列实现方法详解

    这篇文章主要介绍了C++数据结构与算法之双缓存队列实现方法,结合实例形式分析了双缓存队列的原理、实现方法与相关注意事项,需要的朋友可以参考下
    2017-08-08
  • 深入理解c++中virtual关键字

    深入理解c++中virtual关键字

    本篇文章主要是对c++中virtual关键字进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-02-02
  • C语言基础函数用法示例详细解析

    C语言基础函数用法示例详细解析

    最接地气的C函数基础介绍,此处对于函数的相关知识点做一些简要的介绍,作者实属初学,写博客也是作者学习的一个过程,难免文章中有内容理解不到位或者有不当之处,还请朋友们不吝指正
    2021-11-11
  • 在1个Matlab m文件中定义多个函数直接运行的操作方法

    在1个Matlab m文件中定义多个函数直接运行的操作方法

    这篇文章主要介绍了如何在1个Matlab m文件中定义多个函数直接运行,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • C++实现静态链表

    C++实现静态链表

    这篇文章主要为大家详细介绍了C++实现静态链表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • 详解C++ 参数的三种传递方式和应用场景

    详解C++ 参数的三种传递方式和应用场景

    这篇文章主要介绍C++ 参数的三种传递方式和应用场景,C++ 参数的三种传递方式分别是值传递、指针传递和引用传递,感兴趣的同学可以参考阅读下
    2023-06-06

最新评论