| Код | #include <iostream> #include <iterator> #include <vector> #include <string> #include <string.h> #include <algorithm> #include <fstream> #include <time.h>
struct Date { int day; int month; int year; };
const int MAX_FIRSTNAME = 100; const int MAX_LASTNAME = 200; const int MAX_PHONE = 20;
struct Abonent { char firstName[ MAX_FIRSTNAME ]; char lastName[ MAX_LASTNAME ]; Date birthDay; int age; char phone[ MAX_PHONE ]; };
void countAge( Abonent & abonent ) { time_t rawtime; time ( &rawtime ); tm current = *(localtime( &rawtime )); abonent.age = current.tm_year - abonent.birthDay.year; }
bool operator==( const Abonent & left, const Abonent & right ) { return std::string( left.firstName ) == std::string( right.firstName ) && std::string( left.lastName ) == std::string( right.lastName ); }
std::ostream & operator<<( std::ostream & os, const Abonent & abonent ) { Abonent abonentForAge = abonent; countAge( abonentForAge );
os << abonent.firstName << " " << abonent.lastName << ":" << std::endl; os << " birthDay : " << abonent.birthDay.day << "." << abonent.birthDay.month << "." << abonent.birthDay.year << std::endl; os << " age : " << abonentForAge.age << std::endl; os << " phone : " << abonent.phone << std::endl; return os; }
typedef std::vector< Abonent > AddressBook;
void addAbonent( AddressBook & addressBook, const Abonent & abonent ) { Abonent abonentToAdd = abonent; countAge( abonentToAdd ); addressBook.push_back( abonentToAdd ); }
void addAbonent( AddressBook & addressBook, const char * firstName, const char * lastName, int day, int month, int year, const char * phone ) { Abonent abonent; strcpy( abonent.firstName, firstName ); strcpy( abonent.lastName, lastName ); abonent.birthDay.day = day; abonent.birthDay.month = month; abonent.birthDay.year = year; strcpy( abonent.phone, phone ); addAbonent( addressBook, abonent ); }
void deleteAbonent( AddressBook & addressBook, const Abonent & abonent ) { AddressBook::iterator found = std::find( addressBook.begin(), addressBook.end(), abonent ); if ( found != addressBook.end() ) { addressBook.erase( found ); } }
void printAddressBook( const AddressBook & addressBook ) { std::copy( addressBook.begin(), addressBook.end(), std::ostream_iterator< Abonent >( std::cout, "\n" ) ); }
void saveAddressBookToFile( const AddressBook & addressBook, const char * fileName ) { std::ofstream file( fileName, std::ofstream::binary ); if ( ! file ) { return; } for ( AddressBook::const_iterator it = addressBook.begin(), end = addressBook.end(); it != end; ++it ) { file.write( (char*)&(*it), sizeof( *it ) ); } }
void loadAddressBookFromFile( AddressBook & addressBook, const char * fileName ) { std::ifstream file( fileName, std::ofstream::binary ); if ( ! file ) { return; } Abonent abonent; while ( ! file.eof() ) { file.read( (char*)&abonent, sizeof( abonent ) ); if ( ! file.fail() ) { addAbonent( addressBook, abonent ); } } }
int main() { AddressBook book; addAbonent( book, "Freddy", "Kruger", 1, 11, 1980, "911" ); addAbonent( book, "Arnold", "Schwartz(enough...)", 2, 6, 1960, "+009876543210" ); printAddressBook( book ); Abonent freddy; strcpy( freddy.firstName, "Freddy" ); strcpy( freddy.lastName, "Kruger" ); deleteAbonent( book, freddy ); std::cout << "freddy deleted:" << std::endl; printAddressBook( book ); }
|
Цитата(Hqw33 @ 10.4.2012, 06:44 ) | | Желаемое действие выбирайте с помощью меню. |
это уже сам |