-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdefibrillateurs.cpp
114 lines (97 loc) · 2.26 KB
/
defibrillateurs.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
// Read inputs from stdin. Write outputs to stdout.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <sstream>
using namespace std;
class Point
{
public:
double latitude;
double longitude;
Point()
{
latitude = longitude = 0;
}
Point(string p_lat, string p_lon)
{
// replace ',' by '.' to match floats
int pos = p_lat.find(',');
p_lat[pos] = '.';
pos = p_lon.find(',');
p_lon[pos] = '.';
// convert strings to double
latitude = stod(p_lat);
longitude = stod(p_lon);
}
static double distance(const Point& a, const Point& b);
};
double Point::distance(const Point& a, const Point& b)
{
double x = (b.longitude - a.longitude) * cos((a.latitude + b.latitude) / 2);
double y = b.latitude - a.latitude;
double d = sqrt(pow(x,2) + pow(y,2)) * 6371;
return d;
}
class Defibrilateur
{
public:
Defibrilateur(string descr);
string id, name, address, tel;
Point loc;
};
Defibrilateur::Defibrilateur(string descr)
{
// parse the ';' separted string 'descr' into
// a vector of fields
vector<string> fields;
stringstream ss(descr);
string field;
while (getline(ss, field, ';')) {
fields.push_back(field);
}
// update instance variables
this->id = fields[0];
this->name = fields[1];
this->address = fields[2];
this->tel = fields[3];
string longitude = fields[4];
string latitude = fields[5];
Point p(latitude, longitude);
this->loc = p;
}
int main()
{
// get the user loc
string u_long, u_lat;
getline(cin, u_long);
getline(cin, u_lat);
Point user(u_lat, u_long);
// get the nb of defibrillators
string N_str;
getline(cin, N_str);
int N = stoi(N_str);
// build the list of defibrillators
vector<Defibrilateur*> d;
for (int i = 0; i < N; i++) {
string descr; // ';' separated string
getline(cin, descr);
d.push_back(new Defibrilateur(descr));
}
// find the nearest defibrillator and store its address
double min = -1;
string addr;
for (int i = 0; i < N; i++) {
double dist = Point::distance(user, d[i]->loc);
if((min == -1) or (dist < min)) {
min = dist;
addr = d[i]->name;
}
}
cout << addr << endl;
// clean
for (int i = 0; i < N; i++)
delete d[i];
return 0;
}