resurce
Мастер
(2303)
1 неделю назад
#include <exception>
class ExceptionAttempts : public std::exception {
public:
const char* what() const noexcept override {
return "Too many attempts";
}
};
class Proxy : public IVault {
private:
IVault* realObject;
int attempts, failedAttempts;
bool isLocked;
public:
Proxy(IVault* realObject, int attempts) : realObject(realObject), attempts(attempts), failedAttempts(0), isLocked(false) {}
bool unlock(int pass) override {
if (isLocked) throw ExceptionAttempts();
bool result = realObject->unlock(pass);
if (!result) {
if (++failedAttempts >= attempts) {
isLocked = true;
throw ExceptionAttempts();
}
} else isLocked = true;
return result;
}
};
Сергей
Гений
(56722)
1 неделю назад
Размер приложения не зависит от этого кода, никаких лишних библиотек не используется, статической и динамической памяти тоже. Если проверяющая программа сама себе накидала более мегабайта и сама же жалуется на это - тут наука бессильна)
#include <exception>
class ExceptionAttempts : public std::exception {
public:
const char* what() const noexcept override {
return "Too many attempts";
}
};
class Proxy : public IVault {
private:
IVault* realObject;
int attempts;
int failedAttempts;
bool isLocked;
public:
Proxy(IVault* realObject, int attempts) : realObject(realObject), attempts(attempts), failedAttempts(0), isLocked(false) {}
bool unlock(int pass) override {
if (isLocked) {
throw ExceptionAttempts();
}
bool result = realObject->unlock(pass);
if (!result) {
failedAttempts++;
if (failedAttempts >= attempts) {
isLocked = true;
throw ExceptionAttempts();
}
} else {
isLocked = true;
}
return result;
}
};