Question

//evil_server.cpp #include <string> #include <cstdlib> #include <iostream> #include "evil_server.h" using namespace std; EvilServer :: EvilServer() {...

//evil_server.cpp

#include <string>
#include <cstdlib>
#include <iostream>
#include "evil_server.h"

using namespace std;

EvilServer :: EvilServer()
{
   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 EvilServer :: 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 EvilServer :: 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 EvilServer :: random_char()
{
   char c = rand() % 36;
   if (c < 10)
       c += 48;
   else
       c += (97-10);
   return c;
}

string EvilServer :: agent_name(EvilAgent agent)
{
   switch(agent)
   {
       case DrEvil:
           return "Dr. Evil";
       case MsChief:
           return "Ms. Chief";
       case MrMean:
           return "Mr. Mean";
   }
}

int EvilServer :: agent_index(EvilAgent agent)
{
   switch(agent)
   {
       case DrEvil:
           return 0;
       case MsChief:
           return 1;
       case MrMean:
           return 2;
   }
}

bool EvilServer :: guess(EvilAgent agent, string guess)
{
   string name = agent_name(agent);
   int index = agent_index(agent);
   if (guess != passwords[index] || hacked[index])
       return false;
   hacked[index] = true;
   return true;
}

bool EvilServer :: is_hacked(EvilAgent agent)
{
   return hacked[agent_index(agent)];
}

//***************************************************************************

//evil_server.h

#ifndef EVILSERVER_H
#define EVILSERVER_H

#include <string>

using namespace std;

enum EvilAgent {DrEvil, MsChief, MrMean};

class EvilServer
{
   public:
       EvilServer();
       bool guess(EvilAgent agent, string guess);
       void change_pw(EvilAgent agent, string new_pw);
       bool is_hacked(EvilAgent agent);

   private:
       static string agent_name(EvilAgent agent);
       static int agent_index(EvilAgent agent);
       static string random_pw(EvilAgent agent);
       static char random_char();
       bool hacked[3];
       string passwords[3];
};

#endif

//************************************************************

//hack.h

#include "evil_server.h"

void hack(EvilServer &es);

//*******************************************************

//main.cpp

#include <iostream>
#include <cassert>
#include <string>
#include <cstdlib>
#include <ctime>
#include "evil_server.h"
#include "hack.h"

using namespace std;

clock_t test_start;

bool test_time_exceeds(int seconds)
{
   clock_t cur = clock();
   return seconds <= static_cast<float>(cur-test_start)/CLOCKS_PER_SEC;
}

int main()
{
   // Initialize random number generation, i.e. rand(), using current millisecond
   srand(2017);
   test_start = clock();

   // Hack something  
   EvilServer es1;
   es1.change_pw(MrMean, "grrr");
   es1.change_pw(MsChief, "haha");
   es1.change_pw(DrEvil, "gato1");
   hack(es1);
   assert(es1.is_hacked(DrEvil) || es1.is_hacked(MsChief) || es1.is_hacked(MrMean));
   cout << "20% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);
  
   // Hack something  
   es1.change_pw(MrMean, "gggrrr");  
   es1.change_pw(MsChief, "haha0");
   es1.change_pw(DrEvil, "1gato");
   hack(es1);
   assert(es1.is_hacked(DrEvil) || es1.is_hacked(MsChief) || es1.is_hacked(MrMean));
   cout << "30% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);
  
   // Hack something  
   es1.change_pw(MrMean, "grggrrrr");  
   es1.change_pw(MsChief, "haha0001");
   es1.change_pw(DrEvil, "gato0abc");
   hack(es1);
   assert(es1.is_hacked(DrEvil) || es1.is_hacked(MsChief) || es1.is_hacked(MrMean));
   cout << "40% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);

   // Hack all three
   es1.change_pw(MrMean, "grrr");  
   es1.change_pw(MsChief, "haha");
   es1.change_pw(DrEvil, "gato1");
   hack(es1);
   assert(es1.is_hacked(MrMean));
   assert(es1.is_hacked(MsChief));
   assert(es1.is_hacked(DrEvil));
   cout << "50% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);
  
   // Hack all three
   es1.change_pw(MrMean, "gggrrr");  
   es1.change_pw(MsChief, "haha0");
   es1.change_pw(DrEvil, "1gato");
   hack(es1);
   assert(es1.is_hacked(MrMean));
   assert(es1.is_hacked(MsChief));
   assert(es1.is_hacked(DrEvil));
   cout << "60% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);
      
   // Hack all three
   es1.change_pw(MrMean, "grggrrrr");  
   es1.change_pw(MsChief, "haha0001");
   es1.change_pw(DrEvil, "gato0abc");
   hack(es1);
   assert(es1.is_hacked(MrMean));
   assert(es1.is_hacked(MsChief));
   assert(es1.is_hacked(DrEvil));
   cout << "70% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);

   // Hack something (random passwords)
   EvilServer es2;
   hack(es2);  
   assert(es2.is_hacked(DrEvil) || es2.is_hacked(MsChief) || es2.is_hacked(MrMean));
   cout << "75% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);

   // Hack all three (random passwords)
   assert(es2.is_hacked(MrMean));
   assert(es2.is_hacked(MsChief));
   assert(es2.is_hacked(DrEvil));
   cout << "80% earned." << endl;  
   if (test_time_exceeds(60))
       exit(0);
  
   // Hack all three (random passwords, 3x)
   for (int i = 0; i < 3; ++i)
   {
       EvilServer es_tmp;
       hack(es_tmp);
       assert(es_tmp.is_hacked(MrMean));
       assert(es_tmp.is_hacked(MsChief));
       assert(es_tmp.is_hacked(DrEvil));
       if (test_time_exceeds(60))
           exit(0);
   }  
   cout << "90% earned." << endl;  

   // Hack all three (random passwords, 10x)
   for (int i = 0; i < 10; ++i)
   {
       EvilServer es_tmp;
       hack(es_tmp);
       assert(es_tmp.is_hacked(MrMean));
       assert(es_tmp.is_hacked(MsChief));
       assert(es_tmp.is_hacked(DrEvil));
       if (test_time_exceeds(60))
           exit(0);
   }  
   cout << "95% earned." << endl;  

   // Hack all three (random passwords, 50x)
   for (int i = 0; i < 50; ++i)
   {
       EvilServer es_tmp;
       hack(es_tmp);
       assert(es_tmp.is_hacked(MrMean));
       assert(es_tmp.is_hacked(MsChief));
       assert(es_tmp.is_hacked(DrEvil));
       if (test_time_exceeds(60))
           exit(0);
   }  
   cout << "100% earned." << endl;  

   return 0;
}


//*******************************************************

/*

You are a software engineer hired by a coalition of nations to circumvent the security system of an evil, multinational cabal. In particular, you need to write functions that use a brute-force approach to crack the passwords of three high-level cabal members: Mr. Mean, Ms. Chief, and Dr. Evil. As you already know, recursion is often helpful in such a situation.

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

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 code must be able to crack passwords within a few seconds.

Create a new C++ source file named hack.cpp and implement the function declared in hack.h, so that main.cpp, hack.cpp, and evil server.cpp compile into a program that runs with no failed assertions.

*/

Homework Answers

Answer #1

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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void modifyArray( int [ ], int );    void modifyElement ( int );      int main( ) {   const int arraySize = 8;   int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };      modifyArray ( a, arraySize);      for ( int i =0; i < arraySize; i++)               cout << a[i] << ‘  ’;         modifyElement ( a[4] );          for ( int i =0; i <...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std;...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std; int main() {        int x;        cout << "Selection option " << endl;        cin >> x;        if (x == 1)               cout << "You select option 1" << endl;        else if (x >= 2 || x <= 4)               cout << "You select options 2 or 3 or 4" << endl;               else if (x == 10)               cout << "You select option 10" << endl;               else...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
#include <iostream> #include <string> using namespace std; string STUDENT = "ckim84"; // Add your Blackboard login...
#include <iostream> #include <string> using namespace std; string STUDENT = "ckim84"; // Add your Blackboard login name extern string ASSIGNMENT; /** * Describe the purpose of your program here. * @return 0 for success. */ int run() { repeatChar(3, "abcde$"); return 0; } string repeatChar(int n, const string& pat) { string result; for (int i = 0; i < n; i++) { result += pat; } return result; } could you correct my code?? i got an error msg like...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT