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
|
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
void read_emergency(double& emergency_x, double& emergency_y);
/*Reads the location of the emergency from the first line of the file.
*/
int read_stations(vector<int> & station_num, vector<double> & x, vector<double> & y);
/*Reads the station number (station_num) as well as it's x and y coordinates (vectors x and y respectively)
nd stores them in to variables.
*Returns the total number of stations read.
*/
void dispatch(double emergency_x, double emergency_y, vector<int> station_num, vector<double> x, vector<double> y, int num_stations);
/*Tests the distance of all available stations and the emergency, calling display to display the information
f the station with the shortest distance to the emergency.
*/
void display(double emergency_x, double emergency_y, vector<int> station_num, vector<double> x, vector<double> y, int num_station, double distance);
/*Displays the station number and location as well as that stations distance from the emergency, followed by the location of the
mergency itself.
*/
double get_distance(double emergency_x, double emergency_y, const vector<int> station_num, const vector<double> x, const vector<double> y, int station);
/*Uses the Pythagorean Theorem to find the distance from an emergency to a given station.
eturns the distance as a double.
*/
int main(void)
{
int num_stations; // Total number of stations
double emergency_x, emergency_y; // X and Y coordinates emergency call
vector<int> station_num; // vector that will hold stations’ ids
vector<double> x; // vector that will hold stations’ Xs
vector<double> y; // vector that will hold stations’Ys
read_emergency(emergency_x, emergency_y); // Reads the location of the emergency
num_stations = read_stations(station_num, x, y); // Reads location of available stations and places the information in vectors.
cout << "Point C" << endl;
cout << num_stations << endl;
cout<<station_num.size()<<endl;
cout<<x.size()<<endl;
cout<<y.size()<<endl;
for(int k=0; k < num_stations; k++)
{
cout << "Point D";
cout << station_num[k] << x[k] << y[k];
}
dispatch(emergency_x, emergency_y, station_num, x, y, num_stations); // Calculates which station to dispatch and displays the result
system("pause");
// return 0;
}
void read_emergency(double& emergency_x, double& emergency_y)
{
cin >> emergency_x >> emergency_y;
}
int read_stations(vector<int> & station_num, vector<double> & x, vector<double> & y)
{
int i = 0;
int id_temp;
double x_temp, y_temp;
int count = 2;
do
{
cin >> id_temp >> x_temp >> y_temp;
if(!cin.fail())
{
station_num.push_back(id_temp);
x.push_back(x_temp);
y.push_back(y_temp);
cout << station_num[i] << " " << x[i] << " " << y[i] << " " << i << endl;
i++;
cout << "Point A" << endl;
}
else if (cin.fail() || cin.eof())
break;
count--;
}while(count);
cout << " Point B" << endl;
return i;
}
void dispatch(double emergency_x, double emergency_y, vector<int> station_num, vector<double> x, vector<double> y, int num_station)
{
int j = 0;
double distance;
double test;
distance = get_distance(emergency_x, emergency_y, station_num, x, y, 0);
int counter = 1;
while(counter)
{
test = get_distance(emergency_x, emergency_y, station_num, x, y, j);
if (test < distance)
distance = test;
if (j<num_station)
j++;
else
break;
--counter;
}
display (emergency_x, emergency_y, station_num, x, y, j, distance);
}
void display(double emergency_x, double emergency_y, vector<int> station_num, vector<double> x, vector<double> y, int num_station, double distance)
{
cout << "Closest station is: " << station_num[num_station] << endl << endl;
cout << "Its X and Y coordinates are: " << x[num_station] << ", " << y[num_station] << " " << endl << endl;
cout << "Its distance from the emergency position is: " << distance << endl << endl;
cout << "The X and Y coordinates of the emergency position are: " << emergency_x << ", " << emergency_y;
}
double get_distance(double emergency_x, double emergency_y, const vector<int> station_num, const vector<double> x, const vector<double> y, int station)
{
double distance;
distance = sqrt(pow((emergency_x - x[station]), 2) + pow((emergency_y - y[station]), 2));
return distance;
}
|