已知三角形三条边长,求该三角形的面积:
补充:
海伦公式:
海伦公式验证:
代码实现:
# include <stdio.h>
# include <math.h>
int main()
{
float a,b,c,p,s;
printf("Please enter three sides:\n");
scanf("%f%f%f",&a,&b,&c);
if(a+b>c&&a+c>b&&b+c>a) // 构成三角形的条件
{
p=(a+b+c)/2; // 周长的一半
s = sqrt(p*(p-a)*(p-b)*(p-c)); // 海伦公式
printf("The area of triangle is %f.",s);
}
else
printf("Data error!");
return 0;
}
运行结果: