0

地球上の 2 点間の距離と方位角を計算するプログラムを作成しようとしています。次に、距離と方位角を取得したら、ダイクストラのアルゴリズムを使用して最短経路を見つけるグラフ データ構造を作成する必要があります。

テキスト ファイルの数行を次に示します。各行は、2 つの都市の位置の座標を表し、2 つの都市間の距離を見つけます。

Cairo=  30:2:39 N 31:14:8 E |Cape Town=  33:55:29 S 18:25:26 E
Cape Town=  33:55:29 S 18:25:26 E |Cairo=  30:2:39 N 31:14:8 E
Cairo=  30:2:39 N 31:14:8 E |Lagos=  6:27:11 N 3:23:45 E
Lagos=  6:27:11 N 3:23:45 E |Cairo=  30:2:39 N 31:14:8 E
Lagos=  6:27:11 N 3:23:45 E |Cape Town=  33:55:29 S 18:25:26 E
Cape Town=  33:55:29 S 18:25:26 E |Lagos=  6:27:11 N 3:23:45 E
Lagos=  6:27:11 N 3:23:45 E |Birmingham=  52:20:10 N 1:53:25 E
Birmingham=  52:20:10 N 1:53:25 E |Lagos=  6:27:11 N 3:23:45 E

形式は次のとおりです。

<lat> d:m:s <N|S>, 
where d = degrees, m = minutes, s = seconds (max(d) == 90)
<lon> is d:m:s <E|W> (max(d) == 180)

分と秒は重要ですか?

これは、この Web サイトhttp://www.krysstal.com/sphertrig.htmlのコサイン ルールを使用した私の距離関数です。

void findDistance(float x1, float y1, float x2, float y2) {
    float a,b,c;
    float distance;

    /*

    if (latDir == 'W' or lonDir == 'S') {
        //change to negative
    }

    */

    a = y2-y1;
    b = 90-x1;
    c = 90-x2;

    printf("\na = %f b = %f c = %f",a,b,c); 

    //convert to radians for trig functions
    a = a * DEG_TO_RAD;
    b = b * DEG_TO_RAD;
    c = c * DEG_TO_RAD;

    printf("\na = %f b = %f c = %f",a,b,c); 

    distance = cos(b)*cos(c)+sin(b)*sin(c)*cos(a);

    printf("\nCos(distance) in radians = %f",distance);

    distance = acos(distance); 

    float distDegree = distance*RAD_TO_DEG;

    printf("\nCos(distance) in degrees = %f",distDegree);

    distance = EARTH_CIRCUM * distDegree/360;

    printf("\ndistance = %f",distance);

    //return distance; 



}
4

1 に答える 1

1

私は自由にあなたの入力をサニタイズし、次のように各エントリを 1 行に配置しました。

Birmingham=  52:20:10 N 1:53:25 E 
Cairo=  30:2:39 N 31:14:8 E 
Cape Town=  33:55:29 S 18:25:26 E
Lagos=  6:27:11 N 3:23:45 E

重複も削除しました。

#include <stdio.h>

int  main() {
    char line[256];

    // data
    char name[64];
    // hour, minute, second, direction
    int lat_h, lat_m, lat_s; char lat_d;
    int long_h, long_m, long_s; char long_d;

    FILE *fin = fopen("in", "r");

    while (NULL != fgets(line, 256, fin)) {
        sscanf(line, "%[^=]=%*[ ]%d:%d:%d%*[ ]%c%*[ ]%d:%d:%d%*[ ]%c",
            name,
            &lat_h, &lat_m, &lat_s, &lat_d,
            &long_h, &long_m, &long_s, &long_d
        );

        printf("Name: %s\nlat: %d:%d:%d %c\nlong: %d:%d:%d %c\n\n",
            name, 
            lat_h, lat_m, lat_s, lat_d,
            long_h, long_m, long_s, long_d
        );

    }
    return 0;
}

1 行に多くのエントリがある場合は、区切り記号で分割しsscanf、各部分でそれを実行します。

注:この回答は、タイトルが実際の質問であることを前提としています。質問がある場合はIs the minutes and seconds important?、@Jongware のコメントを読んでください。

于 2013-07-27T23:29:33.087 に答える