-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
155 lines (137 loc) · 4.2 KB
/
main.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// main.cpp
// LLTemplate
//
// Created by James Shockey on 12/6/16.
// Copyright © 2016 James Shockey. All rights reserved.
//
/*
*
* Linked List lab.
* - Build a library for singly linked list.
* - Replace the airport array in main with a Linked List.
* - sort the array.
*
*/
#include "slist.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;
class Airport {
public:
char code[5];
double longitude;
double latitude;
};
double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d);
int invalidAirports(linkedList *list, int length);
void simpleSortTotal(linkedList *list, int length);
int main() {
ifstream infile;
int i = 0;
linkedList *airportList = new linkedList();
int airportCount;
Airport *a;
infile.open("airport-codes_US.csv", ifstream::in);
if (infile.is_open()) {
int c = 0;
char pH2[256];
infile.getline(pH2, 256, '\n');
while (infile.good()) {
a = new Airport();
char pH[100];
char cNum[10];
infile.getline(a->code, 256, ',');
infile.getline(pH, 256, ',');
infile.getline(pH, 256, ',');
infile.getline(cNum, 256, ',');
a->longitude = atof(cNum);
infile.getline(cNum, 256, ',');
a->latitude = atof(cNum);
infile.getline(pH, 256, '\n');
// cout << a->code << " long: " << a->longitude << " lat: " << a->latitude
// << endl;
airportList->add(a);
/*
if (!(c % 1000))
{
cout << airportArr[c]->code << " long: " << airportArr[c]->longitude
<< " lat: " << airportArr[c]->latitude << endl; cout <<
airportArr[c+1]->code << endl; //" long: " << airportArr[c+1]->longitude
<< " lat: " << airportArr[c+1]->latitude << endl;
}
*/
a = nullptr;
c++;
}
infile.close();
airportCount = airportList->size();
simpleSortTotal(airportList, airportCount);
double austinLong = -97.6655;
double austinLat = 30.2027;
for (int c = 0; c < 300; c++) {
cout << c << endl;
Airport *a = ((Airport *)airportList->get(c));
cout << a->code << " Distance: "
<< (distanceEarth(a->latitude, a->longitude, austinLat,
austinLong)) /
1.60934
<< " miles" << endl
<< endl
<< endl;
}
} else {
cout << "Error opening file";
}
}
#define pi 3.14159265358979323846
#define earthRadiusKm 6371.0
// This function converts decimal degrees to radians
double deg2rad(double deg) { return (deg * pi / 180); }
// This function converts radians to decimal degrees
double rad2deg(double rad) { return (rad * 180 / pi); }
/**
* Returns the distance between two points on the Earth.
* Direct translation from http://en.wikipedia.org/wiki/Haversine_formula
* @param lat1d Latitude of the first point in degrees
* @param lon1d Longitude of the first point in degrees
* @param lat2d Latitude of the second point in degrees
* @param lon2d Longitude of the second point in degrees
* @return The distance between the two points in kilometers
*/
double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = deg2rad(lat1d);
lon1r = deg2rad(lon1d);
lat2r = deg2rad(lat2d);
lon2r = deg2rad(lon2d);
u = sin((lat2r - lat1r) / 2);
v = sin((lon2r - lon1r) / 2);
return 2.0 * earthRadiusKm *
asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}
/*
Provide sort routine on linked list
*/
void simpleSortTotal(linkedList *list, int length) {
double austinLong = -97.66989899;
double austinLat = 30.19449997;
for (int i = 0; i < length; i++) {
Node *a = list->getNode(0);
Node *b = list->getNode(1);
for (int j = 0; j < length - 1; j++) {
Airport *data1 = (Airport *)a->data;
Airport *data2 = (Airport *)b->data;
if (distanceEarth(data1->latitude, data1->longitude, austinLat,
austinLong) > distanceEarth(data2->latitude,
data2->longitude, austinLat,
austinLong)) {
list->swap(a, b);
}
a = a->next;
b = b->next;
}
}
}