C++ 通讯录实现

昨晚实现了用C++编写通讯录,深刻的感受到了封装的便利性啊,vector真是太方便了!!!

代码如下:

info.h

#ifndef _PERSON_H_
#define _PERSON_H_

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Info
{
private:
    int id;
    string name;
    string tel;
    string addr;
public:
    Info();
    ~Info();
    static int count; //记录通讯录中的人数
    int GetId();
    void SetName();
    string GetName() const;
    void SetTel();
    string GetTel() const;
    void SetAddr();
    string GetAddr() const;
    void choose();
    void insert();
    void show();
    void search();
    void interface();
    void delete_info();
    void exit_info();
    void modify();
};

#endif


info.cpp

#include "info.h"


vector<Info> per;
int Info::count = 0;


int Info::GetId()
{
    return id;
}


void Info::SetName()
{
    cout << "姓名:"; 
    cin >> name;
}
string Info::GetName() const
{
    string tmp = name;
    return tmp;
}


void Info::SetTel()
{
    cout << "电话:";
    cin >> tel;
}
string Info::GetTel() const
{
    string tmp = tel;
    return tmp;
}


void Info::SetAddr()
{
    cout << "地址:";
    cin >> addr;
}
string Info::GetAddr() const
{
    string tmp = addr;
    return tmp;
}


Info::Info()
{


}


Info::~Info()
{


}


void Info::insert()
{
    Info tmp;
    vector<Info>::iterator it;
loop:
    count++;
    cout << "ID: " << count << endl;
    tmp.SetName();
    for(it = per.begin(); it != per.end(); ++it)
    {
        if(!((it->GetName()).compare(tmp.GetName())))
{
   cout << "与已有联系人重名,请重新输入。" << endl;
   count--;
   goto loop;
}
    }
    tmp.SetTel();
    tmp.SetAddr();
    tmp.id = count;
    
    per.push_back(tmp);


    cout << "是否继续添加联系人 y/n :";
    char ch;
    cin >> ch;
    if('y' == ch || 'Y' == ch)
    {
        goto loop;
    }
   
}


void Info::show()
{
    vector<Info>::iterator it;
    if(per.empty())
    {
        cout << "通讯录暂无联系人!" << endl;
    }
    else
    {
        for(it = per.begin(); it != per.end(); ++it)
{
   cout << "ID: " << it->GetId() << endl;
   cout << "姓名:" << it->GetName() << endl;
   cout << "电话:" << it->GetTel() << endl;
   cout << "地址:" << it->GetAddr() << endl;
}
cout << "请按任意键退出" << endl;
char ch;
cin >> ch;
    }
}


void Info::search()
{
    vector<Info>::iterator it;
    if(per.empty())
    {
        cout << "通讯录暂无联系人!" << endl;
    }
    else
    {
    search_loop:
int tp =  0; //查询方式选择位
int num = 0; //查找的ID
string tn; //查找的姓名
int flag = 0; //查找成功与否标志位
        cout << "查找方式:1.ID 2.姓名" << endl;
        cin >> tp;
        if(1 == tp)
{
   cout << "请输入查找的ID:";
   cin >> num;
            for(it = per.begin(); it != per.end(); ++it)
   {
       if(it->GetId() == num)
{
                    flag = 1;
   cout << "你要找的联系人为:" <<endl;
           cout << "ID: " << it->GetId() << endl;
           cout << "姓名:" << it->GetName() << endl;
           cout << "电话:" << it->GetTel() << endl;
           cout << "地址:" << it->GetAddr() << endl;
}
   }
}
else if(2 == tp)
{
   cout << "请输入查找的姓名:";
            cin >> tn;
            for(it = per.begin(); it != per.end(); ++it)
   {
       if(!((it->GetName()).compare(tn)))
{
                    flag = 1;
   cout << "你要找的联系人为:" <<endl;
           cout << "ID: " << it->GetId() << endl;
           cout << "姓名:" << it->GetName() << endl;
           cout << "电话:" << it->GetTel() << endl;
           cout << "地址:" << it->GetAddr() << endl;
}
   }
}
else
{
   cout << "查找方式选择错误,请重新选择。" << endl;
   goto search_loop;
}


if(0 == flag)
{
   cout << "无找到此联系人" << endl;
}
else
{
   cout << "查找成功"  << endl;
}
cout << "请按任意键退出" << endl;
char ch;
cin >> ch;
    }
    
}


