So in the project I'm doing I have a circle centered at the origin and I need to cut a vertical line through it. I'm trying to calculate the area of the smaller ratio of the circle; my inputs are the radius and the left and right hand ratio. for now I'm just trying to make sure it's doing my calculations correctly, and it is calculating the ratio properly, but its not calculating the ratio area at all. please help
在我做的这个项目中,我有一个以原点为中心的圆我需要在它上面划出一条垂直线。我要计算圆的小比例的面积;我的输入是半径和左右手的比例。现在我只是想确保计算正确,它计算的是正确的比例,但它不计算比值面积。请帮助
int main(){
float radius, left, right, total;
float ratio;
float ratioArea = ratio*pi*pow(radius, 2);
scanf("%d%d%d", &radius, &left, &right);
if(left == right)
{
printf("left and right are equal");
}
// do area calculations with righthand side
else if(left > right)
{
ratio = right / (right+left);
ratioArea = ratio * pi * pow(radius, 2);
printf("ratio= %.6f, area = %f\n", ratio, ratioArea);
}
// do area calculations with lefthand side
else if(left < right)
{
ratio = left / (left + right);
ratioArea = ratio*pi*pow(radius, 2);
printf("ratio = %.6f, area = %f\n", ratio, ratioArea);
}
return 0;
}
}
1 个解决方案
#1
3
The first problem is here:
第一个问题是:
scanf("%d%d%d", &radius, &left, &right);
%d
reads an integer and takes an int *
, not a float *
. You need %f
to scan a float
.
%d读取整数并取int *,而不是浮点数*。需要%f扫描浮点数。
You also need to check whether scanf
was successful:
您还需要检查scanf是否成功:
if (scanf("%f%f%f", &radius, &left, &right) != 3) {
// handle error here
exit(EXIT_FAILURE);
}
scanf
returns the number of successfully scanned items. If the return value is not 3, at least one of your variables was not set.
scanf返回成功扫描项目的数量。如果返回值不是3,那么至少有一个变量没有设置。
A second (mostly harmless) problem:
第二个问题(基本上是无害的):
float radius, left, right, total;
float ratio;
float ratioArea = ratio*pi*pow(radius, 2);
You're initializing ratioArea
based on the values of ratio
and radius
, which are uninitialized at this point. I'm pretty sure this code has undefined behavior.
您正在根据比率和半径的值初始化比率区域,这些值此时尚未初始化。我很确定这段代码有未定义的行为。
Just remove the initialization and do float ratioArea;
instead; you already set ratioArea
in each branch where you use it below.
只需要删除初始化并做浮点比例区域;相反;您已经在下面使用它的每个分支中设置了ratioArea。
#1
3
The first problem is here:
第一个问题是:
scanf("%d%d%d", &radius, &left, &right);
%d
reads an integer and takes an int *
, not a float *
. You need %f
to scan a float
.
%d读取整数并取int *,而不是浮点数*。需要%f扫描浮点数。
You also need to check whether scanf
was successful:
您还需要检查scanf是否成功:
if (scanf("%f%f%f", &radius, &left, &right) != 3) {
// handle error here
exit(EXIT_FAILURE);
}
scanf
returns the number of successfully scanned items. If the return value is not 3, at least one of your variables was not set.
scanf返回成功扫描项目的数量。如果返回值不是3,那么至少有一个变量没有设置。
A second (mostly harmless) problem:
第二个问题(基本上是无害的):
float radius, left, right, total;
float ratio;
float ratioArea = ratio*pi*pow(radius, 2);
You're initializing ratioArea
based on the values of ratio
and radius
, which are uninitialized at this point. I'm pretty sure this code has undefined behavior.
您正在根据比率和半径的值初始化比率区域,这些值此时尚未初始化。我很确定这段代码有未定义的行为。
Just remove the initialization and do float ratioArea;
instead; you already set ratioArea
in each branch where you use it below.
只需要删除初始化并做浮点比例区域;相反;您已经在下面使用它的每个分支中设置了ratioArea。