我必须从三角形的3个给定边长度中找出三角形是否正确,锐角或钝角 .
这就是我现在所拥有的:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Program that calculates the type of triangle
int main(int argc, char *argv[]) {
int x,y,z;
printf("Type in the integer lengths of 3 sides of a triangle:\n");
scanf("%d %d %d", &x, &y, &z); //reads the user's inputs
if((x<=0) || (y<=0) || (z<=0)) {
printf("This is not a triangle.\n");
} else {
if((x + y <= z) || (x + z <= y) || (y + z <= x)) {
printf("This is not a triangle.\n");
} else {
if( ((x * x) + (y * y) == (z * z)) || ((x * x) + (z * z) == (y * y)) || ((z * z) + (y * y) == (x * x)) ) {
printf("This is a right-angled triangle.\n");
} else if( ( ((x * x) + (y * y) < (z * z)) || ((x * x) + (z * z) < (y * y)) || ((z * z) + (y * y) < (x * x)) ) || ( ( x<=z && y<=z ) || ( x<=y && z<=y ) || ( y<=x && z<=x ) ) ) {
printf("This is an acute-angled triangle.\n");
} else if( ( ((x * x) + (y * y) > (z * z)) || ((x * x) + (z * z) > (y * y)) || ((z * z) + (y * y) > (x * x)) ) || ( ( x>z && y>z ) || ( x>y && z>y ) || ( y>x && z>x ) ) ) {
printf("This is an obtuse-angled triangle.\n");
} else {
printf("Not a triangle\n");
}
}
}
return 0;
}
但我仍然收到同样的错误“这是一个锐角三角形”,对于像10,10,11这样的钝角三角形
知道我做错了什么吗?
谢谢
2 回答
我已经简化了你的代码,首先找到最长的一面,并删除大部分的比较(和括号) .
但最重要的是,你的急性和钝角的方形比较是错误的 . 对于急性三角形,较小边的正方形的总和大于最长的正方形的总和 .
对于边a,b,c的三角形:
为锐角:a ^ 2 b ^ 2> c ^ 2和b ^ 2 c ^ 2> a ^ 2和c ^ 2 a ^ 2> b ^ 2 .
为钝角:a ^ 2 b ^ 2 <c ^ 2或b ^ 2 c ^ 2 <a ^ 2或c ^ 2 a ^ 2> b ^ 2 .