您的位置:首页 > 编程语言 > C语言/C++

C++实现通讯录管理程序

2006-03-02 18:06 471 查看
// person_info.h: interface for the person_info class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PERSON_INFO_H__8E4305E3_3140_483E_80C7_234281FF3F9B__INCLUDED_)
#define AFX_PERSON_INFO_H__8E4305E3_3140_483E_80C7_234281FF3F9B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string>
using namespace std;
/*
the person information class
which has person name and
person phone number
*/
class person_info
{
public:
string p_name;//person name
string p_phone;//person phone number
public:
person_info(string name="",string phone="");
virtual ~person_info();

};

#endif // !defined(AFX_PERSON_INFO_H__8E4305E3_3140_483E_80C7_234281FF3F9B__INCLUDED_)

// person_info.cpp: implementation of the person_info class.
//
//////////////////////////////////////////////////////////////////////

#include "person_info.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

person_info::person_info(string name,string phone)
{
p_name = name;
p_phone = phone;
}

person_info::~person_info()
{

}

// phone_book.h: interface for the phone_book class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PHONE_BOOK_H__E255EBB1_B35F_4CD9_87F1_D38777E51928__INCLUDED_)
#define AFX_PHONE_BOOK_H__E255EBB1_B35F_4CD9_87F1_D38777E51928__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "phone_list.h"
#include "book_file.h"
#include <string>

using namespace std;
/*
the main class of this application
*/
class phone_book
{
public:
phone_book();
virtual ~phone_book();
void del_note();
void search_note();
void add_note();
void display_all_note();
void info_save();
void info_save_as();
bool info_open();
private:
void del_note_as_name(const string& name);
void del_note_as_phone(const string& phone);
void search_note_as_name(const string& name);
void search_note_as_phone(const string& phone);

phone_list all_info;
book_file b_file;

string current_file_name;//the current used file name

};

#endif // !defined(AFX_PHONE_BOOK_H__E255EBB1_B35F_4CD9_87F1_D38777E51928__INCLUDED_)

// phone_book.cpp: implementation of the phone_book class.
//
//////////////////////////////////////////////////////////////////////

#include "phone_book.h"

#include <iostream>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

phone_book::phone_book():b_file()
{
current_file_name = "PhoneBook.txt";//The default file name
}

phone_book::~phone_book()
{

}

void phone_book::add_note()
{
string name,phone;
cout<<"Input the person's name:";
cin>>name;
cout<<"Input the person's phone:";
cin>>phone;

person_info p_info(name,phone);

all_info.add_person_info(p_info);

cout<<"Add successfully!"<<endl<<endl;
}

void phone_book::del_note()
{
cout<<"10 Delete as name"<<endl
<<"11 Delete as phone"<<endl;
cout<<"Please choose the order:";
int choose;
cin>>choose;
if(choose == 10){
cout<<"Input the name:";
string name;
cin>>name;
del_note_as_name(name);
}
else if(choose == 11){
cout<<"Input the phone:";
string phone;
cin>>phone;
del_note_as_phone(phone);
}
else{
cout<<"WRONG ORDER"<<endl;
}
cout<<"Delete successfully!"<<endl<<endl;
return;
}

void phone_book::search_note()
{
cout<<"12 Search as name"<<endl
<<"13 Search as phone"<<endl;
cout<<"Please choose the order:";
int choose;
cin>>choose;
if(choose == 12){
cout<<"Input the name:";
string name;
cin>>name;
search_note_as_name(name);
}
else if(choose == 13){
cout<<"Input the phone:";
string phone;
cin>>phone;
search_note_as_phone(phone);
}
else{
cout<<"WRONG ORDER"<<endl;
}
////cout<<"Search successfully!"<<endl<<endl;
return;
}

void phone_book::display_all_note()
{
all_info.display_list_info();
cout<<"Show the phone book successfully!"<<endl<<endl;
}

void phone_book::del_note_as_name(const string& name)
{
all_info.del_person_as_name(name);
}

void phone_book::del_note_as_phone(const string& phone)
{
all_info.del_person_as_phone(phone);
}

void phone_book::search_note_as_name(const string &name)
{
all_info.search_person_as_name(name);
}

void phone_book::search_note_as_phone(const string &phone)
{
all_info.search_person_as_phone(phone);
}

void phone_book::info_save()
{
book_file out_file(current_file_name);
out_file.set_data(all_info);
out_file.write_info();
cout<<"The phone book is saved successfully!"<<endl<<endl;
}

void phone_book::info_save_as()
{
cout<<"Input the new file name:";
string new_file_name;
cin>>new_file_name;

current_file_name = new_file_name;//save the current file name

book_file out_file(current_file_name);
out_file.set_data(all_info);
out_file.write_info();
cout<<"The phone book is saved as...successfully!"<<endl<<endl;
}

