1A)
void Station::ResetAll() {
for (int a = 0; a < myPumps.length(); a++)
myPumps[a].ResetGallonsSold();
}
1B)
double Station::TotalSales() const {
double total = 0.0;
for (int a = 0; a < myPumps.length(); a++) {
if (a < 2)
total +=
myPumps[a].GallonsSold() * (myBasePrice + .25);
else
total +=
myPumps[a].GallonsSold() * myBasePrice;
}
return total;
}
1C)
void Station::CloseStation(ostream &logFile) {
logFile << TotalSales();
ResetAll();
}
2A)
bool LessThan(const Book & lhs, const Book & rhs) {
return ((lhs.lowAge < rhs.lowAge) || ((lhs.lowAge
== rhs.lowAge) &&
(lhs.highAge < rhs.highAge)));
}
2B)
BookList::InsertOne(const book & bk) {
// first we need to figure out where bk needs to be
inserted, and that will
// be after the last item that is smaller than our
book
int a = 0;
while (!LessThan(bk, myList[a])
a++;
// ok, now we have the position... now we gotta
insert
// stage 1 is we need to move every item one over to
make room
// start at the end, and move it one over to an
empty spot
for (int b = myCount; b < a; b--)
myList[b] = myList[b-1];
// now we have the room
myList[b] = bk;
myCount++; // and we need to increment here
}
2C)
BookList::InsertMany(const apvector<Book> & second) {
for (int a = 0; a < second.length(); a++)
InsertOne(second[a]);
}
3A)
void Environment::RemoveFish(const Position & pos) {
if (isEmpty(pos)) {
cerr << "error - precondition
not met at: " << pos << endl;
return;
}
Fish newFish;
myWorld[pos.row()][pos.col()] = newFish; // overwrite the
existing fish with a new one that will be undefined
// this way isEmpty() will return true
}
3B)
void Fish::Breed(Environment &env) {
// check the preconditions
if (env.isEmpty(myPos)) || (myAge > 0)) // they
don't say how old the fish needs to be?!?!
return;
if (env.IsEmpty(myPos.North())
env.AddFish(myPos.North(), 0, myProbDie);
if (env.IsEmpty(myPos.South())
env.AddFish(myPos.South(), 0, myProbDie);
if (env.IsEmpty(myPos.West())
env.AddFish(myPos.West(), 0, myProbDie);
if (env.IsEmpty(myPos.East())
env.AddFish(myPos.East(), 0,myProbDie);
// given there's no built in functions for
diagonals, let's assume they don't care
}
3C)
void Fish::Act(Environment &env) {
Randgen r;
if (env.isEmpty(myPos)) return; // check the
precondition
if (r.RandReal() < myProbDie) {// am I dead yet?
env.RemoveFish(myPos);
return;
}
myAge++;
if (myAge == 3) Breed(env);
else
Move(env);
}
4A)
bool Window::IsInBounds(int row, int col) {
return ((row < myNumRows && col <
myNumCols) && (row >= 0 && col >= 0));
}
4B)
void Window::ColorSquare(int ULrow, int ULcol, int N, int val) {
for (int x = ULrow, x < ULrow + N)
for (int y = ULcol; y < ULcol + N)
if (IsInBounds(x,y))
myMat[x][y] = val;
}
4C)
void Enlarge(Window &W, const Rectangle &rect, int factor) {
// it's important to enlarge the furthest away elements first...
so reverse the order of the last method
if (factor <= 0) return;
for (int x = rec.numRows-1; x >= 0; x--)
for (int y = rec.numCols-1; y >=0;
y--)
W.ColorSquare(rec.ULrow + x * factor, rec.ULcol + y * factor, factor,
ValAt(rec.ULrow+x, rec.ULcol+y));
}