![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
编程题:编写程序输入三角形的3条边长,计算并输出三角形的面积。
2022-12-14 · 百度认证:北京惠企网络技术有限公司官方账号
![](https://wyw-base.cdn.bcebos.com/pc-content/follow.gif)
一、程序分析
三角形面积海伦公式:√[ p ( p - a ) ( p - b ) ( p - c ) ] 。其中 p = (a + b + c) / 2 。a、b、c分别是三角形的三边长。
二、根据三角形面积计算公式用if语句编写程序如下:
#include "stdio.h"
#include "math.h"
int main(void)
{
float a = 0, b = 0, c = 0, p = 0;
float area = 0;
printf("Please input three sides of triangle:");
scanf_s("%f %f %f", &a, &b, &c);
if((a + b) > c && (a + c) > b && (b + c) > a)
{
p = (a + b + c) / 2;
area = sqrt(p * (p - a) * (p - b) * (p - c));
}
else
printf("Triangle does not exist!\n");
printf("The area of triangle is:%f\n", area);
return 0;
扩展资料:
还可以使用switch语句计算三角形的面积,编写程序如下
#include "stdio.h"
#include "math.h"
int main(void)
{
float a = 0, b = 0, c = 0;
float p = 0;
printf("Please input three sides of triangle:");
scanf_s("%f %f %f", &a, &b, &c);
switch (a + b > c && a + c > b && b + c > a)
{
case 0:printf("Triangle does not exist!\n"); break;
case 1:
p = (a + b + c)*0.5;
printf("The area of triangle is:%f\n", sqrt(p * (p - a) * (p - b) * (p - c)));
break;
}
return 0;
}
参考资料:百度百科-switch
参考资料:百度百科-结束条件语句