bool phone_book::info_open()
{
cout<<"Input the file name:";
string open_file_name;
cin>>open_file_name;

current_file_name = open_file_name;// save the current file name

book_file in_file(open_file_name);
if(in_file.read_info()){
cout<<"The phone book is opened!"<<endl<<endl;
all_info = in_file.get_data();
return true;
}
else
return false;

}

// phone_list.h: interface for the phone_list class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PHONE_LIST_H__B64F68E7_4508_4DE1_B0FC_2C99B3861EBC__INCLUDED_)
#define AFX_PHONE_LIST_H__B64F68E7_4508_4DE1_B0FC_2C99B3861EBC__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "person_info.h"
#include <list>
#include <algorithm>

using namespace std;

typedef list<person_info> person_list;

/* the class phone_list which is the container
of the person information
*/
class phone_list
{
private:
person_list p_list;//information container
class is_name{//pred of is_name
public:
string p_name;
is_name(const string& name){ p_name = name; }
bool operator() (const person_info& info)
{
return info.p_name == p_name;
}
};
class is_phone{//pred of is_phone
public:
string p_phone;
is_phone(const string& phone){ p_phone = phone; }
bool operator() (const person_info& info)
{
return info.p_phone == p_phone;
}
};
public:
phone_list();
bool add_person_info(const person_info info);
bool del_person_as_name(const string& name);
bool del_person_as_phone(const string& phone);
bool search_person_as_name(const string& name);
bool search_person_as_phone(const string& phone);
bool display_list_info(void) const;
person_list& get_person_list(void);

virtual ~phone_list();

};

#endif // !defined(AFX_PHONE_LIST_H__B64F68E7_4508_4DE1_B0FC_2C99B3861EBC__INCLUDED_)

// phone_list.cpp: implementation of the phone_list class.
//
//////////////////////////////////////////////////////////////////////

#include "phone_list.h"
#include <iostream>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

phone_list::phone_list()
{
person_list p_list;
}

phone_list::~phone_list()
{

}

bool phone_list::add_person_info(const person_info info)
{
p_list.push_back(info);

return true;
}

bool phone_list::del_person_as_name(const string& name)
{
//p_list.remove_if(is_name(name));
list<person_info>::iterator location;
location = remove_if(p_list.begin(),p_list.end(),is_name(name));

if(location == p_list.end()){
cout<<"The information is not exist!Delete as name failed!"<<endl;
return false;
}
else{
//cout<<location->p_name<<" "<<location->p_phone<<endl;
p_list.erase(location,p_list.end());
}

return true;
}

bool phone_list::del_person_as_phone(const string& phone)
{
//p_list.remove_if(is_phone(phone));
list<person_info>::iterator location;
location = remove_if(p_list.begin(),p_list.end(),is_phone(phone));

if(location == p_list.end()){
cout<<"The information is not exist!Delete as phone failed!"<<endl;
return false;
}
else{
//cout<<location->p_name<<" "<<location->p_phone<<endl;
p_list.erase(location,p_list.end());
}

return true;
}

bool phone_list::display_list_info(void) const
{
list<person_info>::const_iterator iter;

for(iter=p_list.begin();iter!=p_list.end();iter++)
{
cout<<(*iter).p_name<<" "<<(*iter).p_phone<<endl;
}

return true;
}

bool phone_list::search_person_as_name(const string& name)
{
list<person_info>::const_iterator location;

location = find_if(p_list.begin(),p_list.end(),is_name(name));

if(location == p_list.end()){
cout<<"The information is not exist!Search as name failed!"<<endl<<endl;
return false;
}
else{
cout<<location->p_name<<" "<<location->p_phone<<endl;
}

return true;
}

bool phone_list::search_person_as_phone(const string &phone)
{
list<person_info>::const_iterator location;

location = find_if(p_list.begin(),p_list.end(),is_phone(phone));

if(location == p_list.end()){
cout<<"The information is not exist!Search as phone failed!"<<endl<<endl;
return false;
}
else{
cout<<location->p_name<<" "<<location->p_phone<<endl;
}

return true;
}

person_list& phone_list::get_person_list(void)
{
return p_list;
}

#pragma once
#ifndef BOOK_FILE
#define BOOK_FILE

#include "phone_list.h"
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>

using namespace std;

class book_file//The file class which is contain the operate of the file
//This is a sevice object
{
public:
book_file(const string& f_name="PhoneBook.txt");
bool read_info();
bool write_info();
//bool info_save();
//bool info_save_as(const string& new_file_name);
phone_list& get_data();
bool set_data(const phone_list& list);
public:
~book_file(void);
private:
phone_list file_p_list;
string file_name;
};

#endif

#include "book_file.h"

book_file::book_file(const string& f_name)
{
file_name = f_name;
}

book_file::~book_file(void)
{
}

