C语言 定义计算四边形面积的函数

2024-12-27 13:13:02
推荐回答(3个)
回答1:

楼主,你把公式写错了,正确的应该是S = sqrt((s-a)*(s-b)*(s-c)*(s-d) - a*b*c*d*cosα*cosα);
代码我写好了,通过测试,结果正确

#include
#include
#define PI 3.1415926

double area(double a, double b, double c, double d, double theta)
{
double S,s;
s = (a + b + c + d) / 2;
S = sqrt((s - a) * (s - b) * (s - c) * (s - d)
- a * b * c * d * pow(cos(PI * theta / 360),2));
return S;
}

int main()
{
double a, b, c, d, theta, s;
printf("请依次输入四条边和theta的值:");
scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &theta);
s = (a + b + c + d) / 2;
while((s - a) * (s - b) * (s - c) * (s - d)
- a * b * c * d * pow(cos(PI * theta / 360),2) < 0)
{
printf("invalid input!\n");
printf("请重新输入:");
scanf("%lf %lf %lf %lf %lf", &a, &b, &c, &d, &theta);
}
printf("四边形面积为: %.4lf\n", area(a, b, c, d, theta));
return 0;
}

回答2:

#define PI 3.1415926
#include
#include
int main()
{
double a,b,c,d;
double s,S,e,m,p;
scanf("%lf\n",&a);
scanf("%lf\n",&b);
scanf("%lf\n",&c);
scanf("%lf\n",&d);
scanf("%lf\n",&m);
p=m* PI/(2*180);
s = (a+b+c+d)/2;
S = sqrt((s-a)*(s-b)*(s-c)*(s-d) - a*b*c*d*cos(p)*cos(p));
if ((s-a)*(s-b)*(s-c)*(s-d) - a*b*c*d*cos(p)*cos(p)>=0)
{
printf("%.4lf",S);
}
else
printf("Invalid input");

return 0;
}

回答3:

嘿嘿。。。。。。LZ哪个系的??