copyright Steve J. Hodges   http://steveh.net/cs19/cs19-hw04.html

CS19 Spring 2017

Assignment 4 ( Paradise Ranch Pet Resort )

Directory Name

19-4

Executable resulting from make

p4

This program is an exercise in Linked Lists.

Create a class named Pet. This class should have a name (string,) a count of the number of treats received (int,) and a Coordinate as private members. Add a toString function that returns a string with the information included in the Pet. (See example for formatting details.) Add any additional member functions you deem necessary.

Create a doubly-linked linked list class named PetList. This list will hold Pet objects. The List must have a functions named rotate(int n) and treat(float lat, float lon, float dist). The Rotate function takes n Pets from the front of the list and moves them to the back of the list. (if n==0 or n>=size, rotate does nothing, If n is negative move -n entries from the back of the list to the front. Treat gives a single treat each Pet that is within dist distance of the provided coordinates. You are also required to have a printAll() function. This function will call toString on each Pet in the list, printing the result to STDOUT, one entry per line. Add additional member function that you deem necessary.

Create a main function that reads all input from STDIN (until EOF.) Each Line will be one of four commands "add", "rotate", "treat", or "print". A command may be input in any (mix of) case. The Rotate command will be followed by an integer, the add command will be followed by two floats (representing lat and lon coordinates) and then a string. Add a new Pet to the front of the list. The treat command will be followed by three floats. The print command doesn't have any parameters. After you input each line, call the appropriate command on a PetList. ("print" should result in a call to printAll.) See the input and output example for more details.

Coordinate (updated with copy constrcutor and toString)

class Coordinate{
 public:
  Coordinate(float lat=0.0f, float lon=0.0f){
    setLatitude(lat);
    setLongitude(lon);
  }
  Coordinate(Coordinate &source){
    setLatitude(source._lat);
    setLongitude(source._lon);
  }
  void setLatitude(float lat){
    _lat = (lat>=-90.0f&&lat<=90.0f)?lat:0.0f;
  }
  void setLongitude(float lon){
    _lon = (lon>=-180.0f&&lon<=180.0f)?lon:0.0f;
  }
  float longitude(){ return getLongitude(); }
  float latitude(){ return getLatitude(); }
  float getLongitude(){ return _lon; }
  float getLatitude(){ return _lat; }
  // calculate distance using approx Haversine formula
  // https://en.wikipedia.org/wiki/Haversine_formula
  // and
  // http://www.movable-type.co.uk/scripts/gis-faq-5.1.html
  // distance is returned in meters
  float distanceTo(Coordinate &target){
    const float PI = 3.14159265f;
    const float RADIUS = 6373000.0f; // ~Earth radius (meters)
    float distance = 0.0f;
    // convert degrees to radians
    float lon1 = _lon *PI/180.0f;
    float lat1 = _lat *PI/180.0f;
    float lon2 = target._lon *PI/180.0f;
    float lat2 = target._lat *PI/180.0f;

    float dlon = lon2 - lon1;
    float dlat = lat2 - lat1;
    float a = pow(sin(dlat/2.0f),2.0f)+cos(lat1)*cos(lat2)*pow(sin(dlon/2.0f),2.0f);
    float c = 2.0f * asin( std::min( 1.0f, sqrt(a)));
    distance = c * RADIUS;

    return distance;
  }
  
    std::string toString(){
    std::stringstream result;
    if (_lat>0.0) result <<"+";
    result << _lat;
    result << ", ";
    if (_lon>0.0) result <<"+";
    result << _lon;
    return result.str();
  }

 private:
  float _lat;
  float _lon;

};

Sample Run

[steveh@pengo PRPR]$ ./p4
add 2.0 2.0 Three
add 1.0 1.0 Two
add 0.0 0.0 One
print
One{0} 0, 0
Two{0} +1, +1
Three{0} +2, +2
rotate 1
print
Two{0} +1, +1
Three{0} +2, +2
One{0} 0, 0
rotate -1
print
One{0} 0, 0
Two{0} +1, +1
Three{0} +2, +2
rotate 2
print
Three{0} +2, +2
One{0} 0, 0
Two{0} +1, +1
rotate -2
print
One{0} 0, 0
Two{0} +1, +1
Three{0} +2, +2
rotate 3
print
One{0} 0, 0
Two{0} +1, +1
Three{0} +2, +2
rotate -3
print
One{0} 0, 0
Two{0} +1, +1
Three{0} +2, +2
[steveh@pengo PRPR]$ 

Another Example

Example Input
Matching Output

 

What to turn in

For this assignment, you will be creating a project with several files. All of your project files should be placed in a directory named 19-4 inside your home directory on Pengo. This directory should not contain any files not part of your project, nor should it contain any subdirectories. I will collect all of the files from this directory.