示例代码如下:
#include
#include
int t_style(int a,int b,int c)
{
int tmp;
if(a<=0 || b<=0 || c<=0) //如果边长有非正值
return -2;
if(a == b && b==c)
return 1;
//先对a,b,c进行从小到大排序,方便判断
if(a>b){
tmp=a; a=b; b=tmp;
}
if(a>c){
tmp=a; a=c; c=tmp;
}
if(b>c){
tmp=b; b=c; c=tmp;
}
if(a+b > c){
if(a==b || b==c || a==c)
return 2;
else if(a*a + b*b == c*c)
return 3;
else
return 4;
}
else
return -1;
}
float t_area(int a,int b,int c)
{
float s,area;
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
int main()
{
int x,y,z;
printf("输入边长: ");
scanf("%d%d%d",&x,&y,&z);
switch(t_style(x,y,z))
{
case 1:
printf("等边三角形\n"); break;
case 2:
printf("等腰三角形\n"); break;
case 3:
printf("直角三角形\n"); break;
case 4:
printf("一般三角形\n"); break;
case -1:
printf("边长为正但不能构成三角形\n"); break;
case -2:
printf("边长有负值\n");
}
if(t_style(x,y,z) > 0)
printf("三角形面积= %.2f\n",t_area(x,y,z));
return 0;
}
示例运行结果: