C++實(shí)現(xiàn)簡(jiǎn)單通訊錄
本文實(shí)例為大家分享了C++實(shí)現(xiàn)簡(jiǎn)單通訊錄的具體代碼,供大家參考,具體內(nèi)容如下
說(shuō)明:
1 程序中運(yùn)用到兩個(gè)類(lèi),一個(gè)是Person類(lèi),另一個(gè)是List類(lèi)。前者存儲(chǔ)用戶信息,后者主要用于操作,如增刪改查等。但由于本程序中沒(méi)有涉及到太復(fù)雜的功能,用戶信息可以由一個(gè)簡(jiǎn)單的結(jié)構(gòu)體表示,但是為了以后拓展方便,和達(dá)到學(xué)習(xí)運(yùn)算符重載的目的,還是使用了類(lèi)。
2 List類(lèi)中的Reflush()方法用戶刷新文件內(nèi)容,即每次修改了vector后要將最新內(nèi)容寫(xiě)入到文件。因此增刪改操作中都要調(diào)用該操作,這種方法在數(shù)據(jù)庫(kù)開(kāi)發(fā)中常用到,以小見(jiàn)大。
3 setout()方法設(shè)置字符左對(duì)齊,便于美觀。另外std::cout.width(15)設(shè)置輸出字符域?qū)挾龋粚?duì)下一次輸出有效。
4 判斷文本文件是否為空還有另一種方法,即string類(lèi)中的empty()方法,但為了讀取方便沒(méi)有采用。
5 其實(shí)對(duì)于通訊錄的操作只是在類(lèi)內(nèi)的vector容器中進(jìn)行,只有最后刷新的時(shí)候同步到磁盤(pán)文件中。
6 一些函數(shù)中設(shè)置多個(gè)返回值有利于判斷操作的情況。
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)算符,本程序中并沒(méi)有用到 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();//從磁盤(pán)讀取文件 void Add(); void Reflush();//刷新數(shù)據(jù),即重新寫(xiě)入磁盤(pán) 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()//輸出格式控制,即左對(duì)齊 { 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;//磁盤(pán)中不存在文件的話則創(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);//刪除對(duì)應(yīng)項(xià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 : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 // #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<<"沒(méi)有該選項(xiàng)請(qǐng)重輸!\n"; break; } cout << "請(qǐng)輸入選項(xiàng):\n"; cin >> num; } system("pause"); return 0; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語(yǔ)言
下一篇:C語(yǔ)言用函數(shù)實(shí)現(xiàn)電話簿管理系統(tǒng)
本文標(biāo)題:C++實(shí)現(xiàn)簡(jiǎn)單通訊錄
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/73.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見(jiàn)的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?