Address Book: C ++ implementation

Source: Internet
Author: User

 

// Keep the source author and other relevant information for any source code reprinted.

// Author: chlaws

// Running environment: Visual Studio 2008

// Project: Address Book

// Description: it can be used as a project design or graduation thesis.

// Note: the original ideas are not provided here. If you do not know how to ask during reading

// Main. CPP <br/> # ifndef headfile <br/> # define headfile <br/> # include <iostream> <br/> # include <string> <br/> # include <algorithm> <br/> # include <fstream> <br/> # include <list> <br/> # include <Io. h> <br/> # include <iterator> <br/> # include <cassert> <br/> # include <sstream> <br/> using namespace STD; <br/> # endif <br/> # include "telbook. H "<br/> # include" contacts. H "<br/> int main () <br/>{< br/> telbook A1 (" ASD. dat "); <br/> a1.menu (); </P> <p> return 0; <br/>} 

 

// Contacts. h <br/> # ifndef headfile <br/> # define headfile <br/> # include <iostream> <br/> # include <string> <br/> # include <algorithm> <br/> # include <fstream> <br/> # include <list> <br/> # include <Io. h> <br/> # include <iterator> <br/> # include <cassert> <br/> # include <sstream> <br/> using namespace STD; <br/> # endif <br/> # ifndef _ contacts_h _ <br/> # DEFINE _ contacts_h _ <br/> class contacts <br/> {<br/>/ /friend Telbook; <br/> Public: <br/> contacts (): _ name (""), _ ADDR (""), _ qqnum (""), _ telnum ("") {}< br/> contacts (string name, string ADDR, string qqnum, string telnum) <br/>: _ name (name ), _ telnum (telnum), _ qqnum (qqnum), _ ADDR (ADDR) {}< br/> contacts (char * Name, char * ADDR, char * qqnum, char * telnum) <br/>: _ name (name), _ telnum (telnum), _ qqnum (qqnum), _ ADDR (ADDR) {}< br/> contacts (const contacts & RHs) <br/>: _ name (RHS. _ name), _ ADDR (R HS. _ ADDR), _ qqnum (RHS. _ qqnum), _ telnum (RHS. _ telnum) {}< br/> contacts & operator = (const contacts & RHs) <br/>{< br/> _ name = RHS. _ name; <br/> _ ADDR = RHS. _ ADDR; <br/> _ qqnum = RHS. _ qqnum; <br/> _ telnum = RHS. _ telnum; <br/> return * This; <br/>}< br/> void initcontacts () <br/> {<br/> cout <"Start input contacts info/N" <"name:"; <br/> CIN> _ name; <br/> cout <"/ntel:"; <br/> CIN >>_telnum; <br/> cout <"/nqq:"; <B R/> CIN >>_qqnum; <br/> cout <"/naddr:"; <br/> CIN >>_addr; <br/> cout <"end input contacts info/N" <Endl; <br/>}< br/> // Operator name <br/> string getname () <br/>{< br/> return _ name; <br/>}< br/> const char * getname () const <br/> {<br/> return _ name. c_str (); <br/>}< br/> void setname (string name) <br/>{< br/> _ name = Name; <br/>}< br/> void setname (char * Name) <br/>{< br/> _ name = Name; <br/>}< br/> // o Perator telephone number <br/> string gettelnum () <br/>{< br/> return _ telnum; <br/>}< br/> const char * gettelnum () const <br/>{< br/> return _ telnum. c_str (); <br/>}< br/> void settelnum (string telnum) <br/>{< br/> _ telnum = telnum; <br/>}< br/> void settelnum (char * telnum) <br/>{< br/> _ telnum = telnum; <br/>}< br/> // opeartor address <br/> string getaddr () <br/>{< br/> return _ ADDR; <br/>}< br/> const char * geta DDR () const <br/>{< br/> return _ ADDR. c_str (); <br/>}< br/> void setaddr (string ADDR) <br/>{< br/> _ ADDR = ADDR; <br/>}< br/> void setaddr (char * ADDR) <br/>{< br/> _ ADDR = ADDR; <br/>}< br/> // operator QQ number <br/> string getqqnum () <br/>{< br/> return _ qqnum; <br/>}< br/> const char * getqqnum () const <br/> {<br/> return _ qqnum. c_str (); <br/>}< br/> void setqqnum (string qqnum) <br/>{< br/> _ qqnum = qqnum; <br/>}< B R/> void setqqnum (char * qqnum) <br/>{< br/> _ qqnum = qqnum; <br/>}< br/> friend ostream & operator <(ostream & OS, const contacts & RHs ); <br/> friend istream & operator> (istream & is, contacts & RHs); <br/> friend fstream & operator> (fstream & in, contacts & RHs ); <br/> friend fstream & operator <(fstream & out, const contacts & RHs); <br/> ~ Contacts () {}< br/> PRIVATE: <br/> string _ name; // name <br/> string _ ADDR; // contact address <br/> string _ qqnum; // QQ number <br/> string _ telnum; // phone number <br/>}; <br/> # endif 

 

