📄 addressbook.cpp
字号:
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
using namespace std;
const unsigned char SEPARATOR = '@';
typedef enum { load = 0, save, add, edit, erase, print, prall, leave } ECommands;
typedef enum { fname = 0, opened, saved, updated, erased, noopen, nosave, noupdate, noerase } EMessages;
map<string,ECommands> MCommands;
map<string,EMessages> MMessages;
const char *SCommands[] = { "load", "save", "add", "edit", "erase", "print", "prall", "exit" };
const char *DCommands[] = { "Load a phones file",
"Save a phones file",
"Add new contact",
"Edit contact info",
"Erase a contact",
"Print contact's info",
"Print all contacts",
"Exit" };
const char *SMessages[] = { "File name: ",
"Opened file: ",
"Saved file: ",
"Updated",
"Erased",
"Couldn't open file: ",
"Couldn't save file: ",
"Couldn't update contact",
"Couldn't erase contact" };
class Contact;
class AddressBook;
void LoadCommands();
void PrintCommands();
void ProcessCommands( AddressBook & ab );
void LoadMessages();
//-----------------------------------------------------------------------------
class Contact
{
public:
string Name;
string Phone;
string Address;
Contact & operator = ( const Contact & c );
bool operator == ( const Contact & c );
friend ostream & operator << ( ostream & os, Contact & c );
friend istream & operator >> ( istream & is, Contact & c );
friend ofstream & operator << ( ofstream & ofs, Contact & c );
friend ifstream & operator >> ( ifstream & ifs, Contact & c );
};
//-----------------------------------------------------------------------------
Contact & Contact::operator = ( const Contact & c )
{
Name = c.Name;
Phone = c.Phone;
Address = c.Address;
return * this;
}
//-----------------------------------------------------------------------------
bool Contact::operator == ( const Contact & c )
{
return (( Name == c.Name ) && (Phone == c.Phone) && (Address == c.Address));
}
//-----------------------------------------------------------------------------
ostream & operator << ( ostream & os, Contact & c )
{
os << "Name : " << c.Name << endl;
os << "Phone : " << c.Phone << endl;
os << "Address: " << c.Address << endl;
return os;
}
//-----------------------------------------------------------------------------
istream & operator >> ( istream & is, Contact & c )
{
stringbuf ssBuffer;
string sEmpty = "";
cin.get();
cout << "Name: ";
is.get( ssBuffer );
c.Name = ssBuffer.str();
ssBuffer.str( sEmpty );
is.get();
cout << "Phone: ";
is.get( ssBuffer );
c.Phone = ssBuffer.str();
ssBuffer.str( sEmpty );
is.get();
cout << "Address: ";
is.get( ssBuffer );
c.Address = ssBuffer.str();
ssBuffer.str( sEmpty );
is.get();
return is;
}
//-----------------------------------------------------------------------------
ifstream & operator >> ( ifstream & ifs, Contact & c )
{
stringbuf ssBuffer;
string sEmpty = "";
ifs.get( ssBuffer, SEPARATOR );
ifs.ignore();
c.Name = ssBuffer.str();
ssBuffer.str( sEmpty );
ifs.get( ssBuffer, SEPARATOR );
ifs.ignore();
c.Phone = ssBuffer.str();
ssBuffer.str( sEmpty );
ifs.get( ssBuffer, SEPARATOR );
ifs.ignore();
c.Address = ssBuffer.str();
ssBuffer.str( sEmpty );
return ifs;
}
//-----------------------------------------------------------------------------
ofstream & operator << ( ofstream & ofs, Contact & c )
{
ofs << c.Name << SEPARATOR;
ofs << c.Phone << SEPARATOR;
ofs << c.Address << SEPARATOR;
return ofs;
}
//-----------------------------------------------------------------------------
class AddressBook
{
public:
AddressBook();
~AddressBook();
// Operaciones para agregar, eliminar y actualizar
void Add( Contact &c );
bool Delete( string Name );
bool Update( Contact &c );
// Obtener un contacto por nombre
Contact & Get( string Name );
// Guardar y cargar desde disco
bool Save( string File );
bool Load( string File );
// Moverse dentro de la lista de contactos
Contact & First();
Contact & Next();
Contact & Previous();
Contact & Last();
Contact & Current();
// Para verificar si se est
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -