1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/*City.h Begins here */
//city.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include<iostream>
#include <exception>
#include <stdexcept>
#include <string>
const double PI= 3.14159265358979;
const double earth_radius= 6371;
class City{
private:
std::string nm;
double latitude;
double longitude;
bool check_latitude(double lati );
bool check_longitude(double longi);
public:
// Constructors and Destructor
// Default constructor, sets latitude and longitude to 0
City();
// Constructor, sets height and width to parameters w and h
// Throws an invalid_argument exception if log or lat are <= 0
// The exception's string indicates the error condition
City(std::string nm,double lati, double longi);
// Destructor
~City();
// Getters and Setters
std::string getCityname(){return nm;};
double getLatitude(){return latitude;}; //inline method
double getLongitude(){return longitude;}; //inline method
// The setter methods throw an invalid_argument exception
// if log or lat are <= 0, the exception's string indicates the
// error condition
// Sets latitude to parameter lat
void setLatitude(double lati);
// Sets longitude to parameter log
void setLongitude(double longi);
// set the city nametion
void setCityname(std::string nm);
double distance(const City & c) const;
friend std::ostream& operator<<(std::ostream& os, const City& c));
};
/*City.h Ends here */
/*City.cpp Begins here */
// city.cpp : Defines the entry point for the console application.
//
#include"city.h"
#include<iostream>
#include<cmath>
// constructors
City::City() : nm(""), latitude(0.0), longitude(0.0) {}
City(std::string nm,double lati, double longi){
setCityname(nm);
setLatitude(lati);
setLongitude(longi);
}
bool City::check_latitude(double lati)
{/* Range of latitude is [0,90]*/
return lati >= 0 && lati<=90;
}
bool City::check_longitude(double longi)
{ /* Range of longitude is [0,180]*/
return longi >=0 && longi <= 180;
}
//Setters
void City::setLatitude(double lati);
{
if(! check_latitude(lati)){
throw invalid_argument("latitude value out of bounds");
}
latitude=lati;
}
void City::setLongitude(double longi);
{
if(! check_longitude(longi)){
throw invalid_argument("longitude value out of bounds");
}
longitude= longi;
}
void City::setCityname(std::string nm);
{
this->nm = nm;
}
double City::distance(const City & c) const
{
double lati1 = this->latitude * (PI / 180);
double lati2 = c.laltitude * (PI/180);
double longi1 = this->longitude * (PI/180);
double longi2 = c.longitude * (PI/180);
double dist = acos( sin(lati1) * sin(lati2) + cos(lati1) * cos(lati2) * cos(longi2−longi1) ) * earth_radius;
return dist;
}
std::ostream& operator<<(std::ostream& os, const City& c){
os <<c.nm<<" " << c.latitude << " " << c.longitude<<endl; //
return os;
}
/*City.cpp Ends here
*/
| |