void Info::delete_info()
{
    vector<Info>::iterator it;
    if(per.empty())
    {
        cout << "通讯录暂无联系人!" << endl;
    }
    else
    {
    delete_loop:
int tp =  0; //删除方式选择位
int num = 0; //删除的ID
string tn; //删除的姓名
int flag = 0; //删除成功与否标志位
        cout << "删除方式:1.ID 2.姓名" << endl;
        cin >> tp;
        if(1 == tp)
{
   cout << "请输入删除的ID:";
   cin >> num;
            for(it = per.begin(); it != per.end();)
   {
       if(it->GetId() == num)
{
                    flag = 1;
   cout << "你要删除的联系人为:" <<endl;
           cout << "ID: " << it->GetId() << endl;
           cout << "姓名:" << it->GetName() << endl;
           cout << "电话:" << it->GetTel() << endl;
           cout << "地址:" << it->GetAddr() << endl;
           
   cout << "确定删除此联系人吗?y/n : ";
           char ch1;
           cin >> ch1;
           if (ch1 == 'y' || ch1 == 'Y')
           {
               it = per.erase(it);
           }
}
else
{
   ++it;
}
   }
}
else if(2 == tp)
{
   cout << "请输入删除的姓名:";
            cin >> tn;
            for(it = per.begin(); it != per.end(); )
   {
       if(!((it->GetName()).compare(tn)))
{
                    flag = 1;
   cout << "你要删除的联系人为:" <<endl;
           cout << "ID: " << it->GetId() << endl;
           cout << "姓名:" << it->GetName() << endl;
           cout << "电话:" << it->GetTel() << endl;
           cout << "地址:" << it->GetAddr() << endl;
   
   cout << "确定删除此联系人吗?y/n : ";
           char ch1;
           cin >> ch1;
           if (ch1 == 'y' || ch1 == 'Y')
           {
               it = per.erase(it);
           }
}
else
{
   ++it;
}
   }
}
else
{
   cout << "删除方式选择错误,请重新选择。" << endl;
   goto delete_loop;
}


if(0 == flag)
{
   cout << "没有找到此联系人" << endl;
        }
else
{
   cout << "删除成功" << endl;
}
cout << "请按任意键退出" << endl;
char ch;
cin >> ch;
    }
    
}
void Info::modify()
{
    vector<Info>::iterator it;
    if(per.empty())
    {
        cout << "通讯录暂无联系人!" << endl;
    }
    else
    {
    modify_loop:
        string tn;
int flag2 = 0;
int flag = 0; //修改对象查找成功与否标志位
        cout << "请输入你要编辑的人的姓名:" ;
        cin >> tn;
        for(it = per.begin(); it != per.end(); )
{
   if(!((it->GetName()).compare(tn)))
            {
       flag = 1;
cout << "你要修改的联系人为:" <<endl;
       cout << "ID: " << it->GetId() << endl;
       cout << "姓名:" << it->GetName() << endl;
       cout << "电话:" << it->GetTel() << endl;
       cout << "地址:" << it->GetAddr() << endl;
   
cout << "确定修改此联系人吗?y/n : ";
       char ch1;
       cin >> ch1;
char ch2;
       if (ch1 == 'y' || ch1 == 'Y')
       {
           cout << "你要修改的是:1.姓名 2.电话 3.地址:";
   cin >> ch2;
                    switch(ch2)
   {
       case '1':
{
                            it->SetName();
   cout << "修改成功!该联系人信息改为:" << endl;
                   cout << "ID: " << it->GetId() << endl;
                   cout << "姓名:" << it->GetName() << endl;
                   cout << "电话:" << it->GetTel() << endl;
                   cout << "地址:" << it->GetAddr() << endl;
   flag2 = 1;
   break;
}
case '2':
{
   it->SetTel();
   cout << "修改成功!该联系人信息改为:" << endl;
                   cout << "ID: " << it->GetId() << endl;
                   cout << "姓名:" << it->GetName() << endl;
                   cout << "电话:" << it->GetTel() << endl;
                   cout << "地址:" << it->GetAddr() << endl;
   flag2 = 1;
   break;
}
case '3':
{
   it->SetAddr();
   cout << "修改成功!该联系人信息改为:" << endl;
                   cout << "ID: " << it->GetId() << endl;
                   cout << "姓名:" << it->GetName() << endl;
                   cout << "电话:" << it->GetTel() << endl;
                   cout << "地址:" << it->GetAddr() << endl;
   flag2 = 1;
   break;
}
default :
{
   cout << "指令输入错误!" << endl;
   break;
}
   }
       }
else
{
   break;
}
            }
            else
   {
++it;
            }
}
if(0 == flag)
{
   cout << "没有找到此联系人" << endl;
}
if(1 == flag2)
{
   cout << "修改成功!" << endl;
}    
cout << "请按任意键退出" << endl;
char ch3;
cin >> ch3;
    }    
}




