📜  使用“大圆距离”公式查找附近的出租车

📅  最后修改于: 2021-06-01 01:34:54             🧑  作者: Mango

给定需要出租车的人的GPS坐标(以度为单位)以及以JSON格式存储在文本文件中的城市中所有出租车的坐标,请在中找到可用的所有出租车司机的用户ID和名称50公里的距离。

例子:

使用的方法:
1.以字符串格式获取每个出租车的经度和纬度以及它们的经度
JSON编码的输入文件中的用户标识和名称。

2.将以字符串格式显示的出租车的经度和纬度转换为两倍。

3.转换用户和驾驶室中的乘员的纬度和经度
度到弧度。

4.使用Great Circle计算用户位置与驾驶室之间的距离
距离公式。

5.如果发现距离小于或等于50 kms,则输出用户-
出租车司机的ID和名称到新文件,否则不执行任何操作。

运行程序的步骤:
1.将代码和文件customer.json保存在同一位置。
2.现在,编译代码(使用cmd: g ++ file_name.cpp )并运行它(使用cmd: ./a.out /home/gfg/customers.json ),并传递文件名customer.json以及正确的位置(例如/home/gfg/customers.json)。
3.将在存在代码和customers.json文件的位置创建一个名为answers.json的文件。

// C++ code to find cabs nearby
#include 
using namespace std;
  
// Latitude of customer who needs a cab.
#define lat1d 12.9611159
  
// Longitude of customer who needs a cab.
#define lon1d 77.6362214
  
#define pi 3.14159265358979323846
#define earth_radius 6371.0
  
ifstream customer_list ("customers.json");
ofstream out ("answer.json");
  
// Function to convert degree to radian.
double degtorad(double deg)
{
    return ( deg * pi / 180);
}
  
// Function to calculate distance
// between 2 given locations 
// using Great Circle Distance Formula.
double distanceEarth(double lat2d, double lon2d)
{                 
    double lat1, lon1, lat2, lon2, 
           delta_lon, central_ang;
  
    lat1 = degtorad(lat1d);
    lon1 = degtorad(lon1d);
    lat2 = degtorad(lat2d);
    lon2 = degtorad(lon2d);
  
    delta_lon = lon2 - lon1;
      
    // great circle distance formula.
    central_ang = acos ( sin(lat1) *
                  sin(lat2) + cos(lat1) *
                  cos(lat2) * cos(delta_lon) ); 
                    
    return (earth_radius * central_ang);
}
  
// Structure which contains data and
// functions for accessing and processing
// data from the given customers.json file.
struct json
{
    /* i and j are used to access various
    elements of the char arrays. x is used
    to measure the size of the element of
    latitude_as_string array. y is used to
    measure the size of the element of
    longitude_as_string array. m is used
    to measure the size of the element
    of id_as_string array. n is used to
    measure the size of the element of
    name array. f keeps count of " " "
    symbol. fi keeps count of " : " symbol.
    */
    long long int length, i, j, x, y, m,
                  n, f, fi, id[100000];
      
    char latitude_as_string[1000], 
         longitude_as_string[1000], 
         id_as_string[1000], name[1000];
      
    double lat2d, lon2d;
      
    // To get each line of customers.json
    // file as string.
    string line;
  
    // Function to check whether distance between
    // 2 points is less than 50km or not.
    void distance_calculator()
    {
        if (distanceEarth(lat2d, lon2d) <= 50.0000)
        {
            // Converting id to int format.
            id[i] = atoll(id_as_string);
            i++;
            out << "{\"user_id\": " << id[i - 1] << 
                ", \"name\": " << name << "}" << endl;
        }
    }
  
    // Function to read various attributes
    // like latitude, longitude, name , id,
    // etc, from customers.json file. simplistic
    // approach is used to get JSON attributes.
    void json_parser()
    {                     
        if (customer_list.is_open())
        {
              
            while (getline(customer_list, line))
            {
                  
                f = 0; x = 0; y = 0; fi = 0; m = 0, n = 0;
                length = line.size();
  
                for (j = 0; j < length; j++)
                {
                      
                    if (line[j] == '"')
                        f++;
  
                    else if (line[j] == ':')
                        fi++;
                          
                    // To get latitude of the location.    
                    if (f == 3)
                    {
                        j++;
  
                        while (line[j] != '"')
                        {
                            latitude_as_string[x] = line[j];
                            x++; j++;
                        }
  
                        j--; latitude_as_string[x] = '\0';
                    }
                      
                    // To get longitude of the location.
                    else if (f == 13)
                    {
                        j++;
  
                        while (line[j] != '"')
                        {
                            longitude_as_string[y] = line[j];
                            y++; j++;
                        }
  
                        j--; longitude_as_string[y] = '\0';
                    }
                      
                    // To get id of the friend.
                    if (fi == 2)
                    {
                        j += 2;
  
                        while (line[j] != ',')
                        {
                            id_as_string[m] = line[j];
                            m++; j++;
                        }
  
                        j--; id_as_string[m] = '\0';
                        fi++;
                    }
                      
                    // To get name of the friend.
                    else if (fi == 4)
                    {
                        j += 2;
  
                        while (line[j] != ',')
                        {
                            name[n] = line[j];
                            n++; j++;
                        }
  
                        j--; name[n] = '\0';
                        fi++; f += 2;
                    }
                }
  
                // Converting latitude and longitude
                // in string to float.
                lat2d = atof(latitude_as_string);
                lon2d = atof(longitude_as_string);
                distance_calculator();
            }
        }
          
        // closing stream of customer's file.
        customer_list.close();
          
        // closing stream of answer's file.
        out.close();
    }
};
  
