Computer >> Máy Tính >  >> Lập trình >> C ++

Chương trình C ++ để tìm xe taxi ở gần bằng công thức Khoảng cách vòng tròn lớn

Trong bài viết này, chúng ta sẽ thảo luận về một chương trình tìm các xe taxi ở gần (dưới 50km) bằng cách sử dụng công thức Khoảng cách Vòng tròn Lớn.

Giả sử chúng tôi đã được cung cấp một tệp JSON chứa tên và tọa độ của những người cần taxi và cũng là tọa độ của tất cả các taxi hiện có.

Để giải quyết vấn đề này, chúng tôi sẽ chuyển đổi tọa độ GPS thành gấp đôi. Từ dạng kép, cuối cùng chúng ta sẽ chuyển đổi chúng theo độ sang radian. Sau đó, cuối cùng chúng tôi có thể áp dụng công thức Khoảng cách vòng tròn lớn để tìm các xe taxi có sẵn trong 50 km tính từ vị trí của người dùng.

Lưu ý rằng do số lượng lớn dữ liệu đầu vào, chúng tôi sẽ lấy tệp JSON làm đầu vào trong chương trình và cung cấp đầu ra trong tệp JSON khác.

Ví dụ

#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265358979323
#define earth_radius 6371.0
//defining the user's coordinates
#define latitude1d 12.9611159
#define longitude1d 77.6362214
ifstream users ("input.access_file");
ofstream out ("output.access_file");
//converting degree to radian
double to_radian(double degree) {
   return ( degree * (pi/180));
}
//to calculate the distance
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);
}
//creating structure to access JSON file
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;
   //to check the value of distance
   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;
               }
            }
            //converting string to float
            latitude2d = atof(latitude_string);
            longitude2d = atof(longitude_string);
            check_distance();
         }
      }
      //closing input file
      users.close();
      //closing output file
      out.close();
   }
};
int main() {
   access_file object;
   object.file_parser();
   return 0;
}

Đầu ra

(A file named output.json will be created at the same place where the code and the input.json file is stored.)