C++容器vector实现通讯录功能

 更新时间:2020年4月25日 17:24  点击:2147

之前学习C语言的时候,用链表实现过通讯录的基本功能。最近写了一个C++版本的通讯录,参考代码如下所示。

main.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : main.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 16时47分52秒
Description : 主函数
Funcion List : main()
*****************************************************/
 
#include "../../include/head.h"
 
personMessage pep;
vector<personMessage> person;
vector<personMessage>::iterator it;
 
int main()
{
 //personMessage pep;
 //vector<personMessage> person;
 
 char ch = 0;
 
 //system("clear");
 
 while(ch != 'q')
 {
 if((ch != 'a') && (ch != 'c') && (ch != 'd') && (ch != 'f'))
 {
  system("clear");
  ch = book_ui();
 }
 
 switch(ch)
 {
      case 'a':
  {
  ch = add_person();
  break;
  }
  case 'c':
  {
  ch = change_person();
  break;
  }
  case 'd':
  {
  ch = delete_person();
  break;
  }
  case 'e':
  {
  ch = display_person();
  break;
  }
  case 'f':
  {
  ch = find_person();
  break;
  }
  case 'q':
  {
  cout << "Byebye!" << endl;
  return 0;
  break;
  }
  default:
  {
  cout << "input error!" << endl;
  break;
  }
 }
 }
 
  return 0;
}

head.h

/*****************************************************
Copyright (C): 2017-2018 
File name  : head.h
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 17时11分29秒
Description : 
Funcion List : 
*****************************************************/
 
#ifndef __HEAD_H__
#define __HEAD_H__
 
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
 
#include <stdio.h>
#include <string.h>
 
using namespace std;
 
class personMessage
{
public:
 personMessage();
 personMessage(string s);
 ~personMessage();
 
 personMessage& operator=(string s);
 personMessage& operator=(personMessage& other);
 
 /* sort排序算法需要重载'<',注意加const! */
 bool operator<(const personMessage& p) const;
 bool operator>(const personMessage& p) const;
 bool operator<=(const personMessage& p) const;
 bool operator>=(const personMessage& p) const;
  
 bool operator==(string s);
 
 friend istream& operator>>(istream& in, personMessage& p);
 friend ostream& operator<<(ostream& out, personMessage& p);
 
 int selectFlag; //用来选择哪一个私有成员!
 
private:
 string name_;
 string addr_;
 string phone_;
};
 
extern personMessage pep;
extern vector<personMessage> person;
extern vector<personMessage>::iterator it;
 
extern char book_ui();
extern char add_person();
extern char change_person();
extern char delete_person();
extern char display_person();
extern char find_person();
 
#endif

book.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : book.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时53分19秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
personMessage::personMessage() : selectFlag(0)
{
 cout << "default coonstructor!" << endl;
}
 
personMessage::personMessage(string s)
{
 name_ = s;
}
 
personMessage::~personMessage()
{
 cout << "destroy person message!" << endl;
}
 
#if 1
personMessage& personMessage::operator=(string s)
{
 name_ = s;
 return *this;
}
#endif
 
personMessage& personMessage::operator=(personMessage& other)
{
 if(this == &other)
 {
 return *this;
 }
 
 name_ = other.name_;
 addr_ = other.addr_;
 phone_ = other.phone_;
 return *this;
}
 
bool personMessage::operator>(const personMessage& p) const
{
 return name_ > p.name_;
}
 
bool personMessage::operator>=(const personMessage& p) const
{
 return name_ >= p.name_;
}
 
bool personMessage::operator<(const personMessage& p) const
{
 return name_ < p.name_;
}
 
bool personMessage::operator<=(const personMessage& p) const
{
 return name_ <= p.name_;
}
 
bool personMessage::operator==(string s)
{
 if(selectFlag == 1)
 {
 return name_ == s;
 }
 else if(selectFlag == 2)
 {
 return addr_ == s;
 }
 else if(selectFlag == 3)
 {
 return phone_ == s;
 }
 else
 {
 return false;
 }
}
 
#if 1
istream& operator>>(istream& in, personMessage& p)
{
 string name;
 string addr;
 string phone;
 
 cout << "请输入新的成员名字:" << endl;
 in >> name;
 p.name_ = name;
 
 cout << "请输入新的成员地址:" << endl;
 in >> addr;
 p.addr_ = addr;
 
 cout << "请输入新的成员电话:" << endl;
 in >> phone;
 p.phone_ = phone;
 
 return in;
}
 
ostream& operator<<(ostream& out, personMessage& p)
{
 out << "名字: " << p.name_ << endl;
 out << "地址: " << p.addr_ << endl;
 out << "电话: " << p.phone_ << endl;
 
 return out;
}
#endif

book_ui.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : book_ui.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 16时49分50秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char book_ui()
{
 char ch = 0;
 
 cout << " ____________________________________" << endl;
 cout << "|                  |" << endl;
 cout << "|    欢迎进入通讯录系统 v2.0   |" << endl;
 cout << "|                  |" << endl;
 cout << "|====================================|" << endl;
 cout << "|                  |" << endl;
 cout << "|     a. 增加新的成员      |" << endl;
 cout << "|     c. 修改成员信息      |" << endl;
 cout << "|     d. 删除成员信息      |" << endl;
 cout << "|     e. 展示所有成员      |" << endl;
 cout << "|     f. 查找成员信息      |" << endl;
 cout << "|     q. 退出通讯录系统     |" << endl;
 cout << "|____________________________________|" << endl;
 cout << endl << "请输入你的选择:" << endl;
 cin >> ch;
 
 return ch;
}

