/** * Aquarium Lab Series * Copyright (C) 2000 Alyce Brady * * Implementation file for IntToString and SortByDepth -- see utils.h * for details. * * Acknowledgements: * This file is part of the Aquarium Lab Series, which was inspired * by the AP Marine Biology Case Study found at * http://www.collegeboard.org/ap/computer-science/marine_biology/ * The IntToString function is the same as that found in utils.cpp * in Part II of the Case Study. The SortByDepth function is * similar to the sort function found there. * * License: * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include "utils.h" apstring IntToString(int i) // postcondition: returns stringized form of i { if (i == 0) return "0"; // special case for 0 int k; apstring reverse = ""; // will be correct, but in reverse apstring val = ""; // the apstring returned if (i < 0) // start with "-" if i < 0 { val = "-"; i = -i; } while (i > 0) // get each digit, catenate in reverse { reverse += char('0' + i%10); i /= 10; } // now build the apstring to return by "unreversing" for (k = reverse.length() - 1; k >= 0; k--) { val += reverse[k]; } return val; } void SortByDepth(apvector & list) // precondition: list has no excess capacity // (i.e., list contains list.length() fish to be sorted) // postcondition: list sorted so that entries are // in order top-down/left-right by X and Y Coordinates { // use selection sort int j, k, minIndex; int nbrFish = list.length(); AquaFish min; AquaFish current; for (j = 0; j < nbrFish - 1; j++) { minIndex = j; min = list[j]; for (k = j + 1; k < nbrFish; k++) { current = list[k]; if (current.YCoord() < min.YCoord() || (min.YCoord() == current.YCoord() && current.XCoord() < min.XCoord())) { min = current; minIndex = k; } } AquaFish temp = list[minIndex]; list[minIndex] = list[j]; list[j] = temp; } } // Indicates level of detail at which we want debugging information. // 1 => fish moves only // 3 => neighborhood contents + output for 1 // 5 => neighborhood element selection + positions added and not added // to the neighborhood + myFish vector + output for 3 int LEVEL_OF_DEBUG_DETAIL = 0; // The given msg is to be printed if level (which is positive) is less than // or equal to LEVEL_OF_BUG_DETAIL, the level of detail at which we want to // see debugging info. Indent the printed msg 2 spaces for each level of // detail. void DebugPrint(int level, apstring msg) { int k; if (level <= LEVEL_OF_DEBUG_DETAIL) { for (k = 0; k < level; k++) { cout << ' '; } cout << "**** " << msg << endl; } }