C++实现简单通讯录

 更新时间:2019年12月18日 10:10:47   作者:zebra_zzh  
这篇文章主要为大家详细介绍了C++实现简单通讯录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现简单通讯录的具体代码,供大家参考,具体内容如下

说明:

1 程序中运用到两个类,一个是Person类,另一个是List类。前者存储用户信息,后者主要用于操作,如增删改查等。但由于本程序中没有涉及到太复杂的功能,用户信息可以由一个简单的结构体表示,但是为了以后拓展方便,和达到学习运算符重载的目的,还是使用了类。

2 List类中的Reflush()方法用户刷新文件内容,即每次修改了vector后要将最新内容写入到文件。因此增删改操作中都要调用该操作,这种方法在数据库开发中常用到,以小见大。

3 setout()方法设置字符左对齐,便于美观。另外std::cout.width(15)设置输出字符域宽度,只对下一次输出有效。

4 判断文本文件是否为空还有另一种方法,即string类中的empty()方法,但为了读取方便没有采用。

5 其实对于通讯录的操作只是在类内的vector容器中进行,只有最后刷新的时候同步到磁盘文件中。

6 一些函数中设置多个返回值有利于判断操作的情况。

Person.h 与cpp文件: 

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
 
class Person
{
public:
 std::string name;
 std::string tel;
public:
 Person();
 ~Person();
 int operator==(const Person& p);//重载==运算符,本程序中并没有用到
private:
 
};
 
 
#endif // !PERSON_H_
#include "Person.h"
 
Person::Person()
{
}
 
Person::~Person()
{
}
 
int Person::operator==(const Person& p)
{
 if (this->name == p.name)
 {
 if (this->tel == p.tel)
  return 0;
 else
  return -1;
 }
 else
 return -2;
}

List.h文件: 

#ifndef LIST_H_
#define LIST_H_
#include <vector>
#include "Person.h"
class List
{
public:
 List();
 ~List();
 void Showfile();//显示通讯录
 int Readfile();//从磁盘读取文件
 void Add();
 void Reflush();//刷新数据,即重新写入磁盘
 void Del();
 void Search();
private:
 std::vector<Person> myfile;
};
 
inline void setout();//输出格式控制
#endif

List.cpp文件: 

#include "List.h"
#include <iostream>
#include <fstream>
#include <string>
 
List::List()
{
}
 
List::~List()
{
}
 
void setout()//输出格式控制,即左对齐
{
 std::cout.setf(std::ios_base::left, std::ios_base::adjustfield);
}
void List::Showfile()
{
 std::vector<Person>::iterator iter;
 setout();
 for (iter = this->myfile.begin(); iter != this->myfile.end(); iter++)
 {
 std::cout.width(15);//字域宽度为15
 std::cout << iter->name;
 std::cout.width(15);
 std::cout << iter->tel << "\n";
 }
}
 
int List::Readfile()
{
 std::fstream readfile("mylist.txt");
 int rows = 0;
 if (readfile)//如果文件存在
 {
 std::cout << "*******Telephone book********\n";
 std::cout << "name:" << "\t\t" << "phone:" << "\n";
 Person p;
 if (!(readfile >> p.name >> p.tel))//如果第一次读取为空
 {
  std::cout << "\tNULL\n";
  return 1;
 }
 myfile.push_back(p);
 rows++;
 while(readfile>>p.name>>p.tel)//读取后存入vector容器中
 {
  rows++;
  myfile.push_back(p);
 }
 
 this->Showfile();
 std::cout << "Total:\t" << rows << "\tinfos\n";
 readfile.close();
 return rows;
 }
 else
 {
 std::ofstream outfile;//磁盘中不存在文件的话则创建
 outfile.open("mylist.txt");
 if (!outfile.is_open())
 {
  std::cout << "file is not created!\n";
  return -1;
 }
 else
 {
  std::cout << "file not exist but we have created one for you!\n";
  std::cout << "*******Telephone book********\n";
  std::cout << "name:" << "\t\t" << "phone:" << "\n";
  std::cout << "\tNULL\n";
 }
 outfile.close();
 }
 return 2;
}
void List::Reflush()
{
 std::ofstream outfile("mylist.txt");
 std::vector<Person>::iterator iter;
 setout();
 for (iter = this->myfile.begin(); iter != this->myfile.end(); iter++)
 {
 outfile.width(15);
 outfile << iter->name;
 outfile.width(15);
 outfile << iter->tel << "\n";
 }
 outfile.close();
}
 
void List::Add()
{
 Person p;
 std::cout << "please input the name:\n";
 std::cin >> p.name;
 std::cout << "please input the phone\n";
 std::cin >> p.tel;
 std::cout << "sucessfully created!\n";
 myfile.push_back(p);
 this->Reflush();
}
 
