2016-11-08 7 views
0

関数を使用しなければならないクラス用のプログラムを作成していますが、構文エラーが発生せず、複数回試行して成功しないものがありました。関数が.outファイルに正しく保存されていません。C返されない関数

ここではコードです:

/* LAB13, functions          */ 
/* Given the number of sides of an n-side regular polygon */ 
/* and the radius of the circle, find the perimeter and */ 
/* area of the polygon         */ 

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#define IN_FILE "lab13.dat" 
#define OUT_FILE "lab13.out" 

/* function prototypes */ 
double get_perimeter(double n, double radius); 
double get_area(double n, double radius); 

int main(void) 
{ 
     double n;    /* number of sides   */ 
     double radius;   /* radius of the circle  */ 
     double area;   /* area of the polygon  */ 
     double perimeter;  /* perimeter of the polygon */ 

    FILE * data_in;  /* input file pointer */ 
    FILE * data_out; /* output file pointer */ 

    /* Open the two required files */ 
    data_in = fopen(IN_FILE, "r"); 
    if (data_in == NULL) 
    { 
     printf("Error on fopen file %s \n", IN_FILE); 
     exit(EXIT_FAILURE); 
    } 

    data_out = fopen(OUT_FILE, "w"); 
    if (data_out == NULL) 
    { 
     printf("Error on fopen file %s \n", OUT_FILE); 
     exit(EXIT_FAILURE); 
    } 
    /* Print headers */ 
    fprintf(data_out, "\nScott _________. Lab13. \n\n"); 
    fprintf(data_out, " Number Of Sides  Radius Perimeter  Area \n"); 
    fprintf(data_out, " of Polygon  of Circle of Polygon of Circle \n"); 
    fprintf(data_out, "---------------- --------- ---------- --------- \n"); 
    while ((fscanf(data_in, "%lf%lf", &n, &radius))== 2) 
     { 
      perimeter = get_perimeter(n, radius); 
      area  = get_area(n, radius); 
      fprintf(data_out,"%8i %17.3f %11.3f %10.3f\n", 
       n, radius, perimeter, area); 
     } 
    printf("\n"); 
    fclose(data_in); 
    fclose(data_out); 
    return EXIT_SUCCESS; 
} 

/*-----------------------------------------------------------*/ 
double get_perimeter(double n, double radius) 
{ 
    double perimeter; 

    perimeter = 2 * n * radius * (sin(M_PI/n)); 
    return perimeter; 
} 

/*-----------------------------------------------------------*/ 
double get_area(double n, double radius) 
{ 
    double area; 

    area = 0.5 * n * (radius*radius) * (sin(2*M_PI)/n); 
    return area; 
} 

/*-----------------------------------------------------------*/ 

それから奪っていることlab13.datファイルは次のとおりです。

3.0 16.5
5.0 9.1
7.0 11.5
The output from running the program

+2

'fprintf'形式の'%8i'は 'double n;には悪いです。 –

+1

コンピューティングについての質問はありません。あなたは、あなたが直面する困難についての話を持っています。あなたは[小さなプログラムをデバッグする方法](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を学ぶ必要があります – StoryTeller

+0

私は 'n'の役割を理解していません - 境界関数と領域関数には1つの引数、つまり半径のみが必要です。 –

答えて

1

あなたはndouble)を印刷し、フォーマットは%8iintが必要です。 doubleを印刷するが、可能性のある非整数部分を抑止する場合は、%8.0fのように0の精度を使用します。

1

問題はfprintfにあります。

fprintf(data_out,"%8i %17.3f %11.3f %10.3f\n", n, radius, perimeter, area); 

私はあなたが望むフォーマット指定子ではありません。おそらくタイプミスです