add_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : add_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 17时22分56秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char add_person()
{
 cout << "This is add person!" << endl;
 
#if 0
 getchar();
 string tmp;
 
 getline(cin, tmp);
 
 cout << "tmp = " << tmp << endl;
 
 pep = tmp;
#endif
 
 /* 输入新的成员信息 */
 cin >> pep;
 cout << pep << endl;
 
 /* 向vector插入元素 */
 person.push_back(pep);
 
 cout << "插入成员信息成功!" << endl;
 
 char ch = 0;
 
 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;
 
 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'a';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

delete_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : delete_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时29分33秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char delete_person()
{
 cout << "This is delete person!" << endl;
 
 /* 删除成员的信息 */
 string pep_info;
 
 int d_flag = 0;
 int d_key = 0;
 
 cout << "请输入你想要查找的方式(1-姓名/2-地址/3-电话):" << endl;
 cin >> d_key;
 
 switch(d_key)
 {
 case 1:
 {
  cout << "请输入你想要删除成员的名字:" << endl;
  cin >> pep_info;
  break;
 }
 case 2:
 {
  cout << "请输入你想要删除成员的地址:" << endl;
  cin >> pep_info;
  break;
 }
 case 3:
 {
  cout << "请输入你想要删除成员的电话:" << endl;
  cin >> pep_info;
  break;
 }
 default:
 {
  cout << "输入有误!" << endl;
  return 0;
  break;
 }
 }
 
 for(it = person.begin(); it != person.end(); )
 {
 it->selectFlag = d_key;
 if(*it == pep_info)
 {
  person.erase(person.begin()+d_flag, person.begin()+d_flag+1);
  cout << "删除成员信息成功!" << endl;
 }
 else
 {
  ++it;
  d_flag++;
 }
 }
 
 char ch = 0;
 
 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;
 
 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'd';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

change_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : change_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时20分15秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char change_person()
{
 cout << "This is change person!" << endl;
 
 /* 修改成员的信息 */
 string pep_info;
 
 int ch_flag = 0;
 int c_key = 0;
 
 cout << "请输入你想要查找的方式(1-姓名/2-地址/3-电话):" << endl;
 cin >> c_key;
 
 switch(c_key)
 {
 case 1:
 {
  cout << "请输入你想要修改成员的名字:" << endl;
  cin >> pep_info;
  break;
 }
 case 2:
 {
  cout << "请输入你想要修改成员的地址:" << endl;
  cin >> pep_info;
  break;
 }
 case 3:
 {
  cout << "请输入你想要修改成员的电话:" << endl;
  cin >> pep_info;
  break;
 }
 default:
 {
  cout << "输入有误!" << endl;
  return 0;
  break;
 }
 }
 
 for(it = person.begin(); it != person.end(); ++it)
 {
 it->selectFlag = c_key;
 if(*it == pep_info)
 {
  ch_flag = 1;
  cin >> *it;
  cout << "修改成员信息成功!" << endl;
 }
 }
 
 if(ch_flag != 1)
 {
 cout << "没有找到该成员!" << endl;
 }
 
 char ch = 0;
 
 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;
 
 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'c';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

find_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : find_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时21分59秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char find_person()
{
 cout << "This is find person!" << endl;
 
 int f_key = 0;
 int f_flag = 0;
 /* 输入查找的姓名 */
 string f_info;
 
 cout << "请输入查找方式(1-姓名/2-地址/3-电话)" << endl;
 cin >> f_key;
 
 switch(f_key)
 {
 case 1:
 {
  cout << "请输入你想要查找成员的名字:" << endl;
  cin >> f_info;
  break;
 }
 case 2:
 {
  cout << "请输入你想要查找成员的地址:" << endl;
  cin >> f_info;
  break;
 }
 case 3:
 {
  cout << "请输入你想要查找成员的名字:" << endl;
  cin >> f_info;
  break;
 }
 default:
 {
  cout << "输入有误!" << endl;
  return 0;
  break;
 }
 }
 
 //pep.selectFlag = 2; //it迭代器在变化,不能直接赋值。
 
 for(it = person.begin(); it != person.end(); ++it)
 {
 it->selectFlag = f_key;
 if(*it == f_info)
 {
  f_flag = 1;
  cout << "找到该成员!" << endl;
  cout << *it << endl;
 }
 }
 
 if(f_flag != 1)
 {
 cout << "没有找到该成员!" << endl;
 }
 
 char ch = 0;
 
 cout << "是否返回主菜单?(y/n)" << endl;
 getchar();
 cin >> ch;
 
 if(ch == 'y')
 {
 return 0;
 }
 else if(ch == 'n')
 {
 return 'f';
 }
 else
 {
 cout << "输入错误!" << endl;
 return 0;
 }
}

display_person.cpp

/*****************************************************
Copyright (C): 2017-2018 
File name  : display_person.cpp
Author    : Zhengqijun
Date     : 2017年02月12日 星期日 18时23分04秒
Description : 
Funcion List : 
*****************************************************/
 
#include "../../include/head.h"
 
char display_person()
{
 cout << "This is display person!" << endl;
 
 sort(person.begin(), person.end());
 
 for(it = person.begin(); it != person.end(); ++it)
 {
 cout << *it << endl;
 }
 
 char ch = 0;
 cout << "按任意键返回" << endl;
 getchar();
 cin >> ch;
 return 0;
}

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

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • python实现学生通讯录管理系统

    这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25