[英]C Program to find area of an octagon?
我需要使用当前的函数并使用相同的参数来找到八角形的面积,每次输入某些东西时,它都会输出为“ 0.000000”,我该如何解决呢? 这也不是正确的错误检查。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int die(const char * msg);
double areaFromSide(double length);
int main()
{
double length = 0;
printf("Enter length of one side of the octagon: ");
scanf("%f", &length);
if (length > 0 && length <= 100)
{
double area = areaFromSide(length);
printf("Area of the octagon is: %f\n", area);
}
else
{
die("Input Failure!");
}
return 0;
}
double areaFromSide(double length)
{
double area = 0;
area = 2 * (1 + (sqrt(2)))*pow(length, 2);
return area;
}
int die(const char * msg)
{
printf("Fatal error: %s\n", msg);
exit(EXIT_FAILURE);
}
您总是得到0.000000
的输出,因为您的scanf()
调用是错误的。 要读取双精度字,您需要使用%lf
而不是%f
(有关更多信息,请参见此问题 )。
另外,在printf()
,您要打印区域的值,而不是其地址(通过在变量前面加上&
来检索地址)-仅使用
printf("Area of the octagon is: %f", area);
另外,关于错误捕获,您有(length > 0 || length <= 100)
作为谓词。 因为||
,这将允许任何不为负的值。 只需要一侧就可以评估为真。 你想用
(length > 0 && length <= 100)
确保长度都大于0且小于100。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.