bool book_file::read_info()
{
string name,phone;

ifstream file_in(file_name.c_str());

if(!file_in.is_open())
{
cerr<<"Can't open "<<file_name<<",please check it./n/n";
file_in.close();
return false;
}
else{
file_in>>name>>phone;

//cout<<name<<" "<<phone<<endl;
while(!file_in.eof())
{
person_info temp(name,phone);

file_p_list.add_person_info(temp);
//
//file_p_list.display_list_info();
//delete temp;
file_in>>name>>phone;
//cout<<name<<" "<<phone<<endl;
}
}
file_in.close();
return true;
}

bool book_file::write_info()
{
ofstream file_out(file_name.c_str());

if(!file_out.is_open())
{
cerr<<"Can't open "<<file_name<<",please check it./n";
file_out.close();
return false;
}
else{

list<person_info>::const_iterator info_iter = file_p_list.get_person_list().begin();

for(;info_iter != file_p_list.get_person_list().end();info_iter++)
{
file_out<<info_iter->p_name<<"/t"<<info_iter->p_phone<<"/n";
}
}

file_out.close();
return true;
}

/*bool book_file::info_save(void)//save the modified the information to
{
//...
ofstream file_out(file_name.c_str(),ios::app);

list<person_info>::const_iterator info_iter = file_p_list.get_person_list().begin();

for(;info_iter != file_p_list.get_person_list().end();info_iter++)
{
file_out<<info_iter->p_name<<"/t"<<info_iter->p_phone<<"/n";
}

file_out.close();

return true;
}

bool book_file::info_save_as(const string& new_file_name)
{
//..

return true;
}
*/

phone_list& book_file::get_data()
{
return file_p_list;
}

bool book_file::set_data(const phone_list& list)
{
file_p_list = list;

return true;
}

/*********************main.cpp*************/
#include "phone_book.h"
#include <iostream>

using namespace std;

void order_menu()
{
cout<<"1 new a phone book"<<endl
<<"2 open a phone book"<<endl
<<"3 exit"<<endl<<endl;
cout<<"please choose your order:";
}

void edit_menu()
{
cout<<"4 add a person's information"<<endl
<<"5 delete a person's information"<<endl
<<"6 search the person' information"<<endl
<<"7 show the phone book"<<endl
<<"8 save the phone book"<<endl
<<"9 save the phone book as..."<<endl
<<"0 exit edit"<<endl<<endl;
cout<<"please choose your order:";
}

void main_menu()
{
int choose;

phone_book* pb=0;
//phone_book phBook;

while(true){
order_menu();
cin>>choose;
switch(choose)
{
case 1:
pb = new phone_book;
cout<<"A new phone book,you can edit it..."<<endl<<endl;
edit_menu();
cin>>choose;
while(true)
{
switch(choose)
{
case 4:
pb->add_note();
edit_menu();
break;
case 5:
pb->del_note();
edit_menu();
break;
case 6:
pb->search_note();
edit_menu();
break;
case 7:
pb->display_all_note();
edit_menu();
break;
case 8:
pb->info_save();
//edit_menu();
break;
case 9:
pb->info_save_as();
//edit_menu();
break;
case 0:
while(true)
{
cout<<"Save what you edited Y/N:";
char judge;
cin>>judge;
if(judge=='Y' || judge=='y'){
pb->info_save();
break;
}
else if(judge=='N' || judge=='n')
break;
else
cout<<"WRONG JUDGE!"<<endl;
}
break;
default:
cout<<"WORNG ORDER!"<<endl;
cout<<"please choose your order:";
break;
}
if(choose!=8 && choose!=9 && choose!=0)
cin>>choose;
else
break;
}
break;
case 2:
pb = new phone_book;
if(pb->info_open())
edit_menu();
else
break;
cin>>choose;
while(true)
{
switch(choose)
{
case 4:
pb->add_note();
edit_menu();

break;
case 5:
pb->del_note();
edit_menu();
break;
case 6:
pb->search_note();
edit_menu();
break;
case 7:
pb->display_all_note();
edit_menu();
break;
case 8:
pb->info_save();
//edit_menu();
break;
case 9:
pb->info_save_as();
// edit_menu();
break;
case 0:
while(true)
{
cout<<"Save what you edited Y/N:";
char judge;
cin>>judge;
if(judge=='Y' || judge=='y'){
pb->info_save();
}
else if(judge=='N' || judge=='n'){
cout<<endl;
break;
}
else{
cout<<"WRONG JUDGE!"<<endl;
continue;
}
break;
}
break;
default:
cout<<"WORNG ORDER!"<<endl;
cout<<"please choose your order:";
break;
}
if(choose!=8 && choose!=9 && choose!=0)
cin>>choose;
else
break;
}
break;
case 3:
cout<<"Thanks for use!"<<endl;
exit(0);
default:
cout<<"WRONG ORDER!"<<endl;
order_menu();
break;
}
}

if(pb)
delete pb;

}

int main(int argc,char** argv)
{

main_menu();

system("pause");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: