Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help managing my code (the hack.cpp, file towards the way end) to pass the tests in main.cpp in under 3 seconds using this

I need help managing my code (the hack.cpp, file towards the way end) to pass the tests in main.cpp in under 3 seconds using this weeks lesson "recurrsion." Please feel free to edit my code wherever possible to sucessfully pass the program.

This is the scenerio: You are a software engineer hired by a coalition of nations to subvert the security system of an evil, multi- national 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. As you may know, recursion is often helpful in such a situation.

Previous intelligence gathering has determined that all three passwords have length at most 8 and consist of only lowercase letters and numbers. The following additional information about each password has also been uncovered:

1. Mr. Means password consists of only the characters g and r. 2. Ms. Chiefs password starts with "haha". 3. 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 three passwords in under three seconds.

The following files have been given to you: 1. A C++ source file (evilcomputer.h) defining a replica of the cabals computer system. 2. A C++ source file (evilcomputer.cpp) implementing a replica of the cabals computer system. 3. A C++ header file (hack.h) defining a password hacking function. 4. A C++ source file (main.cpp) containing a main function with tests. Create a new C++ source file named hack.cpp and implement the function declared in hack.h, so that main.cpp, hack.cpp, and evilcomputer.cpp compile into a program that runs with no failed tests and outputs 100%. =======================================================

//evilcomputer.cpp

#include  #include  #include  #include "evilcomputer.h" using namespace std; EvilComputer :: EvilComputer() { hacked[0] = hacked[1] = hacked[2] = false; passwords[agent_index(MrMean)] = random_pw(MrMean); passwords[agent_index(MsChief)] = random_pw(MsChief); passwords[agent_index(DrEvil)] = random_pw(DrEvil); } void EvilComputer :: change_pw(EvilAgent agent, string new_pw) { int index = agent_index(agent); if (new_pw == passwords[index]) return; hacked[index] = false; passwords[index] = new_pw; } string EvilComputer :: random_pw(EvilAgent agent) { string password; int length; switch (agent) { case DrEvil: length = 4 + (rand() % 5); for (int i = 0; i < length - 4; ++i) password += random_char(); if (rand() % 2) password = "gato" + password; else password += "gato"; break; case MsChief: length = 4 + (rand() % 5); password = "haha"; for (int i = 5; i <= length; ++i) password += random_char(); break; case MrMean: length = 1 + rand() % 8; for (int i = 0; i < length; ++i) if (rand() % 2) password += 'g'; else password += 'r'; break; } return password; } char EvilComputer :: random_char() { char c = rand() % 36; if (c < 10) c += 48; else c += (97-10); return c; } int EvilComputer :: agent_index(EvilAgent agent) { switch(agent) { case DrEvil: return 0; case MsChief: return 1; case MrMean: return 2; } } bool EvilComputer :: guess(EvilAgent agent, string guess) { int index = agent_index(agent); if (guess != passwords[index]) return false; hacked[index] = true; return true; } bool EvilComputer :: is_hacked(EvilAgent agent) { return hacked[agent_index(agent)]; } ============================================ //evilcomputer.h  
#ifndef EVILCOMPUTER_H #define EVILCOMPUTER_H #include  using namespace std; class EvilComputer { public: enum EvilAgent {DrEvil, MsChief, MrMean}; // Creates a new evil computer with random passwords // for all three agents (following their individual password rules). EvilComputer(); // Changes an agent's password to something else void change_pw(EvilAgent agent, string new_pw); // Guess a password for an agent. // Returns whether the password guessed is correct. bool guess(EvilAgent agent, string guess); // Returns whether an agent's password has been correctly guessed. bool is_hacked(EvilAgent agent); private: int agent_index(EvilAgent agent); string random_pw(EvilAgent agent); char random_char(); bool hacked[3]; string passwords[3]; }; #endif ================================================== 

//hack.h

#include "evilcomputer.h" void hack(EvilComputer* ec); 

==============================================

//main.cpp

