#include <iostream> #include <string> #include <vector> #include <random> #include…
- User
#include #include #include #include #include "Log.h" #include "Log.cpp" #include "Passgen.h"
using namespace std;
#ifndef PASSGEN #define PASSGEN
class Options { public: virtual bool lower() const = 0; virtual bool upper() const = 0; virtual bool nums() const = 0; virtual bool specchars() const = 0; virtual int len() const = 0; };
class Fabric { public: virtual string gen_password() const = 0; };
class PassOptions : public Options { bool addlower; bool addupper; bool addnums; bool addspec; int passlen;
public: PassOptions(bool lower, bool upper, bool nums, bool spec, int len) : addlower(lower), addupper(upper), addnums(nums), addspec(spec), passlen(len) {} bool lower() const override { return addlower; } bool upper() const override { return addupper; } bool nums() const override { return addnums; } bool specchars() const override { return addspec; } int len() const override { return passlen; }};
class PassFabric : public Fabric {
Options &options; char randomChar(const string &category) const { static mt19937 rng(random_device{}()); uniform_int_distribution<> dist(0, category.length() - 1); return category[dist(rng)]; } public: PassFabric(Options &opts) : options(opts) {} string gen_password() const override { Log::get_ekzempl().write("ИНФО", "Запущен процесс ввода параметров"); string allowedChars; if (options.lower()) allowedChars += "abcdefghijklmnopqrstuvwxyz"; if (options.upper()) allowedChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (options.nums()) allowedChars += "0123456789"; if (options.specchars()) allowedChars += "!@#$%^&*()_-+=<>?";Log::get_ekzempl().write("ИНФО", "Запущен процесс ввода параметров"); if (allowedChars.empty()) throw runtime_error("No characters are allowed for password generation");
string password; for (int i = 0; i < options.len(); ++i) { password += randomChar(allowedChars); } return password; }};
class Com { public: virtual void execute() = 0; };
class PassGenCom : public Com { private: const Fabric &factory; int numberOfPasswords; vector &passwords;
public: PassGenCom(const Fabric &f, int numPasswords, vector &p) : factory(f), numberOfPasswords(numPasswords), passwords(p) {}
void execute() override { Log::get_ekzempl().write("ИНФО", "Запущен процесс ввода параметров"); try { for (int i = 0; i < numberOfPasswords; ++i) { passwords.push_back(factory.gen_password()); } } catch (const exception &e) { throw; } }};
class PassCheck { public: static bool normal_password(const string &password) { int score = 0; Log::get_ekzempl().write("ИНФО", "Запущен процесс ввода параметров"); for (char ch : password) { if (islower(ch)) score += 1; else if (isupper(ch)) score += 2; else if (isdigit(ch)) score += 3; else score += 5; } return score > 10; } }; #endif
- Assistant
To simplify
randomChar()function and avoid usingrng,dist, anduniform_int_distribution, you can userand()coupled with the modulo operator to select a random character from thecategorystring. Below is the simplified version ofrandomChar():char randomChar(const string &category) const { // Make sure that the category string is not empty if (category.empty()) { throw runtime_error("Category cannot be empty"); } // Generate a random index int index = rand() % category.length(); return category[index]; }But do remember to seed the
rand()function to get different sequences of random numbers each time the program is run. A suitable place to seed therand()function is in the main function or at the start of the program:int main() { // Seed the rand() function with the current time srand(static_cast<unsigned int>(time(nullptr))); // ... rest of your main function code }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.