Question
HELP!!!! I need to create a recursive function but I'm not able to implement it without a helper function which isn't what what my professor
HELP!!!! I need to create a recursive function but I'm not able to implement it without a helper function which isn't what what my professor wants :-/ PLEASE HELP!!!
INTRODUCTION
You haven hired by a coalition of nations to circumvent the security system of an evil cabal! In particular, you need to write a function that uses a brute-force approach to guess all possible passwords of three high-level cabal members: Mr. Mean, Ms. Chief, and Dr. Evil, and two other accounts, Guest and Dave. As you may know, recursion is often helpful in such a situation.
Previous intelligence gathering has determined that all five passwords have length at most 8 and contain only lowercase letters (a-z) and digits (0-9. The following additional information about each password has also been uncovered:
1. Guests password is "passw123". 2. Daves password consists of exactly 4 digits. 3. Mr. Means password consists of only the characters g and r. 4. Ms. Chiefs password starts with "haha". 5. Dr. Evils password starts or ends with the string "gato".
Because of the cabals security system, your function must be able to correctly guess all five pass- words in under three seconds.
INSTRUCTIONS
The following files have been given to you:
A C++ source file (evilcomputer.h) declaring a replica of the cabals computer system.
A C++ source file (evilcomputer.cpp) implementing a replica of the cabals computer system.
A C++ header file (hack.h) defining a password hacking function.
A C++ source file (main.cpp) containing a main function with tests.
Create a new C++ source file named hack.cpp and implenet the function declared in hack.h, so that hack.cpp and the provided files compile into a program that runs with no failed tests.
//// EVILCOMPUTER.CPP ////
#include #include #include #include "evilcomputer.h" using namespace std; EvilComputer :: EvilComputer(string dave_pw, string mr_mean_pw, string ms_chief_pw, string dr_evil_pw) { for (int i = 0; i < 5; ++i) hacked[i] = false; passwords[index("Guest")] = "passw123"; passwords[index("Dave")] = dave_pw; passwords[index("Mr. Mean")] = mr_mean_pw; passwords[index("Ms. Chief")] = ms_chief_pw; passwords[index("Dr. Evil")] = dr_evil_pw; } EvilComputer :: EvilComputer() { for (int i = 0; i < 5; ++i) hacked[i] = false; passwords[index("Guest")] = "passw123"; string password; int length; password = ""; while (password.length() < 4) password += '0' + (rand() % 10); passwords[index("Dave")] = password; password = ""; length = 1 + rand() % 8; while (password.length() < length) { if (rand() % 2) password += 'g'; else password += 'r'; } passwords[index("Mr. Mean")] = password; password = "haha"; length = 1 + rand() % 8; while (password.length() < length) password += random_char(); passwords[index("Ms. Chief")] = password; password = ""; length = 4 + rand() % 5; while (password.length() < length - 4) password += random_char(); if (rand() % 2 == 0) password = "gato" + password; else password = password + "gato"; passwords[index("Dr. Evil")] = password; } char EvilComputer :: random_char() { char c = rand() % 36; if (c < 10) c += 48; else c += (97-10); return c; } bool EvilComputer :: guess(string user, string guess) { int user_index = index(user); if (user_index == -1) return false; if (guess != passwords[user_index]) return false; hacked[user_index] = true; return true; } bool EvilComputer :: is_hacked(string user) { return hacked[index(user)]; } int EvilComputer :: index(string user) { if (user == "Mr. Mean") return 0; else if (user == "Ms. Chief") return 1; else if (user == "Dr. Evil") return 2; else if (user == "Dave") return 3; else if (user == "Guest") return 4; return -1; }
//// EVILCOMPUTER.H ////
#ifndef EVILCOMPUTER_H #define EVILCOMPUTER_H #include using namespace std; class EvilComputer { public: // Creates a new evil computer with random passwords. EvilComputer(); // Creates a new evil computer with specific passwords. EvilComputer(string dave_pw, string mr_mean_pw, string ms_chief_pw, string dr_evil_pw); // Guess a password for an agent. // Returns whether the password guessed is correct. bool guess(string user, string guess); // Returns whether an agent's password has been correctly guessed. bool is_hacked(string user); private: int index(string user); char random_char(); string passwords[5]; bool hacked[5]; }; #endif
//// HACK.H ////
#include "evilcomputer.h" void hack(EvilComputer* ec);
//// MAIN.CPP ////
#include #include #include #include "evilcomputer.h" #include "hack.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file; cerr << ", line " << line << "." << endl; abort(); } #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) int main() { srand(2017); // Initialize random number generation // Create five computers with specific passwords EvilComputer ec1("1234", "grrr", "haha0", "gato1"); EvilComputer ec2("1235", "gggrrr", "haha456", "gato123"); EvilComputer ec3("2345", "gr", "haha0abc", "123gato"); EvilComputer ec4("6789", "grggrrrg", "hahaz123", "abc9gato"); EvilComputer ec5("9999", "rrrrrrrr", "hahahaha", "gatogato"); // Try to hack each of the five computers hack(&ec1); hack(&ec2); hack(&ec3); hack(&ec4); hack(&ec5); // Test that Guest's account is always hacked test(ec1.is_hacked("Guest")); test(ec2.is_hacked("Guest")); test(ec3.is_hacked("Guest")); test(ec4.is_hacked("Guest")); test(ec5.is_hacked("Guest")); // Test that Dave's account is always hacked test(ec1.is_hacked("Dave")); test(ec2.is_hacked("Dave")); test(ec3.is_hacked("Dave")); test(ec4.is_hacked("Dave")); test(ec5.is_hacked("Dave")); // Test that Mr. Mean's account was always hacked test(ec1.is_hacked("Mr. Mean")); test(ec2.is_hacked("Mr. Mean")); test(ec3.is_hacked("Mr. Mean")); test(ec4.is_hacked("Mr. Mean")); test(ec5.is_hacked("Mr. Mean")); // Test that Ms. Chief's account was always hacked test(ec1.is_hacked("Ms. Chief")); test(ec2.is_hacked("Ms. Chief")); test(ec3.is_hacked("Ms. Chief")); test(ec4.is_hacked("Ms. Chief")); test(ec5.is_hacked("Ms. Chief")); // Test that Dr. Evil's account was always hacked test(ec1.is_hacked("Dr. Evil")); test(ec2.is_hacked("Dr. Evil")); test(ec3.is_hacked("Dr. Evil")); test(ec4.is_hacked("Dr. Evil")); test(ec5.is_hacked("Dr. Evil")); // Test hacking some "randomly" chosen passwords for (int i = 0; i < 10; ++i) { // Try to hack all three users with randomly chosen passwords // (matching each user's password rules) EvilComputer ec; hack(&ec); test(ec.is_hacked("Guest")); test(ec.is_hacked("Dave")); test(ec.is_hacked("Mr. Mean")); test(ec.is_hacked("Ms. Chief")); test(ec.is_hacked("Dr. Evil")); } cout << "Assignment complete." << endl; }
If possible, please leave notes! Thank you so much!!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started