找出三角形是正确的,锐角还是钝角

时间:2015-09-06 18:40:53

标签: c

我必须从三角形的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;
}

但是我一直收到同样的错误&#34;这是一个锐角三角形&#34;对于钝角三角形,如10,10,11

知道我做错了吗?

谢谢

2 个答案:

答案 0 :(得分:2)

我已经简化了你的代码,首先找到最长的一面,然后删除大部分的比较(和括号)。

但最重要的是,你的急性和钝角的方格比较是错误的。对于急性三角形,较小边的正方形的总和大于最长的正方形。

#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,longest;

    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 {

            longest = z;
            if (longest < x) {
                z = longest;
                longest = x;
                x = z;
            }
            if (longest < y) {
                z = longest;
                longest = y;
                y = z;
            }

            if( x * x + y * y == longest * longest ) {
                printf("This is a right-angled triangle.\n");
            } else if( x * x + y * y > longest * longest) {
                printf("This is an acute-angled triangle.\n");
            } else printf("This is an obtuse-angled triangle.\n");
        }
  }

  return 0;
}

答案 1 :(得分:0)

对于边长为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&lt; c ^ 2或b ^ 2 + c ^ 2&lt; a ^ 2或c ^ 2 + a ^ 2> b ^ 2。