Question
C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in
C++ Programming
Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in case they're needed for understanding. I was able to complete the mayday.cpp file but am stuck on Facility. Not sure if Runway.cpp is required, I am still working on that one.The following link contains a tar file with the files provided by the professor. Thank you so much in advanced!
http://web.cs.ucdavis.edu/~fgygi/ecs40/homework/hw4/
The member functions of the Facility class are defined as follows:
Facility(string s) The constructor takes a single string argument. The argument s contains a full line read from the Facilities.txt file. The constructor should initialize the data members of Facility by selecting the appropriate substrings from the argument. The latitude and longitude fields should be converted to double values using the convert_latitude and convert_longitude member functions. The sign of the latitude_ and longitude_
data members should be determined by checking whether the latitude and longitude fields end with N or S, and E or W respectively.
string site_number(void) const
This function returns the facilitys site number.
string type(void) const
This function returns the facilitys type.
string code(void) const
This function returns the facilitys code.
string name(void) const
This function returns the facilitys name.
double latitude(void) const
This function returns the latitude of the facility in degrees decimal. Latitudes in the southern hemisphere are negative numbers.
double longitude(void) const
This function returns the longitude of the facility in degrees decimal. Longitudes in the western hemisphere are negative numbers.
double distance(double lat, double lon) const
This function returns the distance in nautical miles between the facility and the position defined by (lat,lon) in degrees decimal. The implementation of this function uses the gcdistance function provided in files gcdistance.h and gcdistance.cpp .
double convert_latitude(string s) const This function converts the string s representing a latitude in seconds decimal to a double value in degrees decimal. One degree is 3600 seconds. The sign of the result is positive if the string s ends with N and negative if it ends with S . For example, the latitude represented by the string 135427.7000N should be converted to the value 37.6188
double convert_longitude(string s) const This function converts the string s representing a longitude in seconds decimal to a double value in degrees decimal. One degree is 3600 seconds. The sign of the result is positive if the string s ends with E and negative if it ends with W . For example, the longitude represented by the string 440551.5000W should be converted to the value -122.3754 .
Closer.h:
#ifndef CLOSER_H
#define CLOSER_H
#include
#include "gcdistance.h"
struct Closer {
const double latitude, longitude;
Closer(const double ¤t_latitude, const double ¤t_longitude)
: latitude(current_latitude), longitude(current_longitude) {}
bool operator()(const Facility* a, const Facility* b) {
return (gcdistance(a->latitude(), a->longitude(), latitude, longitude)
latitude(), b->longitude(), latitude, longitude));
}
};
#endif
mayday.cpp
//
// mayday.cpp
//
// use: mayday latitude longitude min_length
//
// Provide a list of facilities and runways closest to the location specified
// by the coordinates (latitude,longitude).
// Only facilities having runways longer than min_length are printed.
// Only runways longer than min_length are printed.
//
// Input:
// latitude, longitude in degrees decimal
// min_length in ft
// Output:
// list of nearest facilities and runways including distance in nautical miles
#include "Facility.h"
#include "Runway.h"
#include "gcdistance.h"
#include "Closer.h"
#include "SiteNumber.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define M_PI 3.14159265358979323846
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);
}
double Facility::convert_latitude(string s) const {
bool doNegative = false;
if(s[s.length() - 1] == 'S') {
doNegative = true;
}
s = s.substr(0, s.length() - 1);
double temp = atof(s.c_str())/3600.00;
return (doNegative ? (-1 * temp) : temp);
}
double Facility::convert_longitude(string s) const {
bool doNegative = false;
if(s[s.length() - 1] == 'W') {
doNegative = true;
}
s = s.substr(0, s.length() - 1);
double temp = atof(s.c_str())/3600.00;
return (doNegative ? (-1 * temp) : temp);
}
Facility::Facility(string s) {
if(s.length() != 0) {
site_number_ = s.substr(0, 11);
type_ = s.substr(11, 13);
code_ = s.substr(24, 4);
name_ = s.substr(130, 50);
latitude_ = convert_latitude(s.substr(535, 12));
longitude_ = convert_longitude(s.substr(562, 12));
}
}
string Facility::site_number(void) const {
return site_number_;
}
string Facility::type(void) const {
return type_;
}
string Facility::code(void) const {
return code_;
}
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(lat, lon, latitude(), longitude());
}
int Runway::convert_length(string s) const {
return atoi(s.c_str());
}
Runway::Runway(string s) {
if(s.length() != 0) {
site_number_ = s.substr(0, 11);
name_ = s.substr(13, 7);
length_ = convert_length(s.substr(20, 5));
}
}
string Runway::site_number(void) const {
return site_number_;
}
string Runway::name(void) const {
return name_;
}
int Runway::length(void) const {
return length_;
}
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]);
string line;
vector
// load facilities data
ifstream inputFile;
inputFile.open("Facilities.txt");
if(!inputFile) {
cout
exit(0);
}
line = "";
while(getline(inputFile, line)) {
if(line.length() != 0) {
Facility* tempFacility = new Facility(line);
facilities.push_back(tempFacility);
}
}
inputFile.close();
// sort facilities in order of proximity to the current position
sort(facilities.begin(), facilities.end(),
Closer(current_latitude,current_longitude));
vector
// load runways data
inputFile.open("Runways.txt");
if(!inputFile) {
cout
exit(0);
}
line = "";
while(getline(inputFile, line)) {
if(line.length() != 0) {
Runway* tempRunway = new Runway(line);
runways.push_back(tempRunway);
}
}
inputFile.close();
// 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
Facility *a = facilities[i];
// Find all runways of this facility that are long enough
vector
vector
for(unsigned int j = 0; j
if(temp_good_runways[j]->length() >= min_runway_length) {
good_runways.push_back(temp_good_runways[j]);
}
}
// print this facility if it has long enough runways
if ( !good_runways.empty() ) {
// increment count of good facilities
count++;
cout type() code()
name()
cout.setf(ios_base::fixed,ios_base::floatfield);
cout.setf(ios_base::right, ios_base::adjustfield);
cout.width(5);
cout.precision(1);
cout distance(current_latitude,current_longitude)
// print all runways that are long enough
for ( unsigned int i = 0; i
Runway *r = good_runways[i];
cout name() length()
}
}
}
}
SiteNumber.h
#ifndef SITE_NUMBER_H
#define SITE_NUMBER_H
#include
#include
std::vector
std::vector
for(int i = 0; i
if(runways[i]->site_number() == s) {
good_runways.push_back(runways[i]);
}
}
return good_runways;
}
#endif
testFacility.cpp
#include "Facility.h"
#include "gcdistance.h"
#include "gcdistance.cpp"
#include
#include
#include
#include
#include
using namespace std;
double Facility::convert_latitude(string s) const {
bool doNegative = false;
if(s[s.length() - 1] == 'S') {
doNegative = true;
}
s = s.substr(0, s.length() - 1);
double temp = atof(s.c_str())/3600.00;
return (doNegative ? (-1 * temp) : temp);
}
double Facility::convert_longitude(string s) const {
bool doNegative = false;
if(s[s.length() - 1] == 'W') {
doNegative = true;
}
s = s.substr(0, s.length() - 1);
double temp = atof(s.c_str())/3600.00;
return (doNegative ? (-1 * temp) : temp);
}
Facility::Facility(string s) {
if(s.length() != 0) {
site_number_ = s.substr(0, 11);
type_ = s.substr(11, 13);
code_ = s.substr(24, 4);
name_ = s.substr(130, 50);
latitude_ = convert_latitude(s.substr(535, 12));
longitude_ = convert_longitude(s.substr(562, 12));
}
}
string Facility::site_number(void) const{
return site_number_;
}
string Facility::type(void) const{
return type_;
}
string Facility::code(void) const{
return code_;
}
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(lat, lon, latitude(), longitude());
}
int main() {
string line;
ifstream inputFile;
// Change the file name to test various cases
inputFile.open("testFacility1.in");
getline(inputFile, line);
Facility f(line);
cout
cout
cout
}
testRunway.cpp
#include "Runway.h"
#include
#include
#include
#include
using namespace std;
int Runway::convert_length(string s) const {
return atoi(s.c_str());
}
Runway::Runway(string s) {
if(s.length() != 0) {
site_number_ = s.substr(0, 11);
name_ = s.substr(13, 7);
length_ = convert_length(s.substr(20, 5));
}
}
string Runway::site_number(void) const {
return site_number_;
}
string Runway::name(void) const {
return name_;
}
int Runway::length(void) const {
return length_;
}
int main() {
string line;
ifstream inputFile;
// Change the file name to test various cases
inputFile.open("testRunway1.in");
getline(inputFile, line);
Runway r(line);
cout
}
Emergency landing application Description In this assignment (HW4), you will implement a program called mayday that prints alist of airports and runways that are closest to a given location. The program will list airports that have runways longer than a given minimum length. It will also print the distance from the given location to the airport. In a fictional scenario, a pilot who encounters an emergency situation would use your program to get a list of the nearest landing options. The pilot would enter hisher current location as latitude and longitude (in degrees decimal as well as the minimum runway length needed to land (in ft). This assignment will test your C++proficiency in opening and reading text files, using strings, using STL containers, using STL algorithms, and implementing function objects. Program The program will read data from two text files containing information about Federal Aviation Administration (FAA facilities and runways. The files Facilities txt and Runways .txt are provided and contain a list of 19700 facilities (such as airports, heliports, seaplane bases) and 23595 nunways located mostly in the United States, but also in remote locations Each line in the Facilities txt file contains the description of a facility (airport, heliport or seaplane base), The first part of the line contains the site number, a 10-character string that uniquely identifies the facility. The rest ofthe line contains other information, including the facility type, name, code, and position (latitude and longitude in various formats). For example, San Francisco Intemational Airport has site number 02187. A type AIRPORT code SFO. name SAN FRANCISCO INTL, latitude 135427.700ON and longitude 440551. 5000W (expressed in seconds deci The positions of these fields in the line are: Site number: haracters haracters 12-24 Type: Code characters 25-28 Name: 80 Latitude (sec decimal characters 536-547 Longitude (see decimal characters 563-574 Other fields are not relevant to this assignment. Each line in the Runways. takt file contains the description of arunway. The first part ofthe line contains the site number ofthe facility it belongs to (i.e. the 10-character string described above). The rest of the line contains other information about the runway, including its name and length (in feet). For example, runway 10L28R of San Francisco International airport has site number 02187. A name 10L/28R. and a length of 11870 ft. The positions of these fields in the line are: Site number: characters
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started