Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Edit the code where it displays Edit here only for the program to pass all tests in the main.cpp file to output 100%. //hack.cpp #include
Edit the code where it displays "Edit here" only for the program to pass all tests in the main.cpp file to output 100%.
//hack.cpp
#include#include #include #include #include "noobcomputer.h" using namespace std; // EDIT HERE // You probably want a helper function to // build up the potential password strings recursively // The password is known to: // 1. Have length at most 4. // 2. Have only characters that are lowercase letters ('a'-'z'). void hack(NoobComputer* nc) { // EDIT HERE
} ===========================================================
//hack.h
#include#include #include #include #include "noobcomputer.h" using namespace std; // The password is known to: // 1. Have length at most 4. // 2. Have only characters that are lowercase letters ('a'-'z'). void hack(NoobComputer* nc); ======================================================
//main.cpp
#include#include #include #include #include #include "noobcomputer.h" #include "hack.h" using namespace std; // Testing stuff; just ignore it :) inline void _test(const char* expression, const char* file, int line) { cerr << "test(" << expression << ") failed in file " << file << ", line " << line << "." << endl; abort(); } #define test(EXPRESSION) ((EXPRESSION) ? (void)0 : _test(#EXPRESSION, __FILE__, __LINE__)) // End of testing stuff int main() { // Attempt to hack a computer with password "abc" NoobComputer nc1; nc1.change_pw("abc"); clock_t start = clock(); hack(&nc1); clock_t end = clock(); float duration = static_cast (end - start) / CLOCKS_PER_SEC; test(duration < 3.0); // Check that the hack didn't take too long test(nc1.is_hacked()); // Check that hack succeeded cout << "20% earned." << endl; // Attempt to hack a computer with password "xy" NoobComputer nc2; nc2.change_pw("xy"); start = clock(); hack(&nc2); end = clock(); duration = static_cast (end - start) / CLOCKS_PER_SEC; test(duration < 3.0); test(nc2.is_hacked()); cout << "30% earned." << endl; // Attempt to hack a computer with password "qqqq" NoobComputer nc3; nc3.change_pw("qqqq"); start = clock(); hack(&nc3); end = clock(); duration = static_cast (end - start) / CLOCKS_PER_SEC; test(duration < 3.0); test(nc3.is_hacked()); cout << "40% earned." << endl; // Attempt to hack a computer with password "pass" NoobComputer nc4; nc4.change_pw("pass"); start = clock(); hack(&nc4); end = clock(); duration = static_cast (end - start) / CLOCKS_PER_SEC; test(duration < 3.0); test(nc4.is_hacked()); cout << "50% earned." << endl; // Attempt to hack a computer with password "word" NoobComputer nc5; nc5.change_pw("word"); start = clock(); hack(&nc5); end = clock(); duration = static_cast (end - start) / CLOCKS_PER_SEC; test(duration < 3.0); test(nc5.is_hacked()); cout << "60% earned." << endl; // Random passwords float total_duration = 0.0; for (int i = 1; i <= 8; ++i) { NoobComputer nc; start = clock(); hack(&nc); end = clock(); duration = static_cast (end - start) / CLOCKS_PER_SEC; total_duration += duration; test(duration < 3.0); test(nc.is_hacked()); cout << 5 * i + 60 << "% earned." << endl; } cout << "8 Accounts hacked in: " << total_duration << " seconds." << endl; } =================================================== //noobcomputer.cpp
#include#include #include #include "noobcomputer.h" using namespace std; NoobComputer :: NoobComputer() { hacked = false; int length = (rand() % 5); password = ""; for (int i = 0; i < length; ++i) password += ('a' + (rand() % 26)); } void NoobComputer :: change_pw(string new_pw) { if (new_pw == password) return; hacked = false; password = new_pw; } bool NoobComputer :: guess(string guess) { if (guess != password) return false; hacked = true; return true; } bool NoobComputer :: is_hacked() { return hacked; } ====================================================
//noobcomputer.h
#ifndef NOOBCOMPUTER_H #define NOOBCOMPUTER_H #includeusing namespace std; class NoobComputer { public: // Creates a new computer with a random password NoobComputer(); // Changes the computer's password void change_pw(string new_pw); // Guess a password. // Returns whether the password is correct. bool guess(string guess); // Returns whether the correct password has been guessed. bool is_hacked(); private: string password; bool hacked; }; #endif
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