c++:vector类编写简易通讯录

本文介绍了一个简单的通讯录管理系统的设计与实现,包括添加、显示、查找、删除及修改联系人的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Function.cpp:通讯录功能实现相关函数

#include <iostream>
#include <vector>
#include "Pon.h"
#include "Function.h"

extern Tel pon;
extern vector<Tel> t;

using namespace std;

//-----------返回函数------------//
void Function::pause()
{
    string ch;
    cout << "input any key to return:" << endl;
    cin >> ch;

    system("clear");

    pon.interface();
    (*this).choose();

}

//-----------查找函数------------//
void Function::find()
{
    system("clear");

    int count = 0;

    string name;
    cout<<"input the name:"<<endl;
    cin>>name;

    vector<Tel>::iterator it;

    system("clear");

    cout<<"ID"<<" "<<"NAME"<<" "<<"PHONE"<<" "<<"ADDRESS"<<endl;
    cout<<"-----------------------"<<endl;

    for(it = t.begin(); it != t.end(); ++it)
    {
        if((*it) == name)
        {
            count += 1;
            cout<<*it<<endl;
        }
    }

    if(count == 0)
    {
        cout<<"cant't find the linkman"<<endl;
    }

    (*this).pause();
}

//------------删除函数-------------//
void Function::del()
{
    system("clear");
    
    if(t.empty())
    {
        cout << "the addressbook is empty" << endl;
        
        (*this).pause();
    }

    string name;
    cout << "input the name:" << endl;
    cin >> name;

    int count = 0;

    vector<Tel>::iterator it;
    cout<<"ID"<<" "<<"NAME"<<" "<<"PHONE"<<" "<<"ADDRESS"<<endl;
    cout<<"-----------------------"<<endl;
    
    for(it = t.begin(); it != t.end(); ++it)
    {
        if((*it) == name)
        {
            cout<< (*it) <<endl;
            count += 1;
        }
    }

    if(count == 0)
    {
        cout << "can't find the contact" << endl;
        (*this).pause();
    }
    else if(count == 1)
    {
        string ch;
        do
        {
            cout << "delete the contact's message?(y)or(n)" <<endl;
            cin >> ch;
        }
        while(ch != "y" && ch != "n");

        if(ch == "y")
        {
            system("clear");

            for(it = t.begin(); it != t.end();)
            {
                if((*it) == name)
                {
                    it = t.erase(it);
                    cout << "delete successed" << endl;
                }
                else
                {
                    ++it;
                }
            }

        }
        
        (*this).pause();
    }
    else
    {
        int num = 0;

        string ch;
        do
        {
            cout << "delete the contact's message?(y)or(n)" <<endl;
            cin >> ch;
        }
        while(ch != "y" && ch != "n");
        
        if(ch == "y")
        {
            int id;
            cout << "input the ID to delete" <<endl;
            cin >> id;

            system("clear");

            for(it = t.begin(); it != t.end();)
            {
                if(*it == id && *it == name)
                {
                    it = t.erase(it);
                    cout << "delete successed" << endl;
                    num++;
                }
                else
                {
                    ++it;
                }
            }

            if(num == 0)
            {
                cout <<"id error!" <<endl;
            }
        }
        (*this).pause();
    }
}

//----------显示函数-----------//
void Function::display()
{
    system("clear");
    
    if(t.empty())
    {
        cout << "the addressbook is empty" << endl;
        
        (*this).pause();
    }

    vector<Tel>::iterator it;
    cout<<"ID"<<" "<<"NAME"<<" "<<"PHONE"<<" "<<"ADDRESS"<<endl;
    cout<<"-----------------------"<<endl;
    
    for(it = t.begin(); it != t.end(); ++it)
    {
        cout<< (*it) <<endl;
    }
    
    (*this).pause();
}

//----------添加函数-------------//
void Function::add()
{
    system("clear");
    
    static int id = 0;

    if(t.empty())
    {
        id = 0;
    }

    cout<<"please input the name, phone and address"<<endl;
    
    Tel temp(++id);
    cin>>temp;
    
    sort(t.begin(),t.end());
    t.push_back(temp);

    (*this).pause();

    return;
}

//------------修改函数-------------//
void Function::change()
{
    system("clear");

    if(t.empty())
    {
        cout << "the addressbook is empty" << endl;
        
        (*this).pause();
    }

    string name;
    cout << "input the name" << endl;
    cin >> name;

    int count = 0;

    vector<Tel>::iterator it;
    cout<<"ID"<<" "<<"NAME"<<" "<<"PHONE"<<" "<<"ADDRESS"<<endl;
    cout<<"-----------------------"<<endl;
    
    for(it = t.begin(); it != t.end(); ++it)
    {
        if((*it) == name)
        {
            cout<< (*it) <<endl;
            count += 1;
        }
    }

    if(count == 0)
    {
        cout << "can't find the contact" << endl;
        (*this).pause();
    }
    else
    {
        string ch;
        do
        {
            cout << "change the contact's message?(y)or(n)" <<endl;
            cin >> ch;
        }
        while(ch != "y" && ch != "n");
        
        if(ch == "y")
        {
            int id;
            int count = 0;

            cout << "input the ID to change" <<endl;
            cin >> id;

            system("clear");

            for(it = t.begin(); it != t.end(); ++it)
            {
                if(*it == id && *it == name)
                {
                    Tel chg_name;
                    cout<< " input the name, phone and address" << endl;
                    cin >> chg_name;
                    
                    chg_name = id;

                    (*it) = chg_name;

                    count++;

                }
            }

            if(count == 0)
            {
                cout << "id error!" <<endl;
            }
        }
        (*this).pause();
    }
}

