Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string.h>
- using namespace std;
- struct contact {
- char name[100];
- char mobile_phone[20];
- char home_phone[20];
- char work_phone[20];
- };
- class phone_book {
- public:
- phone_book() : store(new contact* [100]), contacts_number(0), max_contacts_number(100) {}
- phone_book(int max_number) : store(new contact* [max_number]), contacts_number(0), max_contacts_number(max_number) {}
- phone_book(contact arr[], int arr_size) : store(new contact* [100]), contacts_number(0), max_contacts_number(100) {
- for (int i = 0; i < arr_size; i++) {
- AddContact(arr[i].name, arr[i].mobile_phone, arr[i].home_phone, arr[i].work_phone);
- }
- }
- phone_book(const phone_book& other) : store(new contact* [other.max_contacts_number]), contacts_number(0), max_contacts_number(other.max_contacts_number) {
- for (int i = 0; i < other.contacts_number; i++) {
- AddContact(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) {
- for (int i = 0; i < contacts_number; i++) {
- delete store[i];
- }
- 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++) {
- AddContact(other.store[i]->name, other.store[i]->mobile_phone, other.store[i]->home_phone, other.store[i]->work_phone);
- }
- }
- return *this;
- }
- friend ostream& operator<<(ostream& output, const phone_book& book);
- bool operator==(const phone_book& other) {
- 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) {
- return !(*this == other);
- }
- void AddContact(const char name[], const char mobile_phone[], const char home_phone[], const char work_phone[]) { // не забывать ставить const, иначе не скомпилируется
- if (contacts_number == max_contacts_number) {
- cout << "The contact cannot be added" << endl;
- return;
- }
- store[contacts_number] = new contact{};
- 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 DelContact(const char name[]) {
- for (int i = 0; i < contacts_number; i++) {
- if (!strcmp(store[i]->name, name)) { // возвращает 0 если строки одинаковые, поэтому используем ! (чтобы возврат нуля означал "истину")
- delete store[i];
- for (int j = i; j < contacts_number - 1; j++) {
- store[j] = store[j + 1];
- }
- store[contacts_number - 1] = nullptr;
- contacts_number--;
- }
- }
- }
- void FindContactName(const char name[]) {
- cout << "Found according to your request: " << endl;
- for (int i = 0; i < contacts_number; i++) {
- if (!strcmp(store[i]->name, name)) {
- cout << '\"' << store[i]->name << "\", mobile_phone: " << store[i]->mobile_phone << ", home_phone: " << store[i]->home_phone << ", work_phone: " << store[i]->work_phone << endl;
- }
- }
- cout << endl;
- }
- ~phone_book() {
- for (int i = 0; i < contacts_number; i++) {
- delete store[i];
- }
- delete[] store;
- }
- private:
- contact** store;
- int contacts_number;
- int max_contacts_number;
- };
- ostream& operator<<(ostream& output, const phone_book& book) {
- output << "The following contacts are available: " << endl;
- if (book.contacts_number == 0) {
- output << "No contacts";
- }
- for (int i = 0; i < book.contacts_number; ++i) {
- output << '\"' << book.store[i]->name << "\", mobile_phone: " << book.store[i]->mobile_phone << ", home_phone: " << book.store[i]->home_phone << ", work_phone: " << book.store[i]->work_phone << endl;
- }
- output << endl;
- return output;
- }
- int main() { // для проверки функционала
- phone_book my_store; // для проверки конструктора по умолчанию
- //phone_book my_store(50); // для проверки конструктора для одного параметра
- //contact some_contacts[]{ {"Ivan", "+79111111111", "111111", "121212"}, {"Zhenya", "+79111111333", "111133", "121233"}}; // для проверки конструктора для двух параметров (закомментировать первые два добавления контакта)
- //phone_book my_store(some_contacts, 2); // для проверки конструктора для двух параметров
- my_store.AddContact("Ivan", "+79111111111", "111111", "121212");
- my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
- my_store.AddContact("Galya", "+79115511333", "166133", "127733");
- my_store.AddContact("Olya", "+79115511333", "166133", "127733");
- cout << my_store;
- my_store.DelContact("Zhenya");
- cout << my_store;
- my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
- my_store.FindContactName("Galya");
- cout << "my_store: " << endl;
- cout << my_store;
- phone_book my_store2(my_store);
- cout << "my_store2: " << endl;
- cout << my_store2;
- phone_book my_store3;
- my_store3 = my_store2;
- cout << "my_store3: " << endl;
- cout << my_store3;
- cout << boolalpha;
- cout << (my_store == my_store2) << endl;
- cout << (my_store != my_store2) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement