Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- using namespace std;
- struct contact {
- char name[40];
- char mobile_phone[20];
- char home_phone[20];
- char work_phone[20];
- };
- class phone_book {
- public:
- phone_book() : contacts_number(0), max_contacts_number(4), store(new contact[4]) {}
- explicit phone_book(int max_number) : contacts_number(0), max_contacts_number(max_number), store(new contact[max_number]) {}
- phone_book(const phone_book& other) : contacts_number(0), max_contacts_number(other.max_contacts_number), store(new contact[other.max_contacts_number]) {
- for (int i = 0; i < other.contacts_number; i++) {
- add_contact(other.store[i].name, other.store[i].mobile_phone, other.store[i].home_phone, other.store[i].work_phone);
- }
- }
- phone_book& operator=(const phone_book& other) {
- if (this != &other) {
- delete[] store;
- contacts_number = 0;
- max_contacts_number = other.max_contacts_number;
- store = new contact[max_contacts_number];
- for (int i = 0; i < other.contacts_number; i++) {
- add_contact(other.store[i].name, other.store[i].mobile_phone, other.store[i].home_phone, other.store[i].work_phone);
- }
- }
- return *this;
- }
- void add_contact(const char* name, const char* mobile_phone, const char* home_phone, const char* work_phone) {
- if (contacts_number == max_contacts_number) {
- contact* temp = new contact[max_contacts_number * 2];
- for (int i = 0; i < contacts_number; i++) {
- temp[i] = store[i];
- }
- delete[] store;
- store = temp;
- max_contacts_number *= 2;
- }
- strcpy(store[contacts_number].name, name);
- strcpy(store[contacts_number].mobile_phone, mobile_phone);
- strcpy(store[contacts_number].home_phone, home_phone);
- strcpy(store[contacts_number].work_phone, work_phone);
- contacts_number++;
- }
- void del_contact(const char* name) {
- for (int i = 0; i < contacts_number; i++) {
- if (!strcmp(store[i].name, name)) {
- for (int j = i; j < contacts_number - 1; j++) {
- store[j] = store[j + 1];
- }
- contacts_number--;
- i--; // на случай, если подряд идут два одноименных контакта (иначе через второй перескакивает)
- }
- }
- }
- void show_all_contacts() const {
- for (int i = 0; i < contacts_number; i++) {
- cout << store[i].name << ", мобильный телефон - " << store[i].mobile_phone << ", домашний телефон - " << store[i].home_phone << ", рабочий телефон - " << store[i].work_phone << endl;
- }
- cout << endl;
- }
- void find_contact_name(const char* name) const {
- for (int i = 0; i < contacts_number; i++) {
- if (!strcmp(store[i].name, name)) {
- cout << store[i].name << ", мобильный телефон - " << store[i].mobile_phone << ", домашний телефон - " << store[i].home_phone << ", рабочий телефон - " << store[i].work_phone << endl;
- }
- }
- }
- bool operator==(const phone_book& other) const {
- if (this->contacts_number != other.contacts_number) {
- return false;
- }
- for (int i = 0; i < contacts_number; i++) {
- if (strcmp(other.store[i].name, store[i].name) || strcmp(other.store[i].mobile_phone, store[i].mobile_phone) || strcmp(other.store[i].home_phone, store[i].home_phone) || strcmp(other.store[i].work_phone, store[i].work_phone)) {
- return false;
- }
- }
- return true;
- }
- bool operator!=(const phone_book& other) const {
- return !(*this == other);
- }
- ~phone_book() {
- delete[] store;
- }
- private:
- int contacts_number;
- int max_contacts_number;
- contact* store;
- };
- int main() {
- setlocale(LC_ALL, "ru");
- phone_book my_book;
- my_book.add_contact("Иван", "+79111111111", "111111", "121212");
- my_book.add_contact("Оля", "+79111114444", "114444", "141414");
- my_book.add_contact("Саша", "+79111115555", "115555", "151515");
- my_book.add_contact("Женя", "+79111116666", "116666", "161616");
- my_book.show_all_contacts();
- my_book.del_contact("Женя");
- my_book.show_all_contacts();
- my_book.find_contact_name("Саша");
- cout << endl;
- const phone_book my_book2(my_book);
- my_book2.show_all_contacts();
- phone_book my_book3;
- my_book3 = my_book;
- my_book3.show_all_contacts();
- cout << (my_book == my_book2) << endl;
- cout << (my_book != my_book2) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment