C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

大圏距離式を使用して近くのタクシーを見つけるC++プログラム


この記事では、大圏距離の式を使用して、約(50 km未満)近くのタクシーを見つけるプログラムについて説明します。

タクシーを必要とする人の名前と座標、および利用可能なすべてのタクシーの座標を含むJSONファイルが提供されたとします。

これを解決するには、GPS座標をdoubleに変換します。ダブルフォームから、最終的に度単位でラジアンに変換します。次に、最終的に大圏距離の式を適用して、ユーザーの位置から50kmで利用可能なタクシーを見つけることができます。

入力データが大量にあるため、プログラムの入力としてJSONファイルを取得し、別のJSONファイルでも出力を提供することに注意してください。

#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;
}

出力

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

  1. C++を使用して楕円の領域を見つけるプログラム

    ここでは、C++を使用して楕円の面積を取得する方法を説明します。楕円にはさまざまな部分があります。これらは以下のようなものです。 キーポイント 説明 センター 楕円の中心。また、2つの焦点を結ぶ線分の中心でもあります。 主軸 楕円の最長直径 nmemb これは要素の数であり、各要素のサイズはサイズです。 バイト。 短軸 楕円の最小直径 コード tを指す線分 フォーカス 図で示されている2つのポイント ロータス直腸 蓮の直腸は、焦点を通り、楕円の主軸に垂直な線です。 楕円の面積はΠ𝜋 ∗𝑎a∗b𝑏 サンプルコード #include <iostre

  2. GCDを見つけるためのC++プログラム

    2つの数値の最大公約数(GCD)は、両方を除算する最大の数値です。 例:45と27の2つの数字があるとします。 45 = 5 * 3 * 3 27 = 3 * 3 * 3 したがって、45と27のGCDは9です。 2つの数値のGCDを見つけるプログラムは次のとおりです。 例 #include <iostream> using namespace std; int gcd(int a, int b) {    if (b == 0)    return a;    return gcd(b, a % b); } int