void Info::exit_info()
{
    cout << "确定退出此通讯录吗?y/n: " ;
    char ch1;
    cin >> ch1;
    if (ch1 == 'y' || ch1 == 'Y')
    {
        exit(1);
    }


}


void Info::choose()
{
    char action;
    Info tmp;
    cout << "请输入你要实现的功能(0-4):" ;
    cin >> action;


    switch(action)
    {
        case '1':
{
   tmp.insert();
   tmp.interface();
            choose();
   break; 
}
case '2':
{
   tmp.show();
   tmp.interface();
   choose();
   break;
}
case '3':
{
            tmp.delete_info();
   tmp.interface();
   choose();
}
case '4':
{
   tmp.search();
   tmp.interface();
   choose();
   break;
}
case '5':
{
   tmp.modify();
   tmp.interface();
   choose();
   break;
}
case '6':
{
   tmp.exit_info();
   break;
}
default:
{
   cout << "输入指令有误,请重新输入!" << endl;
   choose();
   break;
}
    }
}




void Info::interface()
{
    system("clear");


    printf("\n");
    printf("\n");
    printf("          |***********************************************|\n");
    printf("          |*          多 功 能 电 子 通 讯 录            *|\n");
    printf("          |*                                             *|\n");
    printf("          |*                              版本号:V_1.0.0*|\n");
    printf("          |***********************************************|\n");
    printf("          |*                                             *|\n");
    printf("          |*      功能选择:   1. 添加好友信息            *|\n");
    printf("          |*                  2. 查看好友信息            *|\n");
    printf("          |*                  3. 删除好友信息            *|\n");
    printf("          |*                  4. 搜索好友信息            *|\n");
    printf("          |*                  5. 修改好友信息            *|\n");
    printf("          |***********************************************|\n");
    printf("          |*      请输入你想要实现的功能:               *|\n");
    printf("          |*                                             *|\n");
    printf("          |*    1添加 2查看 3删除 4搜索 5修改 6退出      *|\n");
    printf("          |***********************************************|\n");


}




main.cpp

#include "info.h"


int main()
{
    Info s;
    s.interface();
    s.choose();


    return 0;
}




  • 6
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
很全面的通讯录。各种项目还可自行修改。// Lab_link.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "Lab_link.h" #include "Lab_linkDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp BEGIN_MESSAGE_MAP(CLab_linkApp, CWinApp) //{{AFX_MSG_MAP(CLab_linkApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp construction CLab_linkApp::CLab_linkApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CLab_linkApp object CLab_linkApp theApp; ///////////////////////////////////////////////////////////////////////////// // CLab_linkApp initialization BOOL CLab_linkApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif CLab_linkDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the applicati
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值