หารถแท็กซี่ที่อยู่ในรัศมี 50 กม. จากผู้ใช้ โดยใช้สูตร Great Circle Distance
รับไฟล์ JSON ที่มีพิกัดผู้ใช้และรถแท็กซี่ แปลง GPS coordinates เป็น double แล้วแปลงเป็น radian นำไปใช้ในสูตร Great Circle Distance
เนื่องจากข้อมูลมาก ใช้ไฟล์ JSON เป็น input และ output
ตัวอย่างโค้ด (โครงสร้างหลัก)
#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 = to_radian(latitude1d);
double lon1 = to_radian(longitude1d);
double lat2 = to_radian(latitude2d);
double lon2 = to_radian(longitude2d);
double diff_lon = lon2 - lon1;
double great_circle = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(diff_lon));
return (earth_radius * great_circle);
}
struct access_file {
// ... parsing JSON fields ...
void check_distance() {
if (cal_distance(latitude2d, longitude2d) <= 50.0000) {
// write to output JSON
}
}
void file_parser() { /* parse JSON line by line */ }
};
int main() {
access_file object;
object.file_parser();
return 0;
}
ผลลัพธ์
(จะสร้างไฟล์ output.json ที่ตำแหน่งเดียวกับโค้ดและ input.json)