Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 05-Practice-22.03.2022.cpp
blob: ef09940bdb1813964082fc32b88f46c6b3c1ecc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#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) {
	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();
}