2024-11-29 09:59:30
按照你的要求编写的判断三角形类型的C语言程序如下
#include<stdio.h>
#include<math.h>
float cal_area(float a,float b,float c){
float p,s;
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
int main(){
int i,n;
float a,b,c,e=0.001;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%f%f%f",&a,&b,&c);
if(a+b<=c || a+c<=b || b+c<=a){
printf("no triangle\n");
}else if(fabs(a-b)<e && fabs(b-c)<e){
printf("equilateral triangle,");
printf("%.1f\n",cal_area(a,b,c));
}else if((fabs(a-b)<e || fabs(b-c)<e || fabs(a-c)<e) && (fabs(a*a+b*b-c*c)<e || fabs(a*a+c*c-b*b)<e || fabs(b*b+c*c-a*a)<e)){
printf("isosceles right triangle,");
printf("%.1f\n",cal_area(a,b,c));
}else if(fabs(a-b)<e || fabs(b-c)<e || fabs(a-c)<e){
printf("isosceles triangle,");
printf("%.1f\n",cal_area(a,b,c));
}else if(fabs(a*a+b*b-c*c)<e || fabs(a*a+c*c-b*b)<e || fabs(b*b+c*c-a*a)<e){
printf("right triangle,");
printf("%.1f\n",cal_area(a,b,c));
}else{
printf("general triangle,");
printf("%.1f\n",cal_area(a,b,c));
}
}
return 0;
}
运行结果
3
3.0 2.0 6.0
no triangle
3.0 4.0 5.0
right triangle,6.0
1.0 1.0 1.414
isosceles right triangle,0.5