[C] language known long sides of a triangle, the triangle area seek

First, the mathematical basis:

Three sides of the triangle are known, the area of ​​the triangle is calculated, the need to use Heron's formula:

I.e. p = (a + b + c) / 2

Second, the algorithm:

Three input side length, formula Helen applied area, and outputs. 

May first be determined whether a triangular configuration, i.e. the sum of any two sides is greater than the third side, the triangle may be formed and then calculated by the following case, the increase stringency.

Third, the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <math.h>
int  main()
{
 printf ( "Enter three sides of \ n order");
     double  a,b,c,p,s;
     scanf ( "%lf%lf%lf" ,&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 ( "面积为%lf\n" , s); //输出结果
     }
     else  printf ( "无法构成三角形\n" ); //输入不合法,提示。
     return  0;
}

Guess you like

Origin www.cnblogs.com/HGNET/p/11751570.html