diff options
Added todays oop practice
1 files changed, 275 insertions, 0 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/05-Practice-22.03.2022.cpp | 275 |
diff --git a/Year 1/Object-oriented Programming/05-Practice-22.03.2022.cpp b/Year 1/Object-oriented Programming/05-Practice-22.03.2022.cpp new file mode 100644 index 0000000..02600bf --- /dev/null +++ b/Year 1/Object-oriented Programming/05-Practice-22.03.2022.cpp @@ -0,0 +1,275 @@ +#include <iostream> +#include <cstring> + +// 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) { + int buff = true; + while (x != 0) { + if (x & 1) buff = false; + x >>= 1; + + res = !res; + } + return res; +} + +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; + + public: + Interval(); + Interval(int a, int b); + + int get_a() const; + int get_b() const; + + int getLen() const; + bool isInInterval(int x) const; + + int countOfWholeNumbers() 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::countOfWholeNumbers() const { + return this->b - this->a; +} + +int Interval::palindromesCount() const { + int count = 0; + for (int i = a; i <= b; i++) { + if (isPalindrome(i)) + count++; + } + return count; +} + +int Interval::numsNonRepeatingDigitsCount() const { + int count = 0; + for (int i = 0; i <= b; i++) { + if (hasRepeatingDigits(i)) + count++; + } + return count; +} + +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(); +} |