// Contacts. CPP <br/> # include "contacts. H "<br/> ostream & operator <(ostream & OS, const contacts & RHs) <br/>{< br/> OS <" Name: "<RHS. _ name <"/N" <br/> <"Tel:" <RHS. _ telnum <"/tqq:" <RHS. _ qqnum <br/> <"/N" <"ADDR:" <RHS. _ ADDR <Endl; <br/> return OS; <br/>}< br/> istream & operator> (istream & is, contacts & RHs) <br/>{< br/> cout <"Enter name:"; <br/> is> RHS. _ name; <br/> cout <Endl; <br/> cout <"Enter the phone number:"; <br/> is> RHS. _ telnum; <br/> cout <Endl; <br/> cout <"Enter the QQ number:"; <br/> is> RHS. _ qqnum; <br/> cout <Endl; <br/> cout <"Enter the contact address:"; <br/> is> RHS. _ ADDR; <br/> return is; <br/>}< br/> fstream & operator> (fstream & in, contacts & RHs) <br/>{< br/> string line = ""; <br/> Getline (in, line); <br/> If (line. size () = 0) Return in; <br/> istringstream upstream stream (line); <br/> upstream stream> RHS. _ name> RHS. _ telnum> RHS. _ qqnum> RHS. _ ADDR; </P> <p> return in; <br/>}< br/> fstream & operator <(fstream & out, const contacts & RHs) <br/>{< br/> out <RHS. _ name <"/t" <RHS. _ telnum <"/t" <RHS. _ qqnum <"/t" <RHS. _ ADDR <"/N"; <br/> return out; <br/>} 

 

 

// Telbook. h <br/> # ifndef headfile <br/> # define headfile <br/> # include <iostream> <br/> # include <string> <br/> # include <algorithm> <br/> # include <fstream> <br/> # include <list> <br/> # include <Io. h> <br/> # include <iterator> <br/> # include <cassert> <br/> # include <sstream> <br/> using namespace STD; <br/> # endif <br/> # ifndef _ telbook_h _ <br/> # DEFINE _ telbook_h _ <br/> # include "contacts. H "<br/> class telboo K <br/>{< br/> Public: <br/> telbook (char * fname): _ fname (fname) <br/>{< br/> Init (fname); <br/>}< br/> telbook (string fname = ""): _ fname (fname) <br/>{< br/> Init (fname. c_str (); <br/>}< br/> void initbook () <br/>{< br/> string fname; <br/> cout <"input file name:"; <br/> CIN> fname; <br/> Init (fname. c_str (); <br/>}< br/> ~ Telbook () <br/>{< br/> If (wflag) <br/> endwrite (); <br/>}< br/> bool modify (); // search for the name, find the information of the entire contact to be modified, and modify the information. If not found, an error is returned. <br/> void add (); <br/> int search (); <br/> bool remove (); <br/> void showall (); <br/> void showsingle (INT index); <br/> void menu (); // function menu <br/> string getfilename (); <br/> PRIVATE: <br/> void Init (const char * fname ); <br/> void startload (); <br/> void endwrite (); <br/> int searchbyname (); <br/> int searchbyphone (); </P> <p>/Data <br/> int wflag; // identifies the number of writes, write data only after the operation is completed <br/> // The initial value is 1, and the value is reduced by 1 to 0. If no data is written, write data to the function by calling the destructor; </P> <p> int sflag; // used to identify whether the file is to be loaded to the list. 0 does not need to be loaded, and 1 needs to be loaded. <br/> fstream _ FIO; <br/> string _ fname; <br/> List <contacts> _ ltel; <br/>}; <br/> # endif 

 

 

 

