#ifndef CLASS_PLACE_ON_EARTH_H // prohibit multiple inclusions #define CLASS_PLACE_ON_EARTH_H // class_place_on_earth.h (c) Kari Laitinen // 20.11.2000 File created. // 06.03.2001 Modification. // 2013-11-15 Last modification. #include #include "class_text.h" #define EARTH_RADIUS_IN_KILOMETERS 6370 void calculate_distance_on_earth_surface( double point_1_latitude, double point_1_longitude, double point_2_latitude, double point_2_longitude, long& distance_to_caller ) { // All latitudes and longitudes must be expressed // in radians when calling this function. double cosine_of_angle_between_points = cos( point_1_latitude ) * cos( point_2_latitude ) * cos( point_1_longitude - point_2_longitude ) + sin( point_1_latitude ) * sin( point_2_latitude ) ; double angle_between_points = acos( cosine_of_angle_between_points ) ; distance_to_caller = angle_between_points * EARTH_RADIUS_IN_KILOMETERS ; } // The following function takes arguments as degrees and minutes, // converts them to radians, and then calls the function above. void calculate_distance_on_earth_surface( int point_1_latitude_degrees, int point_1_latitude_minutes, int point_1_longitude_degrees, int point_1_longitude_minutes, int point_2_latitude_degrees, int point_2_latitude_minutes, int point_2_longitude_degrees, int point_2_longitude_minutes, long& distance_to_caller ) { calculate_distance_on_earth_surface( degrees_to_radians( point_1_latitude_degrees, point_1_latitude_minutes ), degrees_to_radians( point_1_longitude_degrees, point_1_longitude_minutes ), degrees_to_radians( point_2_latitude_degrees, point_2_latitude_minutes ), degrees_to_radians( point_2_longitude_degrees, point_2_longitude_minutes ), distance_to_caller ) ; } class Place_on_earth { private: string place_name ; string country_name ; int degrees_of_latitude ; int minutes_of_latitude ; int degrees_of_longitude ; int minutes_of_longitude ; Text travel_info ; public: Place_on_earth( char given_place_name[] = "", char given_country_name[] = "", int given_degrees_latitude = 0, int given_minutes_latitude = 0, int given_degrees_longitude = 0, int given_minutes_longitude = 0 ) { place_name = given_place_name ; country_name = given_country_name ; degrees_of_latitude = given_degrees_latitude ; minutes_of_latitude = given_minutes_latitude ; degrees_of_longitude = given_degrees_longitude ; minutes_of_longitude = given_minutes_longitude ; } string get_place_name() { return place_name ; } string get_country_name() { return country_name ; } string get_coordinates_as_string() ; bool place_name_contains_string( string& given_string ) ; bool country_name_contains_string( string& given_string ) ; bool is_in_northern_hemisphere() ; long get_distance_from_equator() ; long get_distance_from_north_pole() ; long get_distance_from_south_pole() ; void get_distance_to( Place_on_earth& another_place, long& distance_to_caller ) ; void set_travel_info( char travel_info_lines[] ) ; void set_travel_info( Text& new_travel_info ) ; void print_short_place_info() ; void print_long_place_info() ; void get_short_place_info( string& short_place_info ) ; void get_long_place_info( string& long_place_info ) ; void store_to_binary_file( ostream& output_file ) ; void load_from_binary_file( istream& input_file ) ; } ; string Place_on_earth::get_coordinates_as_string() { stringstream coordinates_as_stream ; string coordinates_as_string ; coordinates_as_stream << setfill( '0' ) ; if ( ( degrees_of_latitude > 0 ) || ( degrees_of_latitude == 0 && minutes_of_latitude >= 0 ) ) { coordinates_as_stream << setw( 2 ) << degrees_of_latitude << " " << setw( 2 ) << minutes_of_latitude << " N " ; } else { coordinates_as_stream << setw( 2 ) << - degrees_of_latitude << " " << setw( 2 ) << - minutes_of_latitude << " S " ; } if ( ( degrees_of_longitude > 0 ) || ( degrees_of_longitude == 0 && minutes_of_longitude >= 0 )) { coordinates_as_stream << setw( 3 ) << degrees_of_longitude << " " << setw( 2 ) << minutes_of_longitude << " E" ; } else { coordinates_as_stream << setw( 3 ) << - degrees_of_longitude << " " << setw( 2 ) << - minutes_of_longitude << " W" ; } getline( coordinates_as_stream, coordinates_as_string ) ; return coordinates_as_string ; } bool Place_on_earth::place_name_contains_string( string& given_string ) { if ( place_name.find( given_string ) != string::npos ) { return true ; } else { return false ; } } bool Place_on_earth::country_name_contains_string( string& given_string ) { if ( country_name.find( given_string ) != string::npos ) { return true ; } else { return false ; } } bool Place_on_earth::is_in_northern_hemisphere() { return ( ( degrees_of_latitude > 0 ) || ( degrees_of_latitude == 0 && minutes_of_latitude >= 0 ) ) ; } long Place_on_earth::get_distance_from_equator() { Place_on_earth closest_place_on_equator( "place on equator", "somewhere on equator", 0, 0, degrees_of_longitude, minutes_of_longitude ) ; long distance_from_equator ; get_distance_to( closest_place_on_equator, distance_from_equator ) ; return distance_from_equator ; } long Place_on_earth::get_distance_from_north_pole() { Place_on_earth north_pole( "north pole", "no land there", 90, 0, 0, 0 ) ; long distance_from_north_pole ; get_distance_to( north_pole, distance_from_north_pole ) ; return distance_from_north_pole ; } long Place_on_earth::get_distance_from_south_pole() { Place_on_earth south_pole( "south pole", "Antarctic", -90, 0, 0, 0 ) ; long distance_from_south_pole ; get_distance_to( south_pole, distance_from_south_pole ) ; return distance_from_south_pole ; } void Place_on_earth::get_distance_to( Place_on_earth& another_place, long& distance_to_caller ) { calculate_distance_on_earth_surface( degrees_of_latitude, minutes_of_latitude, degrees_of_longitude, minutes_of_longitude, another_place.degrees_of_latitude, another_place.minutes_of_latitude, another_place.degrees_of_longitude, another_place.minutes_of_longitude, distance_to_caller ) ; } void Place_on_earth::set_travel_info( char travel_info_lines[] ) { travel_info.delete_all_lines() ; travel_info.append_lines( travel_info_lines ) ; } void Place_on_earth::set_travel_info( Text& new_travel_info ) { travel_info = new_travel_info ; } void Place_on_earth::print_short_place_info() { cout << "\n " << setiosflags( ios::left ) << setw(20) << place_name << setw(20) << country_name << setw(20) << get_coordinates_as_string() ; cout << "\n" ; } void Place_on_earth::print_long_place_info() { cout << "\n\n " << place_name << " " << country_name << "\n\n " << get_coordinates_as_string() ; cout << "\n Equator: " << get_distance_from_equator() << " km North pole: " << get_distance_from_north_pole() << " km South pole: " << get_distance_from_south_pole() << " km " ; cout << "\n" ; travel_info.print_with_indentation( cout ) ; } void Place_on_earth::get_short_place_info( string& short_place_info ) { stringstream short_place_info_as_stream ; short_place_info_as_stream << setiosflags( ios::left ) << setw(20) << place_name << setw(20) << country_name << setw(20) << get_coordinates_as_string() ; getline( short_place_info_as_stream, short_place_info ) ; short_place_info = "\n " + short_place_info + "\n" ; } void Place_on_earth::get_long_place_info( string& long_place_info ) { stringstream long_place_info_as_stream ; long_place_info_as_stream << "\n\n " << place_name << " " << country_name << "\n\n " << get_coordinates_as_string() << "\n Equator: " << get_distance_from_equator() << " km North pole: " << get_distance_from_north_pole() << " km South pole: " << get_distance_from_south_pole() << " km " << "\n" ; travel_info.print_with_indentation( long_place_info_as_stream ) ; stringstream_to_string( long_place_info_as_stream, long_place_info ) ; } void Place_on_earth::store_to_binary_file( ostream& output_file ) { int place_name_length = place_name.length() ; output_file.write( (char*) &place_name_length, sizeof( int ) ) ; output_file.write( place_name.c_str(), place_name_length ) ; int country_name_length = country_name.length() ; output_file.write( (char*) &country_name_length, sizeof( int ) ) ; output_file.write( country_name.c_str(), country_name_length ) ; output_file.write( (char*) °rees_of_latitude, sizeof(int) ) ; output_file.write( (char*) &minutes_of_latitude, sizeof(int) ) ; output_file.write( (char*) °rees_of_longitude, sizeof(int) ) ; output_file.write( (char*) &minutes_of_longitude, sizeof(int) ) ; travel_info.store_to_binary_file( output_file ) ; } void Place_on_earth::load_from_binary_file( istream& input_file ) { int place_name_length ; input_file.read( (char*) &place_name_length, sizeof(int) ) ; char* place_name_characters = new char[ place_name_length + 2 ] ; input_file.read( place_name_characters, place_name_length ) ; place_name_characters[ place_name_length ] = 0 ; place_name = place_name_characters ; delete [] place_name_characters ; int country_name_length ; input_file.read( (char*) &country_name_length, sizeof(int) ) ; char* country_name_characters = new char[ country_name_length + 2 ] ; input_file.read( country_name_characters, country_name_length ) ; country_name_characters[ country_name_length ] = 0 ; country_name = country_name_characters ; delete [] country_name_characters ; input_file.read( (char*) °rees_of_latitude, sizeof(int) ) ; input_file.read( (char*) &minutes_of_latitude, sizeof(int) ) ; input_file.read( (char*) °rees_of_longitude, sizeof(int) ) ; input_file.read( (char*) &minutes_of_longitude, sizeof(int) ) ; travel_info.load_from_binary_file( input_file ) ; } #endif