int main()
{
    // Creating object of the structure json
    json obj;
      
    // To read customers.json file.
    obj.json_parser();
    return 0;
}

输入文件(customers.json):

{"latitude": "12.986375", "user_id": 12, "name": "Chris", "longitude": "77.043701"}
{"latitude": "11.92893", "user_id": 1, "name": "Alice", "longitude": "78.27699"}
{"latitude": "11.8856167", "user_id": 2, "name": "Ian", "longitude": "78.4240911"}
{"latitude": "12.3191841", "user_id": 3, "name": "Jack", "longitude": "78.5072391"}
{"latitude": "13.807778", "user_id": 28, "name": "Charlie", "longitude": "76.714444"}
{"latitude": "13.4692815", "user_id": 7, "name": "Frank", "longitude": "-9.436036"}
{"latitude": "14.0894797", "user_id": 8, "name": "Eoin", "longitude": "77.18671"}
{"latitude": "13.038056", "user_id": 26, "name": "Stephen", "longitude": "76.613889"}
{"latitude": "14.1225", "user_id": 27, "name": "Enid", "longitude": "78.143333"}
{"latitude": "13.1229599", "user_id": 6, "name": "Theresa", "longitude": "77.2701202"}
{"latitude": "12.2559432", "user_id": 9, "name": "Jack", "longitude": "76.1048927"}
{"latitude": "12.240382", "user_id": 10, "name": "Georgina", "longitude": "77.972413"}
{"latitude": "13.2411022", "user_id": 4, "name": "Ian", "longitude": "77.238335"}
{"latitude": "13.1302756", "user_id": 5, "name": "Nora", "longitude": "77.2397222"}
{"latitude": "13.008769", "user_id": 11, "name": "Richard", "longitude": "77.1056711"}
{"latitude": "13.1489345", "user_id": 31, "name": "Alan", "longitude": "77.8422408"}
{"latitude": "13", "user_id": 13, "name": "Olive", "longitude": "76"}
{"latitude": "11.999447", "user_id": 14, "name": "Helen", "longitude": "-9.742744"}
{"latitude": "12.966", "user_id": 15, "name": "Michael", "longitude": "77.463"}
{"latitude": "12.366037", "user_id": 16, "name": "Ian", "longitude": "78.179118"}
{"latitude": "14.180238", "user_id": 17, "name": "Patricia", "longitude": "-5.920898"}
{"latitude": "13.0033946", "user_id": 39, "name": "Lisa", "longitude": "77.3877505"}
{"latitude": "12.228056", "user_id": 18, "name": "Bob", "longitude": "76.915833"}
{"latitude": "14.133333", "user_id": 24, "name": "Rose", "longitude": "77.433333"}
{"latitude": "55.033", "user_id": 19, "name": "Enid", "longitude": "78.112"}
{"latitude": "13.121111", "user_id": 20, "name": "Enid", "longitude": "-9.831111"}
{"latitude": "11.802", "user_id": 21, "name": "David", "longitude": "-9.442"}
{"latitude": "14.374208", "user_id": 22, "name": "Charlie", "longitude": "78.371639"}
{"latitude": "13.74412", "user_id": 29, "name": "Oliver", "longitude": "76.11167"}
{"latitude": "13.761389", "user_id": 30, "name": "Nick", "longitude": "76.2875"}
{"latitude": "14.080556", "user_id": 23, "name": "Eoin", "longitude": "77.361944"}
{"latitude": "12.833502", "user_id": 25, "name": "David", "longitude": "78.122366"}

输出文件(answers.json):

{"user_id": 6, "name": "Theresa"}
{"user_id": 5, "name": "Nora"}
{"user_id": 31, "name": "Alan"}
{"user_id": 15, "name": "Michael"}
{"user_id": 39, "name": "Lisa"}

参考:大圆距离

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”