2016-11-22 7 views
-2

さて、これは動作していません! 何が問題なのですか? 誰かが私にそれを指摘できますか?私は何時間もそれを見つめた。 私は以前に助けを求めてきましたが、彼は構造体を思いついたのですが、本当にそれを手に入れていませんでした。私のコードに何が問題なのですか? perimeter.c

正しい計算ができません。私はファイルを読むことに何か問題があるかもしれないと思っています。 /* は、私はあなたのファイルのバイナリフォーマットは推測するバイナリファイルを読み込み、ポリゴン のパラメータ*/

typedef int16_t points[2]; 

int main(int argc, char* argv[]) { 
FILE* fp = fopen(argv[1], "rb"); 
// int points[1000]; 
//long numbytesread=0; 
int16_t num_of_coors=0; 

//open the files 

if(fp == NULL) { 
    printf("Could not open.\n"); 
    return -1; // -1 indicates error 
} 

//read file 
fread(&num_of_coors, sizeof(int16_t), 2, fp); 

points*points=malloc(sizeof(points)*num_of_coors); 

fread(points, sizeof(points), num_of_coors, fp); 



//read the array and seperate x coordinates and y coordinates 
//calculate using formula (x1-x2)^2+(y1-y2)^2 
//need 2 points, 4 coordinates at any single time. read a pair at a time 

double sum=0; 
int i=0; 
//int coors=points[0]*2+1 ; 

for(i=1;i<=num_of_coors;i++){ 
    sum+=sqrt(pow((points[i]-points[i+2]),2) + pow((points[i+1]-points[i+3]),2)); 
} 
sum+=sqrt(pow((points[1]-points[num_of_coors-2]),2) + pow((points[2]-points[num_of_coors-1]),2)); 
printf("The perimeter is %.2lf\n", sum); 

fclose(fp); 
free(points); 

}

+0

どのステートメントでエラーが発生していますか?何らかのエラーについて話しているときは、誰でも問題をすばやく特定できるように詳細を伝えてください。 –

+0

ビルドエラーについて質問するときは、問題の本文に完全な、完全な、編集されていない、および可能な情報メモを含めて常に実際のエラーを含めます。また、エラーを含めるように質問を編集するときには、コード内のどこに*のコメントを追加するなどしてエラーが発生するかを指摘してください。 –

+0

しかし、それによると、あなたは 'points'という名前の2つのシンボルを持っています。それはうまく動作しません。 –

答えて

0

を計算する:

int16_t <num of points> 
int16_t <Xcoord> int16_t <Ycoord> 
.... 

まず、それは悪い方法であります型名と変数名に同じ名前を使用する。 第2に、あなたが 'ポイント'タイプで作業しているが間違っています。 正しいコードは次のようになります。

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

/* 
read binary file and calculate parameter of polygon 
    */ 

typedef int16_t points[2]; 

int main(int argc, char* argv[]) { 
    FILE* fp = fopen(argv[1], "rb"); 
    // int points[1000]; 
    //long numbytesread=0; 
    int16_t num_of_coors=0; 



    //open the files 

    if(fp == NULL) { 
     printf("Could not open.\n"); 
      return -1; // -1 indicates error 
    } 

      //read file 
      fread(&num_of_coors, sizeof(int16_t), 1, fp); 

      points* p=malloc(sizeof(points)*num_of_coors); 

      fread(p, sizeof(points), num_of_coors, fp); 

      //read the array and seperate x coordinates and y coordinates 
      //calculate using formula (x1-x2)^2+(y1-y2)^2 
      //need 2 points, 4 coordinates at any single time. read a pair at a time 

      double sum=0; 
      int i=0; 
      //int coors=points[0]*2+1 ; 

      for(i=1;i<num_of_coors;i++) { 
      sum+=sqrt(pow(p[i-1][0]-p[i][0],2)+pow(p[i-1][1]-p[i][1],2)); 
      } 
      sum+=sqrt(pow(p[0][0]-p[num_of_coors-1][0],2)+pow(p[0][1]-p[num_of_coors-1][1],2)); 
      printf("The perimeter is %.2lf\n", sum); 

       fclose(fp); 
       free(p); 
       return 0; 
} 
関連する問題