//------------选项选择函数-----------//
void Function::choose()
{
    int ch;
    cin >> ch;
    
    switch(ch)
    {
        case 1:
            {
                (*this).add();
                break;
            }
        case 2:
            {
                (*this).display();
                break;
            }
        case 3:
            {
                (*this).del();
                break;
            }
        case 4:
            {   (*this).change();
                break;
            }
        case 5:
            {
                (*this).find();
                break;
            }
        case 6:
            {
                cout << "success to exit" << endl;
                break;
            }
        default:
            break;
    }

}

Function.h:功能类定义

#ifndef _FUNCTION_H_
#define _FUNCTION_H_

class Function
{
    public:
        void choose();
        void add();
        void pause();
        void display();
        void find();
        void change();
        void del();
};

#endif

Pon.cpp:联系人类相关函数实现

#include <iostream>
#include "Pon.h"

using namespace std;

Tel::Tel() //通讯类构造函数
{

}

Tel::Tel(int num):id_(num) //通讯类转换构造函数
{

}

Tel::~Tel() //通讯类析构函数
{

}


//-----------界面函数-------------//
void Tel::interface()
{
    printf("\t\t\t           address_book           \t\t\t\n");
    printf("\t\t\t+--------------------------------+\t\t\t\n");
    printf("\t\t\t+                                +\t\t\t\n");
    printf("\t\t\t+   1.add            2.display   +\t\t\t\n");
    printf("\t\t\t+   3.delete         4.change    +\t\t\t\n");
    printf("\t\t\t+   5.find           6.exit      +\t\t\t\n");
    printf("\t\t\t+                                +\t\t\t\n");
    printf("\t\t\t+--------------------------------+\t\t\t\n");
    printf("\t\t\t+            welcome             +\t\t\t\n");
    printf("\t\t\t+--------------------------------+\t\t\t\n");

    cout<< "input your choose:" << endl;
}

//----------"=="运算符重载-------------//
bool Tel::operator==(const string &s)
{
    if((*this).name_ == s)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//----------"<"运算符重载-------------//
bool Tel::operator<(const Tel &tel) const
{
    return name_ < tel.name_;
}


//----------"="运算符重载-------------//
Tel& Tel::operator=(int id)
{
    (*this).id_ = id;
    return *this;
}

Tel& Tel::operator=(const Tel &s)
{
    (*this).id_ = s.id_;
    (*this).name_ = s.name_;
    (*this).phone_ = s.phone_;
    (*this).address_ = s.address_;
    
    return *this;
}


//----------"=="运算符重载-------------//
bool Tel::operator==(int id)
{
    if((*this).id_ == id)
    {
        return true;
    }
    else
    {
        return false;
    }
}


//----------"<<"运算符重载-------------//
ostream& operator<<(ostream &out,const Tel &tel)
{
    out<<tel.id_<<"  ";
    out<<tel.name_<<"  ";
    out<<tel.phone_<<"  ";
    out<<tel.address_<<"   ";
    return out;
}

//----------">>"运算符重载-------------//
istream& operator>>(istream &in,Tel &tel)
{
    int id;
    string buffer_name;
    string buffer_phone;
    string buffer_address;

    cout<<"name:"<<endl;
    in>>buffer_name;

    cout<<"phone:"<<endl;
    in>>buffer_phone;

    cout<<"address:"<<endl;
    in>>buffer_address;

    tel.name_ = buffer_name;
    tel.phone_ = buffer_phone;
    tel.address_ = buffer_address;

    return in;


}

Pon.h:联系人节点类

#ifndef _PON_H_
#define _PON_H_

#include <iostream>
#include <string>

#define MAX_SIZE 1024

using namespace std;

class Tel
{
    public:
        Tel();
        Tel(int num);
        ~Tel();
        friend ostream& operator<<(ostream &out,const Tel &tel);
        friend istream& operator>>(istream &in,Tel &tel);

        bool operator==(const string &s);
        
        bool operator==(int id);
        
        Tel& operator=(const Tel &s);
        
        Tel& operator=(int id);

        bool operator<(const Tel &tel) const;

        void interface();
    private:
        int id_;
        string name_;
        string phone_;
        string address_;

};


#endif


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值