Implement c++ program distance.cpp The distance program prints the distance betw
ID: 3905747 • Letter: I
Question
Implement c++ program distance.cpp
The distance program prints the distance between two airports. The airport codes are read from the command line arguments. The distance is printed in nautical miles, rounded to an integer. See the example output files for the output format.
Notes:
• The airport codes are entered as command line arguments and may consist of 3 or 4
characters. The program should be able to process both cases correctly.
If any of the airports entered is not found (e.g. ABCD), the program should print ABCD not found
and exit.
If any of the airport codes given is longer than 4 characters, the program should print Airport code must be at most 4 characters
and exit.
Example of input--
#!/bin/bash
./distance 24MD 21NJ
Example of output--
24MD - 21NJ 153 NM
just in case its helpful the folowing files are provided.
//
// gcdistance.h
//
#ifndef GCDISTANCE_H
#define GCDISTANCE_H
double gcdistance(double lat1, double lon1, double lat2, double lon2);
#endif
//
// gcdistance.cpp
//
// great circle distance between points (lat1,lon1) and (lat2,lon2)
// input in degrees decimal
// output in nautical miles (NM)
#include "gcdistance.h"
#include <cmath>
double gcdistance(double lat1, double lon1, double lat2, double lon2)
{
// convert latitudes and longitudes from degrees to radians
const double lat1r = lat1 * (M_PI/180.0);
const double lon1r = lon1 * (M_PI/180.0);
const double lat2r = lat2 * (M_PI/180.0);
const double lon2r = lon2 * (M_PI/180.0);
// psi = central angle between the points (lat1,lon1) and
// (lat2,lon2) on a sphere
double c = cos(lat1r)*cos(lat2r)*cos(lon1r-lon2r) + sin(lat1r)*sin(lat2r);
// truncate possible numerical errors in cos to [-1,1] interval
c = fmin(c,1.0);
c = fmax(c,-1.0);
const double psi = acos(c);
// R_Earth = 6371 km
// 1 NM = 1.852 km
// 1 degree = 60.0405 NM on a great circle
return 60.0405 * psi * (180.0/M_PI);
}
Explanation / Answer
//testRunway.cpp
#include "Runway.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string line;
getline(cin,line);
Runway r(line);
cout << r.site_number() << " " << r.name() << " "
<< setw(8) << r.length() << endl;
}
---------------------------------------------------------------------
//testFacility.cpp
#include "Facility.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
string line;
getline(cin,line);
Facility f(line);
cout << f.site_number() << " " << f.type() << " " << f.code() << " "
<< f.name() << " ";
cout << setw(12) << setprecision(4) << fixed << f.latitude() << " "
<< setw(12) << setprecision(4) << fixed << f.longitude() << " ";
cout << f.distance(40,-100) << endl;
}
----------------------------------------------------------------------------------------------
//mayday.cpp
#include "Facility.h"
#include "Runway.h"
#include "Closer.h"
#include "SiteNumber.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
using namespace std;
int main(int argc, char **argv)
{
// use: mayday current_latitude current_longitude min_runway_length
// latitude and longitudes in degrees decimal
// min runway length of runway in ft
assert(argc==4);
const double current_latitude = atof(argv[1]);
const double current_longitude = atof(argv[2]);
const int min_runway_length = atoi(argv[3]);
vector<Facility*> facilities;
// load facilities data
// Insert your code here
ifstream facilityfile("Facilities.txt");
string line1;
while(getline(facilityfile,line1))
{
Facility* ff = new Facility(line1);
facilities.push_back(ff);
}
// sort facilities in order of proximity to the current position
sort(facilities.begin(), facilities.end(),
Closer(current_latitude,current_longitude));
vector<Runway*> runways;
// load runways data
// Insert your code here
ifstream runwayfile("Runways.txt");
string line2;
while(getline(runwayfile,line2))
{
Runway* rf = new Runway(line2);
runways.push_back(rf);
}
// list up to 10 nearest facilities that have a long enough runway
// list each runway that is long enough
int count = 0;
for ( unsigned int i = 0; i < facilities.size() && count < 10; i++ )
{
Facility *a = facilities[i];
// Find all runways of this facility that are long enough
vector<Runway*> good_runways;
// Insert your code here
for(vector<Runway*>::iterator it = find_if(runways.begin(),runways.end(),SiteNumber(a->site_number())); it != runways.end(); it = find_if(++it,runways.end(),SiteNumber(a->site_number())))
{
if((*it)->length() >= min_runway_length) good_runways.push_back(*it);
}
// print this facility if it has long enough runways
if ( !good_runways.empty() )
{
// increment count of good facilities
count++;
cout << a->type() << " " << a->code() << " "
<< a->name() << " ";
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::right, ios_base::adjustfield);
cout.width(5);
cout.precision(1);
cout << a->distance(current_latitude,current_longitude)
<< " NM" << endl;
// print all runways that are long enough
for ( unsigned int i = 0; i < good_runways.size(); i++ )
{
Runway *r = good_runways[i];
cout << " Runway: " << r->name() << " length: " << r->length()
<< " ft" << endl;
}
}
}
}
-----------------------------------------------------------------------------------------------
//gcdistance.cpp
#include "gcdistance.h"
#include <cmath>
double gcdistance(double lat1, double lon1, double lat2, double lon2)
{
// convert latitudes and longitudes from degrees to radians
const double lat1r = lat1 * (M_PI/180.0);
const double lon1r = lon1 * (M_PI/180.0);
const double lat2r = lat2 * (M_PI/180.0);
const double lon2r = lon2 * (M_PI/180.0);
// psi = central angle between the points (lat1,lon1) and
// (lat2,lon2) on a sphere
double c = cos(lat1r)*cos(lat2r)*cos(lon1r-lon2r) + sin(lat1r)*sin(lat2r);
// truncate possible numerical errors in cos to [-1,1] interval
c = fmin(c,1.0);
c = fmax(c,-1.0);
const double psi = acos(c);
// R_Earth = 6371 km
// 1 NM = 1.852 km
// 1 degree = 60.0405 NM on a great circle
return 60.0405 * psi * (180.0/M_PI);
}
--------------------------------------------------------------------------------------
//gcdistance.h
#ifndef GCDISTANCE_H
#define GCDISTANCE_H
double gcdistance(double lat1, double lon1, double lat2, double lon2);
#endif
-----------------------------------------------------------------------------------
//SiteNumber.h
#ifndef SITENUMBER_H
#define SITENUMBER_H
#include "Runway.h"
#include "Facility.h"
class SiteNumber
{
public:
SiteNumber(std::string site_number): snum(site_number) {}
bool operator() (Runway* r) { return r->site_number() == snum; }
private:
const std::string snum;
};
#endif
----------------------------------------------------------------------------
//RunWay.cpp
#include <iostream>
#include <sstream>
#include "Runway.h"
Runway::Runway(std::string s): site_number_(s.substr(0,10)), name_(s.substr(13,7)), length_(convert_length(s)) {}
std::string Runway::site_number(void) const
{
return site_number_;
}
std::string Runway::name(void) const
{
return name_;
}
int Runway::length(void) const
{
return length_;
}
int Runway::convert_length(std::string s) const
{
std::istringstream is(s.substr(20,5));
int len;
is >> len;
return len;
}
-------------------------------------------------------------------------
//Runway.h
#ifndef RUNWAY_H
#define RUNWAY_H
#include<string>
class Runway
{
public:
Runway(std::string s);
std::string site_number(void) const;
std::string name(void) const;
int length(void) const;
private:
int convert_length(std::string s) const;
const std::string site_number_;
const std::string name_;
const int length_;
};
#endif
------------------------------------------------------------------
//Facility.cpp
#include <iostream>
#include <string>
#include <sstream>
#include "Facility.h"
#include "gcdistance.h"
Facility::Facility(std::string s) : site_number_(s.substr(0,10)), type_(s.substr(11,13)), code_(s.substr(24,4)), name_(s.substr(130,50)), latitude_(Facility::convert_latitude(s)), longitude_(Facility::convert_longitude(s)) {}
std::string Facility::site_number(void) const
{
return site_number_;
}
std::string Facility::type(void) const
{
return type_;
}
std::string Facility::code(void) const
{
return code_;
}
std::string Facility::name(void) const
{
return name_;
}
double Facility::latitude(void) const
{
return latitude_;
}
double Facility::longitude(void) const
{
return longitude_;
}
double Facility::distance(double lat, double lon) const
{
return gcdistance(latitude_,longitude_,lat,lon);
}
double Facility::convert_latitude(std::string s) const
{
std::istringstream is1(s.substr(535,11));
double la;
is1 >> la;
if(s.substr(546,1) == "S") la *= -1;
return la/3600;
}
double Facility::convert_longitude(std::string s) const
{
std::istringstream is2(s.substr(562,11));
double lo;
char dir2;
is2 >> lo >> dir2;
if(s.substr(573,1) == "W") lo *= -1;
return lo/3600;
}
----------------------------------------------------------------------------------
//Facility.h
#ifndef FACILITY_H
#define FACILITY_H
#include<string>
class Facility
{
public:
Facility(std::string s);
std::string site_number(void) const;
std::string type(void) const;
std::string code(void) const;
std::string name(void) const;
double latitude(void) const;
double longitude(void) const;
double distance(double lat, double lon) const;
private:
const std::string site_number_;
const std::string type_;
const std::string code_;
const std::string name_;
const double latitude_, longitude_;
double convert_latitude(std::string s) const;
double convert_longitude(std::string s) const;
};
#endif
-------------------------------------------------------------------------------
//Closer.h
#ifndef CLOSER_H
#define CLOSER_H
#include "Facility.h"
class Closer
{
public:
Closer(double current_latitude, double current_longitude): lat(current_latitude), lon(current_longitude) {}
bool operator() (Facility* f1, Facility* f2) { return f1->distance(lat,lon) < f2->distance(lat,lon); }
private:
const double lat;
const double lon;
};
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.