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. 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/
Closer.h:
#ifndef CLOSER_H
#define CLOSER_H
#include <string>
#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)
< gcdistance(b->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 <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
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<Facility*> facilities;
// load facilities data
ifstream inputFile;
inputFile.open("Facilities.txt");
if(!inputFile) {
cout << "Error while opening the file \"Facilities.txt\"." << endl;
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<Runway*> runways;
// load runways data
inputFile.open("Runways.txt");
if(!inputFile) {
cout << "Error while opening the file \"Runways.txt\"." << endl;
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 < facilities.size() && count < 10; i++ ) {
Facility *a = facilities[i];
// Find all runways of this facility that are long enough
vector<Runway*> good_runways;
vector<Runway*> temp_good_runways = SiteNumber(a->site_number(), runways);
for(unsigned int j = 0; j < temp_good_runways.size(); 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 << 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;
}
}
}
}
SiteNumber.h
#ifndef SITE_NUMBER_H
#define SITE_NUMBER_H
#include <string>
#include <vector>
std::vector<Runway*> SiteNumber(std::string s, std::vector<Runway*> runways) {
std::vector<Runway*> good_runways;
for(int i = 0; i < runways.size(); 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 <iostream>
#include <stdlib.h>
#include <fstream>
#include <iomanip>
#include <cmath>
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 << 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;
}
testRunway.cpp
#include "Runway.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
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 << r.site_number() << " " << r.name() << " "
<< setw(8) << r.length() << endl;
}
#include <iostream>
using namespace std;
int main()
{
int
x;
// A normal integer
int *p;
// A pointer to an integer
p =
&x;
// Read it, "assign the address of x to p"
cin>>
x; // Put a
value in x, we could also use *p here
cin.ignore();
cout<< *p <<"\n"; // Note the use of the * to get the
value
cin.get();
}
C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the...
The following C++ code contains an incomplete program that should be able to calculate the distance between a series of geocoded locations. Two parts of the program need to be completed: 1. The portion of the main() function that calculates the distances between each of the hard-coded 5 locations, and stores it in a two-dimensional array declared as distances. 2. A portion of the calculateDistanceBetweenLocations(l1, l2) function; omitted code spaces are designed with a // TODO: comment. The code is properly...
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...
Runway Runway(string s) The constructor takes a single string argument. The argument s contains a full line read from the Runways.txt file. The constructor should initialize the data members of Runway by selecting the appropriate substrings from the argument. string site_number(void) const This function returns the site number of the facility that the runway belongs to. string name(void) const This function returns the name of the runway. int length(void) const This function returns the length of the runway in ft....
Extend the code from Lab6.A template is given to you with CircleHeader.h, Circle.cpp and CircleMain.cpp Use the same Circle UML as below and make extensions as marked and as needed. Given below is a UML for the Painting Class. You will need to write PaintingHeader.h and Painting.cpp. The Painting Class UML contains a very basic skeleton. You will need to flesh out this UML and add more instance variables and methods as needed. You do NOT need to write a...
I wrote code in C++ that takes a text file containing information on different football players and their combine results, stores those players as a vector of objects of the class, and prints out those objects. Finally, I wrote a function that calculates the average weight of the players. MAIN.CPP: #include "Combine.h" #include using namespace std; // main is a global function // main is a global function int main() { // This is a line comment //cout << "Hello,...
Please Help. C++. Need 3 attributes and 3 methods added to the code below. Need the prototype added to the .h file......and the implementation in the .cpp file....thanks! edited: I just need the above and then I was told to use the methods in the main program; for the vehicle. Any 3 attributes and any three methofds. source.cpp file // Header Comment #include #include #include #include "Automobile.h" using namespace std; struct Address{ string street; string city; string state; string zip;...
I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...
Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME UNIFORM# AVEPPG Mary 15 10.50 Jack 72 8.10 Elizabeth 41 7.50 Joseph 32 6.20 Vince 83 4.20 My Code: #include <iostream>...
I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...