void List::Del()
{
 if (myfile.empty())
 {
 std::cout << "no info to del!\n";
 return;
 }
 std::string name;
 std::cout << "please input the name you want you del:\n";
 std::cin >> name;
 std::vector<Person>::iterator iter;
 for (iter = this->myfile.begin(); iter != this->myfile.end();)
 {
 if (iter->name == name)
 {
  myfile.erase(iter);//删除对应项
  std::cout << "sucessfully delete!\n";
  this->Reflush();
  return;
 }
 else
  ++iter;
 }
 std::cout << "no info matches!\n";
}
 
void List::Search()
{
 std::string name;
 std::cout << "please input the name you want to search:\n";
 std::cin >> name;
 std::vector<Person>::iterator iter;
 for (iter = this->myfile.begin(); iter != this->myfile.end(); iter++)
 {
 if (name == iter->name)
 {
  std::cout << iter->name << "\t\t" << iter->tel << "\n";
  return;
 }
 }
 std::cout << "no info matches!\n";
}

main文件: 

// contact.cpp : 定义控制台应用程序的入口点。
//
 
#include "stdafx.h"
#include "List.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
 
int Menu()
{
 int num;
 cout << "********************" << endl;
 cout << "*  1   ADD   *" << endl;
 cout << "*  2   DEL   *" << endl;
 cout << "*  3  SEARCH  *" << endl;
 cout << "*  4   SHOW   *" << endl;
 cout << "*  5   EXIT   *" << endl;
 cout << "********************" << endl;
 cout << "input the num:";
 cin >> num;
 return num;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
 List mylist;
 mylist.Readfile();
 int num = Menu();
 bool flags = 1;
 while (flags)
 {
 switch (num)
 {
 case 1:
  mylist.Add();
  break;
 case 2:
  mylist.Del();
  break;
 case 3:
  mylist.Search();
  break;
 case 4:
  mylist.Showfile();
  break;
 case 5:
  cout << "Bye.\n";
  return 0;
 default:
  cout<<"没有该选项请重输!\n";
  break;
 }
 cout << "请输入选项:\n";
 cin >> num;
 }
 system("pause");
 return 0;
}

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

相关文章

  • undefined reference to `SetPduPowerConsumptionCnt''错误的解决方法

    undefined reference to `SetPduPowerConsumptionCnt''错误的解决方法

    编译时出现undefined reference to `SetPduPowerConsumptionCnt'错误要如何解决呢?有没有什么好的解决方法?下面小编就为大家解答吧,如果你也遇到了这种情况,可以过来参考下
    2013-07-07
  • C语言编程中对目录进行基本的打开关闭和读取操作详解

    C语言编程中对目录进行基本的打开关闭和读取操作详解

    这篇文章主要介绍了C语言编程中对目录进行基本的打开关闭和读取操作,是C语言入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • C语言实例问题探究字符串函数的应用

    C语言实例问题探究字符串函数的应用

    字符串函数(String processing function)也叫字符串处理函数,指的是编程语言中用来进行字符串处理的函数,如C,pascal,Visual以及LotusScript中进行字符串拷贝,计算长度,字符查找等的函数
    2022-04-04
  • C语言实现高精度加减法

    C语言实现高精度加减法

    这篇文章主要为大家详细介绍了C语言实现高精度加减法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-05-05
  • C语言将24小时制转换为12小时制的方法

    C语言将24小时制转换为12小时制的方法

    这篇文章主要介绍了C语言将24小时制转换为12小时制的方法,涉及C语言针对时间的相关操作技巧,非常简单实用,需要的朋友可以参考下
    2015-07-07
  • C++11显示类型转换的优点

    C++11显示类型转换的优点

    这篇文章主要介绍了C++11显示类型转换的优点,帮助大家更好的理解和学习c++,感兴趣的朋友可以了解下
    2020-08-08
  • 关于C++ string和c类型字符数组的对比

    关于C++ string和c类型字符数组的对比

    下面小编就为大家带来一篇关于C++ string和c类型字符数组的对比。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • QT出现没有MySQL驱动手动编译详细步骤

    QT出现没有MySQL驱动手动编译详细步骤

    这篇文章主要给大家介绍了关于QT出现没有MySQL驱动手动编译详细步骤的相关资料,文中通过图文介绍的非常详细,对大家学习或者使用QT具有一定的参考学习价值,需要的朋友可以参考下
    2023-04-04
  • C++ Easylogging++日志库配置使用超详细讲解

    C++ Easylogging++日志库配置使用超详细讲解

    这篇文章主要介绍了C++ Easylogging++日志库配置使用,Easylogging++是用于C++应用程序的单头高效日志库。它非常强大,高度可扩展并且可以根据用户的要求进行配置
    2022-11-11
  • C/C++宏定义的可变参数详细解析

    C/C++宏定义的可变参数详细解析

    在1999年版本的ISO C 标准中,宏可以象函数一样,定义时可以带有可变参数。宏的语法和函数的语法类似
    2013-09-09

最新评论