欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實現(xiàn)簡單通訊錄

 更新時間:2019年12月18日 10:10:47   作者:zebra_zzh  
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)簡單通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)簡單通訊錄的具體代碼,供大家參考,具體內(nèi)容如下

說明:

1 程序中運(yùn)用到兩個類,一個是Person類,另一個是List類。前者存儲用戶信息,后者主要用于操作,如增刪改查等。但由于本程序中沒有涉及到太復(fù)雜的功能,用戶信息可以由一個簡單的結(jié)構(gòu)體表示,但是為了以后拓展方便,和達(dá)到學(xué)習(xí)運(yùn)算符重載的目的,還是使用了類。

2 List類中的Reflush()方法用戶刷新文件內(nèi)容,即每次修改了vector后要將最新內(nèi)容寫入到文件。因此增刪改操作中都要調(diào)用該操作,這種方法在數(shù)據(jù)庫開發(fā)中常用到,以小見大。

3 setout()方法設(shè)置字符左對齊,便于美觀。另外std::cout.width(15)設(shè)置輸出字符域?qū)挾?,只對下一次輸出有效?/p>

4 判斷文本文件是否為空還有另一種方法,即string類中的empty()方法,但為了讀取方便沒有采用。

5 其實對于通訊錄的操作只是在類內(nèi)的vector容器中進(jìn)行,只有最后刷新的時候同步到磁盤文件中。

6 一些函數(shù)中設(shè)置多個返回值有利于判斷操作的情況。

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);//重載==運(yùn)算符,本程序中并沒有用到
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();//刷新數(shù)據(jù),即重新寫入磁盤
 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);//字域?qū)挾葹?5
 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;//磁盤中不存在文件的話則創(chuàng)建
 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);//刪除對應(yīng)項
  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 : 定義控制臺應(yīng)用程序的入口點。
//
 
#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;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言制作掃雷游戲(圖形庫)

    C語言制作掃雷游戲(圖形庫)

    這篇文章主要為大家詳細(xì)介紹了C語言制作掃雷游戲,結(jié)合圖形庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 七夕表白! C語言實現(xiàn)愛情紅玫瑰

    七夕表白! C語言實現(xiàn)愛情紅玫瑰

    這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)愛情紅玫瑰,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • C++實現(xiàn)連連看游戲

    C++實現(xiàn)連連看游戲

    這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)連連看游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C++ Qt開發(fā)之使用QTcpSocket實現(xiàn)TCP網(wǎng)絡(luò)通信

    C++ Qt開發(fā)之使用QTcpSocket實現(xiàn)TCP網(wǎng)絡(luò)通信

    Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應(yīng)用程序,本文主要為大家介紹了如何運(yùn)用QTcpSocket組件實現(xiàn)基于TCP的網(wǎng)絡(luò)通信功能,需要的可以參考下
    2024-03-03
  • C語言中pthread_exit()函數(shù)實現(xiàn)終止線程

    C語言中pthread_exit()函數(shù)實現(xiàn)終止線程

    本文主要介紹了C語言中pthread_exit()函數(shù)實現(xiàn)終止線程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 基于c++計算矩形重疊面積代碼實例

    基于c++計算矩形重疊面積代碼實例

    這篇文章主要介紹了基于c++計算矩形重疊面積代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • C語言中數(shù)組排序淺析

    C語言中數(shù)組排序淺析

    這篇文章主要為大家介紹了C語言算法練習(xí)中數(shù)組元素排序的四種類型,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C語言有一定幫助,需要的可以參考一下
    2022-12-12
  • 解決pip?install?dlib報錯C++11?is?required?to?use?dlib

    解決pip?install?dlib報錯C++11?is?required?to?use?dlib

    這篇文章主要介紹了在使用pip?install?dlib安裝dlib的時候報錯C++11?is?required?to?use?dlib的解決方法,需要的的小伙伴可以參考一下,希望對你有所幫助
    2022-02-02
  • OpenCV實現(xiàn)相機(jī)標(biāo)定板

    OpenCV實現(xiàn)相機(jī)標(biāo)定板

    這篇文章主要為大家詳細(xì)介紹了OpenCV實現(xiàn)相機(jī)標(biāo)定板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C語言實現(xiàn)時間戳轉(zhuǎn)日期的算法(推薦)

    C語言實現(xiàn)時間戳轉(zhuǎn)日期的算法(推薦)

    下面小編就為大家?guī)硪黄狢語言實現(xiàn)時間戳轉(zhuǎn)日期的算法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06

最新評論