// Telbook. CPP <br/> # include "telbook. H "<br/> # include <windows. h> <br/> bool telbook: Modify () // locate the contact name, locate the contact information, and modify the information. If not found, an error is returned. <br/>{< br/> cout <"Modify contacts info by find [old name] Then do it! "<Endl; <br/> int selnum; <br/> string tmpvalue; </P> <p> int Index = searchbyname (); <br/> If (Index =-1) <br/>{< br/> return false; <br/>}< br/> List <contacts> :: iterator it = _ ltel. begin (); <br/> advance (it, index); <br/> string mprompt = "select choice! /N1. modify the name. /N2. modify the phone number. /N3. modify the QQ number. /n4. modify the address. /n5. modify all "; <br/> cout <mprompt <Endl <" select: "; <br/> CIN> selnum; <br/> switch (selnum) <br/> {<br/> case 1: // modify name <br/> cout <"input new name :"; <br/> CIN> tmpvalue; <br/> it-> setname (tmpvalue); <br/> break; <br/> case 2: // modify telephone number <br/> cout <"input new telephone number:"; <br/> CIN> tmpvalue; <br/> it-> settelnum (tmpvalue); <br/> brea K; <br/> case 3: // modify QQ number <br/> cout <"input new QQ number:"; <br/> CIN> tmpvalue; <br/> it-> setqqnum (tmpvalue); <br/> break; <br/> case 4: // Modify address <br/> cout <"input new address"; <br/> CIN> tmpvalue; <br/> it-> setaddr (tmpvalue ); <br/> break; <br/> case 5: // modify all <br/> cout <"input new name:"; <br/> CIN> tmpvalue; <br/> it-> setname (tmpvalue); <br/> cout <"input new telephone numb Er: "; <br/> CIN> tmpvalue; <br/> it-> settelnum (tmpvalue); <br/> cout <" input new QQ number :"; <br/> CIN> tmpvalue; <br/> it-> setqqnum (tmpvalue); <br/> cout <"input new address "; <br/> CIN> tmpvalue; <br/> it-> setaddr (tmpvalue); <br/> break; <br/> default: <br/> cout <"Choice error" <Endl; <br/> return false; <br/>}< br/> return true; <br/>}< br/> void telbook: add () <br/>{< br/> contacts item; <br/> Ci N> item; <br/> _ ltel. push_back (item); <br/>}< br/> int telbook: Search () // provides two query methods, find the output contact details <br/>{< br/> int retnum =-1; // Save the returned value. <br/> int selnum; <br/> string sprompt = "select choice! /N1. search by name. /N2. search by phone number. "; <br/> cout <sprompt <Endl; <br/> CIN> selnum; <br/> switch (selnum) <br/>{< br/> case 1: <br/> retnum = searchbyname (); <br/> break; <br/> case 2: <br/> retnum = searchbyphone (); <br/> break; <br/> default: <br/> cout <"select error/N" <Endl; <br/> retnum =-1; <br/> break; <br/>}< br/> // cout <"retnum =" <retnum <Endl; <br/> return retnum; <br/>}< br/> bool telbook: Remove () <br/ >{< Br/> int selnum; <br/> string sprompt = "select choice (only remove one item )! /N1. Delete the entire item by name. /N2. Delete the entire item by phone number. /N3. Delete the entire item "; <br/> cout <sprompt <Endl; <br/> CIN> selnum; </P> <p> int index; // used to save the index number of the item to be deleted in the list </P> <p> switch (selnum) <br/>{< br/> case 1: <br/> Index = searchbyname (); <br/> break; <br/> case 2: <br/> Index = searchbyphone (); <br/> break; <br/> case 3: <br/> cout <"You shocould inuput index range is: from 1 to "; <br/> cout <_ ltel. size () <Endl; <br/> cout <"input inde X: "; <br/> CIN> index; <br/> assert (index> 0) & (index <= static_cast <int> (_ ltel. size (); <br/> Index = index-1; // It is used to keep the same index as the list. for example, the index of Item 1 is 0 in the list <br/> break; <br/> default: <br/> cout <"Choice error! /N "<Endl; <br/> return false; <br/>}< br/> List <contacts >:: iterator it = _ ltel. begin (); <br/> If (Index =-1) <br/>{< br/> return false; <br/>}< br/> advance (it, index); <br/> _ ltel. erase (it); <br/> return true; <br/>}< br/> void telbook: showall () <br/>{< br/> ostream_iterator <contacts> out (cout, "/N"); <br/> copy (_ ltel. begin (), _ ltel. end (), OUT); <br/>}< br/> void telbook: showsingle (INT index) <br/>{< br/> in T tmpindex = index; </P> <p> List <contacts>: iterator it = _ ltel. Begin (); <br/> while (tmpindex -- & it! = _ Ltel. end () <br/> it ++; <br/> cout <* It <Endl; <br/>}< br/> inline string telbook :: getfilename () <br/>{< br/> return _ fname; <br/>}< br/> void telbook: Init (const char * fname) <br/>{< br/> _ ltel. clear (); <br/> // check whether the test file exists. If yes, It is loaded into the list. If no, a new file is created. <br/> If (_ access (fname, 0) =-1) <br/>{< br/> cout <"file not existing, will create! "<Endl; <br/> _ FIO. open (fname, IOS: Out); // create a file. The access mode is write <br/> cout <"you shoud appending new contacts to list" <Endl; <br/> sflag = 0; <br/>}< br/> else <br/> {<br/> _ FIO. open (fname, IOS: In | IOs: Out); <br/> _ FIO. seekg (0, ios_base: End); </P> <p> If (! _ FIO. tellg () <br/>{< br/> cout <"you shoud appending new contacts to list" <Endl; <br/> sflag = 0; <br/>}< br/> else <br/> sflag = 1; <br/> _ FIO. seekg (0, ios_base: Beg); <br/>}< br/> wflag = 1; <br/>}< br/> void telbook: startload () <br/>{< br/> contacts item; <br/> _ FIO. seekg (0); <br/> while (_ FIO> item) // Q: If _ FIO. EOF is used as a condition. I do not know why I will read the last record again each time. <br/>{< br/> _ ltel. push_back (item); <br/>}< br/> void telboo K: endwrite () <br/>{< br/> _ FIO. close (); <br/> _ FIO. clear (); <br/> _ FIO. open (_ fname. c_str (), ios_base: Out | ios_base: trunc); <br/> contacts item; <br/> List <contacts >:: iterator it = _ ltel. begin (); <br/> while (it! = _ Ltel. end () <br/>{< br/> item = * it; <br/> _ FIO <item; <br/> it ++; <br/>}< br/> _ FIO. close (); <br/>}< br/> int telbook: searchbyname () <br/>{< br/> string name; <br/> cout <"input name:"; <br/> CIN> name; </P> <p> List <contacts> :: iterator it = _ ltel. begin (); <br/> // It = find_if (_ ltel. begin (), _ ltel. end (), predbyname); <br/> while (it! = _ Ltel. end () <br/>{< br/> If (IT-> getname (). compare (name) = 0) // find the matching element and exit <br/> break; <br/> it ++; <br/>}</P> <p> If (IT = _ ltel. end () <br/> return-1; <br/> else <br/> return distance (_ ltel. begin (), it); <br/>}< br/> int telbook: searchbyphone () <br/>{< br/> string telnum; // phone number <br/> cout <"Input Telephone number:"; <br/> CIN> telnum; </P> <p> List <contacts> :: iterator it = _ ltel. begin (); <br/> // It = fin D_if (_ ltel. Begin (), _ ltel. End (), predbyname); <br/> while (it! = _ Ltel. end () <br/>{< br/> If (IT-> gettelnum (). compare (telnum) = 0) // find the matching element and exit <br/> break; <br/> it ++; <br/>}</P> <p> If (IT = _ ltel. end () <br/> return-1; <br/> else <br/> return distance (_ ltel. begin (), it); <br/>}< br/> void telbook: menu () <br/> {<br/> // determines whether to load information from the file <br/> If (sflag = 1) <br/> startload (); <br/> else if (sflag = 0) <br/> cout <"no contacts info in file! "<Br/> <" you should select 1 to add! "<Endl; <br/> int Pos = 0; <br/> int selnum =-1; <br/> string sprompt = "select choice/N1. Add/N2. delete/N3. search/n4. display/n5. Modify/N6. exit/nselect :"; <br/> while (1 & (selnum! = 6) <br/>{< br/> cout <sprompt <Endl; <br/> CIN> selnum; <br/> switch (selnum) <br/>{< br/> case 1: // Add <br/> Add (); <br/> continue; <br/> case 2: // Delete <br/> If (remove () = false) <br/> cout <"Remove error! "<Endl; <br/> continue; <br/> case 3: // search <br/> Pos = search (); <br/> If (Pos =-1) <br/>{< br/> cout <"not find/N" <Endl; <br/> continue; <br/>}< br/> else <br/>{< br/> showsingle (POS); <br/> continue; <br/>}< br/> continue; <br/> case 4: // display <br/> showall (); <br/> continue; <br/> case 5: // modify <br/> If (modify () = false) <br/> cout <"modification failed" <Endl; <br/> else <br/> cout <"modified successfully" <Endl; <br/> continue; <br /> Case 6: // exit <br/> cout <"exited" <Endl; <br/> break; <br/> default: <br/> cout <"Choice error! Select again! "<Endl; <br/>}< br/>} 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.