#include #include // Exercise 1 void dynInplaceReplaceStr(char*& dest, const char* src) { int strLen = strlen(src); if (dest != nullptr) delete[] dest; dest = new char[strLen]; strcpy(dest, src); } class Teacher { char* name; int age; int employmentTime; public: Teacher(const char* str, int age, int employmentTime); ~Teacher(); const char* get_name() const; void set_name(const char* str); int get_age() const; void set_age(int age); int get_employmentTime() const; void set_employmentTime(int employentTime); }; Teacher::Teacher(const char* str, int age, int employmentTime) { this->set_name(str); this->set_age(age); this->set_employmentTime(employmentTime); } Teacher::~Teacher() { delete[] this->name; } const char* Teacher::get_name() const { return this->name; } void Teacher::set_name(const char* str) { dynInplaceReplaceStr(this->name, str); } int Teacher::get_age() const { return this->age; } void Teacher::set_age(int age) { if (age < 0 || age > 130) return; this->age = age; } int Teacher::get_employmentTime() const { return this->employmentTime; } void Teacher::set_employmentTime(int employmentTime) { if (employmentTime < 0) return; this->employmentTime = employmentTime; } void ex1() { Teacher nang("Antoan Gionov", 25, 5); std::cout << nang.get_name() << " " << nang.get_age() << " " << nang.get_employmentTime(); } // Exercise 2 class Secret { char* task; char* password; int failedAttempts; bool isValidPasswd(const char* passwd) const { return !strcmp(this->password, passwd); } public: Secret(const char* task, const char* password); ~Secret(); const char* getTask(const char* pwd); bool setPassword(const char* newPassword, const char* oldPassword); bool setTask(const char* newTask, const char* passwd); int getLoginFails() const; }; Secret::Secret(const char* task, const char* password) { this->task = this->password = nullptr; this->setTask(task, ""); this->setPassword(password, ""); this->failedAttempts = 0; } Secret::~Secret() { delete[] this->task; delete[] this->password; } const char* Secret::getTask(const char* pwd) { if (!this->isValidPasswd(pwd)) { this->failedAttempts++; return ""; } return this->task; } bool Secret::setPassword(const char* newPassword, const char* oldPassword) { if (this->password != nullptr && !this->isValidPasswd(oldPassword)) { this->failedAttempts++; return false; } dynInplaceReplaceStr(this->password, newPassword); return true; } bool Secret::setTask(const char* newTask, const char* passwd) { if (this->password != nullptr && !this->isValidPasswd(passwd)) { this->failedAttempts++; return false; } dynInplaceReplaceStr(this->task, newTask); return true; } int Secret::getLoginFails() const { return this->failedAttempts; } void ex2() { Secret mySecret("Not determined", "abv"); std::cout << mySecret.getTask("abv") << std::endl; mySecret.setTask("What is the answer of everything?", "abv"); mySecret.setTask("Solve 2+2", "bruh"); mySecret.setPassword("bruh", "abv"); std::cout << mySecret.getTask("bruh") << std::endl; std::cout << mySecret.getLoginFails() << std::endl; } // Exercise 3 bool isPowerOf2(int x) { bool oneOccured = false; while (x != 0) { if (x % 2 == 1 && oneOccured) return false; if(x % 2 == 1) oneOccured = true; x /= 2; } return true; } bool isPalindrome(int num) { int buff = 0, tNum = num; while (tNum != 0) { buff = buff * 10 + (tNum % 10); tNum /= 10; } return buff == num; } bool hasRepeatingDigits(int num) { bool digitOccur[] = { false, false, false, false, false, false, false, false, false, false }; while (num != 0) { if (digitOccur[num % 10]) return false; digitOccur[num % 10] = true; num /= 10; } return true; } class Interval { int a; int b; bool edgesPowersOf2; int counter(bool (comparator)(int)) const { int count = 0; for (int i = a; i <= b; i++) { if (comparator(i)) count++; } return count; } public: Interval(); Interval(int a, int b); int get_a() const; int get_b() const; int getLen() const; bool isInInterval(int x) const; int primeNumbersCount() const; int palindromesCount() const; int numsNonRepeatingDigitsCount() const; int endBegArePowersOf2() const; Interval intervalUnion(Interval inter) const; bool isSubInterval(Interval inter) const; }; Interval::Interval() : Interval(0, 0) {} Interval::Interval(int a, int b) { if (a < b) a = b = 0; this->a = a; this->b = b; this->edgesPowersOf2 = isPowerOf2(a) && isPowerOf2(b); } int Interval::get_a() const { return this->a; } int Interval::get_b() const { return this->b; } int Interval::getLen() const { return this->b - this->a; } bool Interval::isInInterval(int x) const { return a >= x && x <= b; } int Interval::palindromesCount() const { return this->counter(isPalindrome); } int Interval::numsNonRepeatingDigitsCount() const { return this->counter(hasRepeatingDigits); } int Interval::endBegArePowersOf2() const { return this->edgesPowersOf2; } Interval Interval::intervalUnion(Interval inter) const { if (this->b < inter.get_a() || this->a > inter.get_b()) return Interval(); if (this->a <= inter.get_a()) return Interval(inter.a, this->b); else return Interval(this->a, inter.b); } bool Interval::isSubInterval(Interval inter) const { return this->a >= inter.a && inter.b <= this->b; } // Main int main() { ex2(); }