上机题一
题目:
从键盘上输入三个数,判断能否构成三角形,如不能,给出提示;如能,求出对应三角形的面积,并判断该三角形是锐角三角形还是钝角三角形还是直角三角形,输出对应结论。
#include<stdio.h>
#include<math.h>
main()
{
float a,b,c;
float p,s;
printf("input three numbers as the length of side of a triangle\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 are of the triangle is s:%f\n",s);
if(a*a+b*b==c*c||a*a+c*c==b*b||b*b+c*c==a*a)/*勾股定理*/
printf("the triangle is a right triangle\n");
else if(a*a+b*b-c*c>0&&a*a+c*c-b*b>0&&b*b+c*c-a*a>0)/*余弦定理*/
printf("the triangle is a acute triangle\n");
else
printf("the triangle is a obtuse triangle\n");
}
if (a+b<=c||a+c<=b||b+c<=a)/*不能构成三角形*/
printf("they can not constitute a triangle\n");
}
注意事项
定义数时要定义成精度型,否则可能造成面积不正确;
合理运用海伦公式,勾股定理,余弦定理,三角形三边关系。