#include  #include  #include  #include  #include "evilcomputer.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() { // Initialize random number generation for later password generation. srand(2017); // Attempt a hack of Mr. Mean's account EvilComputer ec1; ec1.change_pw(EvilComputer::MrMean, "grrr"); ec1.change_pw(EvilComputer::MsChief, "haha"); ec1.change_pw(EvilComputer::DrEvil, "gato1"); clock_t start = clock(); hack(&ec1); clock_t end = clock(); float duration = static_cast(end - start) / CLOCKS_PER_SEC; test(duration < 3.0); // Test that hacking attempt did not take too long cout << "10% earned." << endl; test(ec1.is_hacked(EvilComputer::MrMean)); cout << "20% earned." << endl; // Attempt another hack of Mr. Mean's account, // where he is using a different password EvilComputer ec2; ec2.change_pw(EvilComputer::MrMean, "gggrrr"); ec2.change_pw(EvilComputer::MsChief, "haha0"); ec2.change_pw(EvilComputer::DrEvil, "1gato"); start = clock(); hack(&ec2); end = clock(); duration = static_cast(end - start) / CLOCKS_PER_SEC; test(duration < 3.0); // Test that hacking attempt did not take too long cout << "25% earned." << endl; test(ec2.is_hacked(EvilComputer::MrMean)); cout << "30% earned." << endl; // Attempt a third hack of Mr. Mean's account, // where he is using a different password EvilComputer ec3; ec3.change_pw(EvilComputer::MrMean, "grggrrrr"); ec3.change_pw(EvilComputer::MsChief, "haha0001"); ec3.change_pw(EvilComputer::DrEvil, "gato0abc"); start = clock(); hack(&ec3); end = clock(); duration = static_cast(end - start) / CLOCKS_PER_SEC; test(duration < 3.0); // Test that hacking attempt did not take too long cout << "35% earned." << endl; test(ec3.is_hacked(EvilComputer::MrMean)); cout << "40% earned." << endl; // Test that previous hacks *also* succeeded in hacking // Ms. Chief's account (for three different passwords) test(ec1.is_hacked(EvilComputer::MsChief)); cout << "45% earned." << endl; test(ec2.is_hacked(EvilComputer::MsChief)); test(ec3.is_hacked(EvilComputer::MsChief)); cout << "50% earned." << endl; // Test that previous hacks *also* succeeded in hacking // Dr. Evil's account (for three different passwords) test(ec1.is_hacked(EvilComputer::DrEvil)); cout << "55% earned." << endl; test(ec2.is_hacked(EvilComputer::DrEvil)); test(ec3.is_hacked(EvilComputer::DrEvil)); cout << "60% earned." << endl; // Attempt hacks for unknown (randomly chosen) passwords float total_duration = 0.0; for (int i = 1; i <= 8; ++i) { EvilComputer ec; start = clock(); hack(&ec); end = clock(); duration = static_cast(end - start) / CLOCKS_PER_SEC; total_duration += duration; test(duration < 3.0); // Test that hacking attempt did not take too long test(ec.is_hacked(EvilComputer::MrMean)); test(ec.is_hacked(EvilComputer::MsChief)); test(ec.is_hacked(EvilComputer::DrEvil)); cout << 5*i + 60 << "% earned." << endl; } // If all 8 hacks happened in under 3 seconds total, then // 1337 h4ck3r 80nu5 p01nt5 test(total_duration < 3.0); cout << "110% earned." << endl; } ================================================== //hack.cpp //my file that needs to be edited.  

#include #include #include #include "hack.h"

using namespace std; void hackMrMean(EvilComputer* ec); void hackMsChief(EvilComputer* ec); void hackDrEvil(EvilComputer* ec); void aux_hackMrMean(EvilComputer* ec,string password, int d); void aux_hackMsChief(EvilComputer* ec,string password, int d); void aux_hackDrEvil(EvilComputer* ec,string password, int d); char getChar(int j);

void hack(EvilComputer* ec) { hackMrMean(ec); hackMsChief(ec); hackDrEvil(ec); } void hackMrMean(EvilComputer* ec) { aux_hackMrMean(ec,"",0); } void aux_hackMrMean(EvilComputer* ec,string password, int d) { if(ec->is_hacked(EvilComputer::MrMean)|| ec->guess(EvilComputer::MrMean,password) || d==9) return;

aux_hackMrMean(ec,password+'g',d+1); aux_hackMrMean(ec,password+'r',d+1); } void hackMsChief(EvilComputer* ec) { aux_hackMsChief(ec,"",0); } void aux_hackMsChief(EvilComputer* ec,string password, int d) { if(ec->is_hacked(EvilComputer::MsChief)|| ec->guess(EvilComputer::MsChief,"haha"+password) || d==5) return; for(int j=0;j<36;j++) { aux_hackMsChief(ec,password+getChar(j),d+1);

}

} void hackDrEvil(EvilComputer* ec) { aux_hackDrEvil(ec,"",0); } void aux_hackDrEvil(EvilComputer* ec,string password, int d) { if(ec->is_hacked(EvilComputer::DrEvil)|| ec->guess(EvilComputer::DrEvil,"gato"+password) || ec->guess(EvilComputer::DrEvil,password+"gato") || d==5) return; for(int j=0;j<36;j++) aux_hackDrEvil(ec,password+getChar(j),d+1);

} char getChar(int j) { if(j<26) return 'a'+j; else return '0'+j-26; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

Students also viewed these Databases questions

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago