You are a software engineer hired by a coalition of nations to subvert the secur
ID: 3810505 • Letter: Y
Question
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. Mean’s password consists of only the characters ’g’ and ’r’. 2. Ms. Chief’s password starts with "haha".
3. Dr. Evil’s password starts or ends with the string "gato".
Because of the cabal’s 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 cabal’s computer system.
2. A C++ source file (evilcomputer.cpp) implementing a replica of the cabal’s 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
//hack.h
==============================================
//main.cpp
/*
EDIT HERE
*/
Explanation / Answer
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.