栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

2022-7-20-----C++------用链表实现通讯录功能

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2022-7-20-----C++------用链表实现通讯录功能

#include 
using namespace std;


struct PersionInfo;
bool is_number(string str);
void showTip();
PersionInfo* endP(PersionInfo* head);
void add_persionInfo(PersionInfo* head);
void show_persionInfo(PersionInfo* head);


struct PersionInfo
{
    int num;
    string name;
    PersionInfo* next;
};

void showTip() {
    cout << "*******************" << endl;
    cout << "*******************" << endl;
    cout << "请选择功能" << endl;
    cout << "1、添加联系人" << endl;
    cout << "2、显示联系人" << endl;
    cout << "3、删除联系人" << endl;
    cout << "4、查找联系人" << endl;
    cout << "5、修改联系人" << endl;
    cout << "6、清空联系人" << endl;
    cout << "7、退出通讯录" << endl;
    cout << "*******************" << endl;
    cout << "*******************" << endl;
}



bool is_number(string str)
{
    if (str.c_str()[0] != 45)
    {
        for (int i = 0; i < str.length(); i++)
        {
            if (str.c_str()[i] < '0' || str.c_str()[i] > '9')
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        for (int i = 1; i < str.length(); i++)
        {
            if (str.c_str()[i] < '0' || str.c_str()[i] > '9')
            {
                return false;
            }
        }
        return true;
    }
}


void show_persionInfo(PersionInfo* head) {
    PersionInfo* q = head->next;
    PersionInfo* p;
    if (q == NULL) {
        cout <<"无联系人" << endl;
    }
    while (q != NULL)
    {
        cout << q->name <<"--------->"<num << endl;
        p = q;
        q = q->next;
        if (q == NULL) {
            cout<<"尾节点为:"<name << endl;
        }
    }
}


PersionInfo* endP(PersionInfo* head) {
    PersionInfo* q = head->next;
    if (q == NULL) {
        return head;
    }
    PersionInfo* p;
    while (q != NULL)
    {
        p = q;
        q = q->next;
        if (q == NULL) {
            cout <<"获取尾节点!!!" << endl;
            return p;
        }
    }
}

void add_persionInfo(PersionInfo* head) {
    PersionInfo* p = NULL;
    PersionInfo* q;

    int num = 0;
    string name = "xxx";
    string input;
    q = endP(head);
   
    cout << "请输入号码" << endl;
    cin >> input;

    while (input != "-1")
    {
        if (is_number(input)) {
            num = atoi(input.c_str());
            cout << "请输入姓名" << endl;
            cin >> name;
            p = new PersionInfo;
            p->num = num;
            p->name = name;
            p->next = NULL;

            q->next = p;
            q = q->next;
     
        }
        else {
            cout << "号码请输入数字!" << endl;
        }
        cout << "请输入号码" << endl;
        cin >> input;
    }
}

void  find_PersionInfo(PersionInfo* head, int findnum) {
    PersionInfo* q = head->next;
    while ( q != NULL )
    {
        if (q->num == findnum) {
            cout<<"查询到:" << endl;
            cout << q->name << "--------->" << q->num << endl;
            break;
        }
        else {
            q = q->next;
        }
    }
};

void  changNum_PersionInfo
(PersionInfo* head, int findnum) {
    PersionInfo* q = head->next;
    while (q != NULL)
    {
        if (q->num == findnum) {
            cout << "请输入想要修改的数据:" << endl;
            cin >> q->num;
            cin >> q->name;
            cout << q->name << "--------->" << q->num << endl;
            break;
        }
        else {
            q = q->next;
        }
    }
};
int main()
{
    showTip();
    PersionInfo* head = NULL;
    head = new PersionInfo;
    head->next = NULL;
    int sel = 0;
    cin >> sel;
    while ( sel != 7 ) {
        if (sel == 1) {
            add_persionInfo( head );
        }
        else if (sel == 2) {
            show_persionInfo(head);
        }
        else if (sel == 3) {

        }
        else if (sel == 4) {
            string findnum = "";
            cin >> findnum;
            if (is_number(findnum)) {
                find_PersionInfo(head, atoi(findnum.c_str()));
            }
            else
            {
                cout << "号码请输入数字!" << endl;
            }
            
        }
        else if (sel == 5) {
            string changNum = "";
            cin >> changNum;
            if (is_number(changNum)) {
                changNum_PersionInfo(head, atoi(changNum.c_str()));
            }
            else
            {
                cout << "号码请输入数字!" << endl;
            }
        }
        else if (sel == 6) {
            head = NULL;
            head = new PersionInfo;
            head->next = NULL;
        }
        else if (sel == 2) {

        }
        showTip();
        cin >> sel;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1022222.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号