目录
构成三角形的基本条件
任意输入的三个数要满足三角形的基本条件:任意两边之和大于第三边。即:
a+b>c a+c>b b+c>a
则:if(a+b>c&&a+c>b&&b+c>a)
三角形的面积公式
本次程序使用海伦公式:
,其中 P= a+b+c/2
![]()
代码演示
#include <stdio.h>
#include <math.h>
int main()
{
//由于面积S有的包含小数,因此采取float,而不采取int型输入;
float a, b, c,S;
printf("please enter three numbers from the keyboard:\n\n");
scanf("%f%f%f", &a, &b, &c);
printf("\n");
int p = (a + b + c) / 2;
if (a + b > c && a + c > b && b + c > a)
{
S = sqrt (p*(p - a)*(p - b)*(p - c));
printf("面积S=%.3f", S);
}
else
printf("The three numbers entered can't form a triangle\n");
return 0;
}