我正在編寫一個程序,它必須使用函數,並且我沒有收到任何語法錯誤,並嘗試了多次嘗試更改,但沒有成功。該函數沒有正確保存到.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
'fprintf'格式'%8i'對'double n'不好;' –
您對計算沒有疑問。你有一個關於你面臨困難的故事。你需要學習[如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – StoryTeller
我甚至不理解'n'的作用 - 周長和麪積函數只需要一個參數:半徑。 –