该项目应该接受用户输入多个位置的坐标,然后比较距离并说明它们的距离。
我在他们相互交流时遇到问题。
#include <iostream>
#include "bigfunction.hpp"
#include "coord.hpp"
#include "dest.hpp"
int main()
{
distloop();
return 0;
}
大函数.cpp
#include "bigfunction.hpp"
#include "coord.hpp"
#include "dest.hpp"
double convertRadians(double deg)
{
double pi,radians;
pi = 3.1459265;
radians = pi * (deg/180.0);
return radians;
}
void distloop(void)
{
Dest start,far,closest;
std::string shortest, currentloc, farthest;
int counter;
double distance, max, min;
max = 0;
min = 0;
std::getline(std::cin,currentloc);
makeDest(start,currentloc);
std::cin >> counter;
std::cin.ignore(1);
for (int i=0; i < counter;i++) {
std::getline(std::cin,currentloc);
distance = findDist(start,currentloc);
if (min==0) {
min = distance;
shortest = currentloc;
}
if (distance > max) {
max = distance;
farthest = currentloc;
}
if (distance < min) {
min = distance;
shortest = currentloc;
}
}
makeDest(far, farthest);
makeDest(closest,shortest);
std::cout<<"Start Loc"<< strDest(start) <<std::endl;
std::cout<<"Closest Loc"<< strDest(closest) << " (" << min << "miles)" << std::endl;
std::cout<<"Farthest Loc"<< strDest(far) << " (" << max << "miles)" << std::endl;
}
大函数.hpp
#include <string>
#include <cmath>
#ifndef BIGFUNCTION_HPP
#define BIGFUNCTION_HPP
#include "dest.hpp"
#include "coord.hpp"
double convertRadians(double deg);
void distloop(void);
#endif
目标.cpp
#include "dest.hpp"
#include "bigfunction.hpp"
#include "coord.hpp"
double findDist(Dest &dest, std::string location)
{
double RAD = 3959.9;
double dlat,dlong,a,c,dist;
double lat2 = getLatitude(getField(location,1));
double long2 = getLongitude(getField(location,2));
dlat = dest.lat - lat2;
dlong = dest.longt - long2;
a = ((sin(convertRadians(dlat/2.0)))*(sin(convertRadians(dlat/2.0)))) + (cos(convertRadians(dest.lat))*cos(convertRadians(lat2)) * sin(convertRadians(dlong/2.0)))*(sin(convertRadians(dlong/2.0)));
c = 2 *atan2(sqrt(a),sqrt(1-a));
dist = RAD * c;
return dist;
}
std::string strDest(Dest &dest)
{
std::string prtStr;
prtStr = dest.latstring + " " + dest.longstring + " ("+ dest.name + ")";
return prtStr;
}
void parseCoor(Dest &dest)
{
dest.lat = getLatitude(dest.latstring);
dest.longstring = getLongitude(dest.longstring);
}
目的地.hpp
#include <string>
#include <cmath>
#ifndef DEST_HPP
#define DEST_HPP
struct Dest
{
double lat, longt;
std::string latstring,longstring, name;
};
void parseCoord(Dest &dest);
//void makeDest(Dest &dest, std::string location);
double findDist(Dest &dest, std::string location);
std::string strDest(Dest &dest);
#endif
坐标.cpp
#include "bigfunction.hpp"
#include "coord.hpp"
#include "dest.hpp"
std::string getField(std::string location, int field)
{
int m = 0;
int s = 0;
int e = -1;
std::string info;
for (int i = 0; i < location.length(); i++) {
if (location[i] == ' ') {
m++;
s = e + 1;
e = i;
}
if (m == field) {
if (field > 2) {
info = location.substr(s, location.length());
return info;
} else {
info = location.substr(s, e - s);
return info;
}
} else {
return info;
}
}
return info;
}
double getLatitude(std::string latit)
{
char d = latit.at(latit.length()-1);
double latt;
if(d == 'S') {
latt = -1 * std::stod(latit.substr(0,latit.length()-2));
} else {
latt = std::stod(latit.substr(0,latit.length()-2));
}
return latt;
}
double getLongitude(std::string longit)
{
double longt;
char d = longit.at(longit.length()-1);
if(d == 'E') {
longt = std::stod(longit.substr(0,longit.length()-2));
} else {
longt = -1 * std::stod(longit.substr(0,longit.length()-2));
}
return longt;
}
坐标文件
#include <string>
#include <iostream>
#include <cmath>
#ifndef COORD_HPP
#define COORD_HPP
std::string getField(std::string location,int field);
double getLongitude(std::string longit);
double getLatitude(std::string latit);
#endif