C ++程序使用Great Circle Distance公式在附近找到出租车
在本文中,我们将讨论一个程序,该程序使用GreatCircleDistance公式查找大约(小于50km)附近的出租车。
让我们假设我们得到了一个JSON文件,其中包含需要出租车的人员的姓名和坐标,以及所有可用出租车的坐标。
为了解决这个问题,我们将GPS坐标转换为double。从双精度形式,我们最终将其转换为度到弧度。然后,我们最终可以应用“大圆距离”公式来查找距用户位置50公里的出租车。
请注意,由于输入数据量很大,我们将以JSON文件作为程序中的输入,并在另一个JSON文件中提供输出。
示例
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265358979323
#define earth_radius 6371.0
//定义用户的坐标
#define latitude1d 12.9611159
#define longitude1d 77.6362214
ifstream users ("input.access_file");
ofstream out ("output.access_file");
//将度转换为弧度
double to_radian(double degree) {
return ( degree * (pi/180));
}
//计算距离
double cal_distance(double latitude2d, double longitude2d) {
double lat1, lon1, lat2, lon2, diff_lon, great_circle;
lat1 = to_radian(latitude1d);
lon1 = to_radian(longitude1d);
lat2 = to_radian(latitude2d);
lon2 = to_radian(longitude2d);
diff_lon = lon2 - lon1;
great_circle = acos( sin(lat1) * sin(lat2) + cos(lat1) *cos(lat2) * cos(diff_lon) );
return (earth_radius * great_circle);
}
//创建访问JSON文件的结构
struct access_file {
long long int user_length, i, j, x, y, m, n, f, friends,id[100000];
char latitude_string[1000], longitude_string[1000], id_string[1000], name[1000];
double latitude2d, longitude2d;
string line;
//检查距离的值
void check_distance() {
if (cal_distance(latitude2d, longitude2d) <=50.0000) {
id[i] = atoll(id_string);
i++;
out << "{\"User_id\": " << id[i - 1] << ", \"Name\": " << name << "}" << endl;
}
}
void file_parser() {
if (users.is_open()) {
while (getline(users, line)) {
f = 0; x = 0; y = 0; friends = 0; m = 0, n = 0;
user_length = line.size();
for (j = 0; j < user_length; j++) {
if (line[j] == '"')
f++;
else if (line[j] == ':')
friends++;
if (f == 3) {
j++;
while (line[j] != '"') {
latitude_string[x] = line[j];
x++; j++;
}
j--; latitude_string[x] = '\0';
}
else if (f == 13) {
j++;
while (line[j] != '"') {
longitude_string[y] = line[j];
y++; j++;
}
j--; longitude_string[y] = '\0';
}
if (friends == 2) {
j += 2;
while (line[j] != ',') {
id_string[m] = line[j];
m++; j++;
}
j--; id_string[m] = '\0';
friends++;
}
else if (friends == 4) {
j += 2;
while (line[j] != ',') {
name[n] = line[j];
n++; j++;
}
j--; name[n] = '\0';
friends++; f += 2;
}
}
//将字符串转换为float-
latitude2d = atof(latitude_string);
longitude2d = atof(longitude_string);
check_distance();
}
}
//关闭输入文件
users.close();
//关闭输出文件
out.close();
}
};
int main() {
access_file object;
object.file_parser();
return 0;
}输出结果
(A file named output.json will be created at the same place where the code and the input.json file is stored.)
热门推荐
10 小学毕业父母简短祝福语
11 幼儿生日祝福语模板简短
12 新娘妈妈的祝福语简短
13 邻居聚会祝福语大全简短
14 十条祝福语简短
15 给同学祝福语的简短
16 过年祝福语简短祝妈妈
17 祝女儿简短祝福语大全
18 恩师诗词祝福语简短大全