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

// 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;
}

#define _CRT_SECURE_NO_WARNINGS 1 #ifndef __CONTACT_H_ #define __CONTACT_H_ #include<stdio.h> #include<stdlib.h> #include<string.h> #define PEO_MAX 30 #define STUID_MAX 10 #define NAME_MAX 20 #define SEX_MAX 10 #define TEL_MAX 15 typedef struct People { char id[STUID_MAX]; char name[NAME_MAX]; char sex[SEX_MAX]; char tel[TEL_MAX]; }*peo; typedef struct Contact { int count; struct People people[PEO_MAX]; }*pCon; void add_peo(pCon pcon); //添加联系人信息 void show_peo(pCon pcon); //显示指定联系人信息 void find_peo(pCon pcon); //查找联系人信息 void modify_peo(pCon pcon); //修改指定联系人信息 void clear_peo(pCon pcon); //清空联系人信息 void show_menu(); //菜单显示 #endif int search(pCon pcon, char *name) { int i = 0; for (i=0; i < pcon->count; i++) { if (strcmp(name, pcon->people[i].name) == 0) return i; } return -1; } void add_peo(pCon pcon) //添加联系人 { if (pcon->count == PEO_MAX) { printf("The contact has fullen."); return; } printf("please input studentID: "); scanf("%s",(pcon->people[pcon->count]).id); printf("please input name: "); scanf("%s", (pcon->people[pcon->count]).name); printf("please input sex : "); scanf("%s", (pcon->people[pcon->count]).sex); printf("please input tel: "); scanf("%s", (pcon->people[pcon->count]).tel); pcon->count++; } void show_peo(pCon pcon)//显示联系人 { int i = 0; for (; i < pcon->count; i++) { printf("studentID name sex tel \n"); printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } } void find_peo(pCon pcon) //查找联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to find:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("%s\t%s\t%s\t%s\n", pcon->people[i].id, pcon->people[i].name, pcon->people[i].sex, pcon->people[i].tel); } void modify_peo(pCon pcon) //修改指定联系人信息 { int i; char name[NAME_MAX]; printf("please input the people you want to modify:"); scanf("%s", name); i = search(pcon, name); if (i == -1) printf("The people doesn't exsit.\n"); else printf("please input studentID: "); scanf("%s", (pcon->people[i]).id); printf("please input name: "); scanf("%s", (pcon->people[i]).name); printf("please input sex : "); scanf("%s", (pcon->people[i]).sex); printf("please input tel: "); scanf("%s", (pcon->people[i]).tel); } void clear_peo(pCon pcon) //清空联系人 { pcon->count = 0; } void show_menu() //菜单显示 { printf("==========通讯录==========\n"); printf(" \n"); printf("*******[1]显示通讯录******\n"); printf("*******[2]查 询******\n"); printf("*******[3]修 改******\n"); printf("*******[4]结 束******\n"); } int main() { struct Contact my_contact; int input = 1; my_contact.count = 0; while (input) { show_menu(); printf("please input:"); scanf("%d", &input;); switch (input) { case 1: add_peo(&my;_contact); break; case 2: show_peo(&my;_contact); break; case 3: find_peo(&my;_contact); break; case 4: modify_peo(&my;_contact); break; case 5: clear_peo(&my;_contact); break; default: break; } } return 0; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值