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];
- 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);
- }
- }
- void ShowAllContacts() {
- cout << "The following contacts are available: " << endl;
- if (contacts_number == 0) {
- cout << "No contacts";
- }
- for (int i = 0; i < contacts_number; ++i) {
- 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;
- }
- 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;
- };
- 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");
- my_store.ShowAllContacts();
- my_store.DelContact("Zhenya");
- my_store.ShowAllContacts();
- my_store.AddContact("Zhenya", "+79111111333", "111133", "121233");
- my_store.FindContactName("Galya");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement