我正在尝试使用城市的经度和纬度制作城市距离计算器。我遇到的问题是把城市变成一个阵列。我的数组尝试在main方法中被注释掉了。请让我知道我做错了什么。
#include <iostream>
#include <math.h>
#define pi 3.14159265358979323846
using namespace std;
double distance(double lat1, double lon1, double lat2, double lon2, char unit);
double deg2rad(double deg);
double rad2deg(double rad);
class City
{
public:
//declare variables
string name;
double latitude;
double longitude;
City(string, double, double); //constructor
double distance(string, string);
};
double cityDistance(City a, City b);
int main() {
//construct cities
City Providence("Providence", 41.8239890, -71.4128340);
City Cranston("Cranston", 41.7798230, -71.4372800);
City NewYork("NewYork", 40.7143530, -74.0059730);
City Boston("Boston", 42.3584310, -71.0597730);
City Killington("Killington", 43.6775680, -72.7798250);
City Springfield("Springfield", 42.1014830, -72.5898110);
City Bridgeport("Bridgeport", 41.1865480, -73.1951770);
City Cambridge("Cambridge", 42.3736160, -71.1097340);
City Norwalk("Norwalk", 41.1175970, -73.4078970);
City Quincy("Quincy", 42.2528770, -71.0022710);
//put city into array
// City cityArray[] = { };
// cityArray[0] = {"Providence", 41.8239890, -71.4128340};
// cityArray[1] = {"Cranston", 41.7798230, -71.4372800};
// cityArray[2] = {"NewYork", 40.7143530, -74.0059730};
// cityArray[3] = {"Boston", 42.3584310, -71.0597730};
// cityArray[4] = {"Killington", 43.6775680, -72.7798250};
// cityArray[5] = {"Springfield", 42.1014830, -72.5898110};
// cityArray[6] = {"Bridgeport", 41.1865480, -73.1951770};
// cityArray[7] = {"Cambridge", 42.3736160, -71.1097340};
// cityArray[8] = {"Norwalk", 41.1175970, -73.4078970};
// cityArray[9] = {"Quincy", 42.2528770, -71.0022710};
//calculate distance
cout << "Distance between Providence and Cranston: " << distance(41.8239890, -71.4128340, 41.7798230, -71.4372800, 'M') << endl;
cout << "Distance between NewYork and Boston: " << distance(40.7143530, -74.0059730, 42.3584310, -71.0597730, 'M') << endl;
cout << "Distance between Killington and Norwalk: " << distance(43.6775680, -72.7798250, 41.1175970, -73.4078970, 'M') << endl;
//print distance using city name
cout << endl;
cout << "Distance between Providence and Cranston: " << cityDistance(Providence, Cranston) << endl;
cout << "Distance between NewYork and Boston: " << cityDistance(NewYork, Boston) << endl;
cout << "Distance between Killington and Norwalk: " << cityDistance(Killington, Norwalk) << endl;
//end
return 0;
}
City::City(string n, double lt, double lg)
{
name = n;
latitude = lt;
longitude = lg;
}
double deg2rad(double deg)
{
return (deg * pi / 180);
}
double rad2deg(double rad)
{
return (rad * 180 / pi);
}
double distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
double theta, dist;
theta = lon1 - lon2;
dist = sin(deg2rad(lat1)) * sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * cos(deg2rad(theta));
dist = acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
switch(unit) {
case 'M':
break;
case 'K':
dist = dist * 1.609344;
break;
case 'N':
dist = dist * 0.8684;
break;
}
return(dist);
}
double cityDistance(City a, City b)
{
return distance(a.latitude, a.longitude, b.latitude, b.longitude, 'M');
}
答案 0 :(得分:4)
你现在的做法是创建一个大小为0的数组。然后你尝试将你的城市放在不存在的单元格0到9中。
解决该问题的一种方法是声明一个大小为10的数组,而不在声明点指定内容。
City cityArray[10];
然后像您一样单独分配每个单元格。然而,这不是最佳解决方案,它甚至无法工作,因为您尚未定义默认的无参数构造函数。
您可以改为创建一个您确切希望数组保持的城市数组。请参阅下面的代码。
如果你想使用类似结构的初始化,就像你一样,代码将是这样的:
//put city into array
City cityArray[] = {
{"Providence", 41.8239890, -71.4128340},
{"Cranston", 41.7798230, -71.4372800},
{"NewYork", 40.7143530, -74.0059730},
{"Boston", 42.3584310, -71.0597730},
{"Killington", 43.6775680, -72.7798250},
{"Springfield", 42.1014830, -72.5898110},
{"Bridgeport", 41.1865480, -73.1951770},
{"Cambridge", 42.3736160, -71.1097340},
{"Norwalk", 41.1175970, -73.4078970},
{"Quincy", 42.2528770, -71.0022710}
};
但是,您已经为City类定义了一个构造函数,因此,这将无法编译。你可以这样做:
City cityArray[] = {
City("Providence", 41.8239890, -71.4128340),
City("Cranston", 41.7798230, -71.4372800),
City("NewYork", 40.7143530, -74.0059730),
City("Boston", 42.3584310, -71.0597730),
City("Killington", 43.6775680, -72.7798250),
City("Springfield", 42.1014830, -72.5898110),
City("Bridgeport", 41.1865480, -73.1951770),
City("Cambridge", 42.3736160, -71.1097340),
City("Norwalk", 41.1175970, -73.4078970),
City("Quincy", 42.2528770, -71.0022710)
};
您还可以重复使用刚刚创建的City对象:
City cityArray[] = {
Providence,
Cranston,
NewYork,
Boston,
Killington,
Springfield,
Bridgeport,
Cambridge,
Norwalk,
Quincy
};
但是在上面的代码中,您必须知道对象Providence
,Cranston
等将被复制到数组中。因此,您将拥有相同数据的两个独立副本,例如一个在Providence
变量中,一个在cityArray[0]
中。改变一个不会影响另一个。
无论你选择什么情况,这都会创建一个固定大小的数组(因为c ++中的所有数组都有固定的大小)。您无法再添加任何城市。
如果您希望阵列是“可扩展的” - 您希望将来添加新城市,那么您应该使用其中一个STL集合而不是数组。例如vector<City>
或map<City>
。如果您打算搜索特定城市的集合,例如地图特别有用,例如以它的名字命名。
答案 1 :(得分:3)
这是何时使用std::vector
或std::map
的一个很好的例子。在这种特殊情况下,我认为std::map
将是更好的选择(因为您不必迭代整个列表来查找您正在寻找的2个城市):
std::map<std::string, City> cities;
cities["Providence"] = City("Providence", 41.8239890, -71.4128340);
// ...
答案 2 :(得分:1)
在C ++中,数组不是动态的。您需要使用您将需要的确切大小声明您的数组。您也不能使用该表示法填充类,它不是有效的C ++构造,因为您没有声明默认构造函数。 尝试这样的事情:
//put city into array
City cityArray[9];
cityArray[0].name = "Providence";
cityArray[0].latitude = 41.8239890;
cityArray[0].longitude = -71.4128340;
cityArray[1] = ...
当然,您可以使用类的构造函数和类似vector的STL容器,使其更简洁,并使其更具动态性。
explicit vector<City*> cityArray(9);
cityArray.push_back(new City("Providence", 41.8239890, -71.4128340));
...
向量的构造函数中的参数(大小)是可选的,但如果明确告诉它为您要存储的最可能数量的城市保留内存,则会获得更好的性能。如果超过该限制,矢量将自动增长。
答案 3 :(得分:0)
使用此
City cityArray[10] = {
City ("Providence", 41.8239890, -71.4128340),
City ("Cranston", 41.7798230, -71.4372800),
City ("NewYork", 40.7143530, -74.0059730),
City ("Boston", 42.3584310, -71.0597730),
City ("Killington", 43.6775680, -72.7798250),
City ("Springfield", 42.1014830, -72.5898110),
City ("Bridgeport", 41.1865480, -73.1951770),
City ("Cambridge", 42.3736160, -71.1097340),
City ("Norwalk", 41.1175970, -73.4078970),
City ("Quincy", 42.2528770, -71.0022710)
};