代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
int main() {
float a, b, c, s, h;
printf("请输入三角形的3个边长:\n");
scanf("%f%f%f", &a, &b, &c);
if (a <= 0 || b <= 0 || c <= 0) {
printf("输入错误!\n");
return 0;
}
if (a + b > c || a + c > b || b + c > a) {
h = (a + b + c) / 2.0; //周长
s = sqrt(h * (h - a) * (h - b) * (h - c)); //海伦公式 s = sqrt(h * (h - a) * (h - b) * (h - c))
printf("三角形面积:%f\n", s);
}
else
{
printf("无效输入!\n");
}
return 0;
}
运行结果:
![](https://img-blog.csdnimg.cn/dbd4c38545a9476eb6f38f7de36beda9.png)
![](https://img-blog.csdnimg.cn/0f08cc2e724644a6b59ce4a335f98ce4.png)
![](https://img-blog.csdnimg.cn/ea74473972cb4098af20b8c946212ea0.png)