26.12.2023 задание 1: Создать базовый класс «Домашнее животное» и производные классы «Собака», «Кошка», «Попугай». С помощью конструктора установить имя каждого животного и его характеристики: class Animal { protected: std::string name; int age; public: Animal(std::string _name, int _age) : name(_name), age(_age) {} void showInfo() { std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; } }; class Dog : public Animal { private: std::string breed; public: Dog(std::string _name, int _age, std::string _breed) : Animal(_name, _age), breed(_breed) {} void showInfo() { Animal::showInfo(); std::cout << "Breed: " << breed << std::endl; } }; class Cat : public Animal { private: std::string color; public: Cat(std::string _name, int _age, std::string _color) : Animal(_name, _age), color(_color) {} void showInfo() { Animal::showInfo(); std::cout << "Color: " << color << std::endl; } }; class Parrot : public Animal { private: std::string species; public: Parrot(std::string _name, int _age, std::string _species) : Animal(_name, _age), species(_species) {} void showInfo() { Animal::showInfo(); std::cout << "Species: " << species << std::endl; } }; int main() { Dog dog("Buddy", 5, "Golden Retriever"); Cat cat("Whiskers", 3, "Gray"); Parrot parrot("Polly", 2, "African Grey"); dog.showInfo(); std::cout << std::endl; cat.showInfo(); std::cout << std::endl; parrot.showInfo(); std::cout << std::endl; return 0; } задание 2: class String { protected: char* str; int length; public: // Конструкторы String() { str = nullptr; length = 0; } String(const char* cstr) { length = strlen(cstr); str = new char[length + 1]; strcpy(str, cstr); } String(const String& other) { length = other.length; str = new char[length + 1]; strcpy(str, other.str); } // Оператор присваивания String& operator=(const String& other) { if (this != &other) { delete [] str; length = other.length; str = new char[length + 1]; strcpy(str, other.str); } return *this; } // Получение длины строки int getLength() const { return length; } // Очистка строки void clear() { delete [] str; str = nullptr; length = 0; } // Деструктор ~String() { delete [] str; } // Конкатенация строк String operator+(const String& other) const { String result; result.length = length + other.length; result.str = new char[result.length + 1]; strcpy(result.str, str); strcat(result.str, other.str); return result; } String& operator+=(const String& other) { char* temp = new char[length + other.length + 1]; strcpy(temp, str); strcat(temp, other.str); delete [] str; str = temp; length += other.length; return *this; } // Проверка на равенство и неравенство bool operator==(const String& other) const { return strcmp(str, other.str) == 0; } bool operator!=(const String& other) const { return strcmp(str, other.str) != 0; } }; class BinaryString : public String { public: BinaryString(const char* cstr) : String(cstr) { // Проверка на наличие символов, отличных от '0' и '1' for (int i = 0; i < length; i++) { if (str[i] != '0' && str[i] != '1') { clear(); break; } } } }; int main() { String str1("Hello"); String str2 = "World"; String str3 = str1 + str2; std::cout << "str1: " << str1.getLength() << " " << str1 << std::endl; std::cout << "str2: " << str2.getLength() << " " << str2 << std::endl; std::cout << "str3: " << str3.getLength() << " " << str3 << std::endl; BinaryString bstr1("101010"); BinaryString bstr2("110011"); BinaryString bstr3 = bstr1 + bstr2; std::cout << "bstr1: " << bstr1.getLength() << " " << bstr1 << std::endl; std::cout << "bstr2: " << bstr2.getLength() << " " << bstr2 << std::endl; std::cout << "bstr3: " << bstr3.getLength() << " " << bstr3 << std::endl; return 0; } 21.12.2023 задание 1: class Firma { private: string company_name; string owner; string phone; string adress; string occupation; public: Firma() = default; Firma(const string &name, const string &own, const string &nphone, const string &adr, const string &occup); void Set(const string &name, const string &own, const string &nphone, const string &adr, const string &occup); void SetFirmName(const string &n); void SetOwnerName(const string &on); void SetPhone(const string &p); void SetAdress(const string &a); void SetOccupation(const string &o); string GetFirmName() const; string GetOwnerName() const; string GetPhone() const; string GetAdress() const; string GetOccupation() const; friend bool operator == (const Firma &lh, const Firma &rh); friend ostream &operator << (ostream &os, const Firma &f); }; // Firma.cpp Firma::Firma(const string &name, const string &own, const string &nphone, const string &adr, const string &occup) { Set(name, own, nphone, adr, occup); } void Firma::Set(const string &name, const string &own, const string &nphone, const string &adr, const string &occup) { company_name = name; owner = own; phone = nphone; adress = adr; occupation = occup; } void Firma::SetFirmName(const string &n) { company_name = n; } void Firma::SetOwnerName(const string &on) { owner = on; } void Firma::SetPhone(const string &p) { phone = p; } void Firma::SetAdress(const string &a) { adress = a; } void Firma::SetOccupation(const string &o) { occupation = o; } string Firma::GetFirmName() const { return company_name; } string Firma::GetOwnerName() const { return owner; } string Firma::GetPhone() const { return phone; } string Firma::GetAdress() const { return adress; } string Firma::GetOccupation() const { return occupation; } bool operator == (const Firma &lh, const Firma &rh) { return lh.company_name == rh.company_name && lh.owner == rh.owner && lh.phone == rh.phone && lh.adress == rh.adress && lh.occupation == rh.occupation; } ostream &operator << (ostream &os, const Firma &f) { return os << f.company_name << '\n' << f.owner << '\n' << f.phone << '\n' << f.adress << '\n' << f.occupation << '\n' << endl; } // Handbook.h class Handbook { private: vector items; public: void AddItem(const Firma &i); bool DeleteItem(const Firma &i); void PrintAll(ostream &os); bool FindByFirmName(const string &name, Firma &f); bool FindByOwner(const string &owner, Firma &f); bool FindByPhone(const string &phone, Firma &f); bool FindByAdress(const string &adress, Firma &f); bool FindByOccupation(const string &occupation, Firma &f); Handbook FindByOwner(const string &owner); void saveFirms(const string &filename); void readFromFirms(const string &filename); }; // Handbook.cpp void Handbook::AddItem(const Firma &i) { items.push_back(i); } bool Handbook::DeleteItem(const Firma &i) { for (vector::iterator it = items.begin(); it != items.end(); ++it) if (*it == i) { items.erase(it); return true; } return false; } void Handbook::PrintAll(ostream &os) { if (items.empty()) { os << "Handbook is empty"; return; } for (vector::iterator it = items.begin(); it != items.end(); ++it) { os << *it; } } bool Handbook::FindByFirmName(const string &name, Firma &f) { for (vector::iterator it = items.begin(); it != items.end(); ++it) if (it->GetFirmName() == name) { f = *it; return true; } return false; } bool Handbook::FindByOwner(const string &owner, Firma &f) { for (vector::iterator it = items.begin(); it != items.end(); ++it) if (it->GetOwnerName() == owner) { f = *it; return true; } return false; } bool Handbook::FindByPhone(const string &phone, Firma &f) { for (vector::iterator it = items.begin(); it != items.end(); ++it) if (it->GetPhone() == phone) { f = *it; return true; } return false; } bool Handbook::FindByAdress(const string &adress, Firma &f) { for (vector::iterator it = items.begin(); it != items.end(); ++it) if (it->GetAdress() == adress) { f = *it; return true; } return false; } bool Handbook::FindByOccupation(const string &occupation, Firma &f) { for (vector::iterator it = items.begin(); it != items.end(); ++it) if (it->GetOccupation() == occupation) { f = *it; return true; } return false; } Handbook Handbook::FindByOwner(const string &owner) { Handbook t; for (vector::iterator it = items.begin(); it != items.end(); ++it) if (it->GetOwnerName() == owner) { t.AddItem(*it); } return t; } void Handbook::saveFirms(const string &filename) { ofstream fs; fs.open(filename, fstream::out); if (fs.is_open()) { for (vector::iterator it = items.begin(); it != items.end(); ++it) { fs << "FirmName: " << it->GetFirmName() << endl << "OwnerName: " << it->GetOwnerName() << endl << "Phone: " << it->GetPhone() << endl << "Adress: " << it->GetAdress() << endl << "Occupation: " << it->GetOccupation() << endl << "---------------------------" << endl; } } fs.close(); } void Handbook::readFromFirms(const string &filename) { ifstream fs; Firma firma; string buf; int j = 0; items.clear(); fs.open(filename, ios::in); if (fs.is_open()) { do { fs >> buf >> buf; firma.SetFirmName(buf); fs >> buf >> buf; firma.SetOwnerName(buf); fs >> buf >> buf; firma.SetPhone(buf); fs >> buf >> buf; firma.SetAdress(buf); fs >> buf >> buf; firma.SetOccupation(buf); fs >> buf; if (!fs.eof()) items.push_back(firma); } while (!fs.eof()); } fs.close(); } // main.cpp const char *indata[][5] = { { "Peter-I", "Peter", "Phone0", "Adress0", "Occupation0" }, { "Boanerges", "John", "Phone1", "Adress1", "Occupation1" }, { "Thomas&Co", "Thomas", "Phone2", "Adress2", "Occupation2" }, { "J&J", "John", "Phone3", "Adress3", "Occupation3" }, { "Peter-II", "Peter", "Phone4", "Adress4", "Occupation4" }, { "Peter-III", "Peter", "Phone5", "Adress5", "Occupation5" } }; const char *succ = "Success!\n"; const char *fail = "Fail ((\n"; int main() { string filename = "Firms.txt"; Handbook h; Firma f; for (size_t i = 0; i < sizeof(indata) / sizeof(indata[0]); ++i) { f.Set(indata[i][0], indata[i][1], indata[i][2], indata[i][3], indata[i][4]); h.AddItem(f); } cout << "--- Initial data:\n"; h.PrintAll(cout); h.saveFirms(filename); h.readFromFirms(filename); cout << "--- Data from file:\n"; h.PrintAll(cout); cout << "--- Find by FirmName:\n"; if (h.FindByFirmName(indata[1][0], f)) cout << succ << "\"" << indata[1][0] << "\" found\n" << f << endl; else cout << fail << "\"" << indata[1][0] << "\" not found" << endl; if (h.FindByFirmName(indata[1][1], f)) cout << succ << "\"" << indata[1][1] << "\" found\n" << f << endl; else cout << fail << "\"" << indata[1][1] << "\" not found" << endl; cout << "--- Find by Owner:\n"; if (h.FindByOwner(indata[4][1], f)) cout << succ << "\"" << indata[4][1] << "\" found\n" << f << endl; else cout << fail << "\"" << indata[4][1] << "\" not found" << endl; if (h.FindByOwner(indata[4][2], f)) cout << succ << "\"" << indata[4][2] << "\" found\n" << f << endl; else cout << fail << "\"" << indata[4][2] << "\" not found" << endl; // ... etc. // find all occurrences! cout << "--- Find by Owner all occurrences of \"" << indata[4][1] << "\":\n"; Handbook found = h.FindByOwner(indata[4][1]); found.PrintAll(cout); cout << "--- Find by Owner all occurrences of \"" << indata[4][2] << "\":\n"; found = h.FindByOwner(indata[4][2]); found.PrintAll(cout); return 0; }