Question: Data Structure C++ language Modify the Ferry Problem as follows: Replace the 2d array of integers in the goodCrossing with a 1d array of Transition
Data Structure C++ language
Modify the Ferry Problem as follows:
Replace the 2d array of integers in the goodCrossing with a 1d array of Transition structs.
For example, A transition struct is
struct Transition
{
int near_side;
int far_side;
int type;
};
without changing any other function in the Ferry Problem.
All below is what was given.
Cargo.cpp
#include "Cargo.h"
int Cargo::charToCargo(char cargo)
{
switch(cargo)
{
case 'w': case 'W':
return WOLF;
case 's': case 'S':
return SHOWMAN;
case 'g': case 'G':
return GOAT;
case 'c': case 'C':
return CABBAGES;
default:
return NOCARGO;
}
}
char Cargo::cargoToChar(int cargo)
{
switch(cargo)
{
case WOLF:
return 'W';
case SHOWMAN:
return 'S';
case GOAT:
return 'G';
case CABBAGES:
return 'C';
case NOCARGO:
return 'N';
default:
return 'B';
}
}
bool Cargo::isCargo(char candidate)
{
switch(candidate)
{
case 'w': case 'W':
case 's': case 'S':
case 'g': case 'G':
case 'C': case 'c':
case 'N': case 'n':
return true;
default:
return false;
}
}
int Cargo::read(void)
{
char cargo;
do
{
cout << "Enter W for Wolf" << endl;
cout << "Enter N for no cargo" << endl;
cout << "Enter G for Goat" << endl;
cout << "Enter C for cabbages" << endl;
cout << "Enter cargo - ";
cin.get(cargo);
cin.ignore(numeric_limits
}
while(!isCargo(cargo));
return charToCargo(cargo);
}
Cargo.h
#pragma once
#include
#include
#include
using namespace std;
class Cargo
{
public:
static int read(void);
static int charToCargo(char);
static char cargoToChar(int);
static bool isCargo(char);
static const int BADCARGO = -1;
static const int CABBAGES = 0;
static const int GOAT = 1;
static const int SHOWMAN = 2;
static const int WOLF = 3;
static const int NOCARGO = 4;
static const int CARGO_AND_SHOWMAN = 5;
};
River.h
#pragma once
#include
#include "cargo.h"
class River
{
public:
River(void);
void show(void);
void cross(int);
bool stop(void);
private:
static const int stop_state = 15;
bool _far_side[Cargo::CARGO_AND_SHOWMAN];
bool _near_side[Cargo::CARGO_AND_SHOWMAN];
void copy(bool[], bool[]);
int state(bool[]);
bool goodCrossing(bool [], bool []);
};
River.cpp
#include "River.h"
River::River(void)
{
for (int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
_far_side[pos] = false;
}
void River::show(void)
{
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
if(_far_side[pos])
cout << "- | " << Cargo::cargoToChar(pos) << endl;
else
cout << Cargo::cargoToChar(pos) << " | -" << endl;
cout << endl;
}
void River::cross(int cargo)
{
copy(_far_side, _near_side);
_far_side[Cargo::SHOWMAN] = !_near_side[Cargo::SHOWMAN];
_far_side[cargo] = !_near_side[cargo];
if(!goodCrossing(_near_side, _far_side))
copy(_near_side, _far_side);
}
void River::copy(bool source[], bool sink[])
{
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
sink[pos] = source[pos];
}
int River::state(bool river[])
{
int _state = 0;
for(int pos = Cargo::CABBAGES; pos <= Cargo::WOLF; pos++)
_state = _state + river[pos] * (int)pow(2, pos);
return _state;
}
bool River::goodCrossing(bool this_side[], bool that_side[])
{
static const int I = 0;
static const int B = 1;
static const int G = 2;
static const int n_states = 16;
static int crossing_table[][n_states] =
{{I,I,I,I,B,B,G,I,I,I,I,I,B,I,I,I},
{I,I,I,I,I,B,I,G,I,I,I,I,I,G,I,I},
{I,I,I,I,I,I,G,G,I,I,I,I,I,I,G,I},
{I,I,I,I,I,I,I,B,I,I,I,I,I,I,I,B},
{B,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I},
{B,B,I,I,I,I,I,I,I,I,I,I,I,I,I,I},
{G,I,G,I,I,I,I,I,I,I,I,I,I,I,I,I},
{I,G,G,B,I,I,I,I,I,I,I,I,I,I,I,I},
{I,I,I,I,I,I,I,I,I,I,I,I,B,G,G,I},
{I,I,I,I,I,I,I,I,I,I,I,I,I,G,I,G},
{I,I,I,I,I,I,I,I,I,I,I,I,I,I,B,B},
{I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,B},
{B,I,I,I,I,I,I,I,B,I,I,I,I,I,I,I},
{I,G,I,I,I,I,I,I,G,G,I,I,I,I,I,I},
{I,I,G,I,I,I,I,I,G,I,B,I,I,I,I,I},
{I,I,I,B,I,I,I,I,I,G,B,B,I,I,I,I}};
int this_side_state = state(this_side);
int that_side_state = state(that_side);
return crossing_table[this_side_state][that_side_state] == G;
}
bool River::stop(void)
{
return state(_far_side) == stop_state;
}
Source.cpp
#include
#include
#include "cargo.h"
#include "river.h"
using namespace std;
int main()
{
River river;
river.show();
do
{
int cargo = Cargo::read();
river.cross(cargo);
river.show();
}
while(!river.stop());
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
