为什么我的 valid_triangle 来自 cs50 的程序不起作用?

Why is my valid_triangle program from cs50 doesn't work?

我对这段代码有疑问,因为我似乎找不到问题所在?这是我试图解决的问题: - 声明并编写一个名为 valid_triangle 的函数,该函数以代表三角形三边长度的三个实数作为参数,并输出 true 或 false,具体取决于这三个长度是否能够组成一个三角形。

//includes
#include <stdio.h>
#include <cs50.h>

bool valid_triangle(float x, float y, float z);

int main (void)
{
   float x = get_float("x:");
   float y = get_float("y:");
   float z = get_float("z:");
   bool w = valid_triangle(x,  y,  z);
}

bool valid_triangle(float x, float y, float z)
{
    // only positive sides
    if (x <= 0 || y <= 0 || z <= 0)
    {
        return false;
        printf("false\n");
    }

    // sum of the lengths of any two sides of the triangle must be greater than the length of the third side.
    else if (x + y < z || x + z < y || y + z < x)
    {
        return false;
        printf("false\n");
    }
    else
    {
        return true;
        printf("True\n");
    }

    return 0;
}

对于初学者来说,这个 if 语句

else if (x + y < z || x + z < y || y + z < x)

不正确,应该是

else if (x + y <= z || x + z <= y || y + z <= x)

以及 return 语句之后的语句,如此代码片段

    return false;
    printf("false\n");

没有效果。看来你的意思是

    printf("false\n");
    return false;

虽然 printf 的调用不应该在函数内部。

您可以在 main 中只调用一次 printf

bool w = valid_triangle(x,  y,  z);
printf( "%s\n", w == false ? "False" : "True" );

也是函数中的最后一个 return 语句

return 0;

是多余的。

这是一个演示程序。

#include <stdio.h>
#include <stdbool.h>

bool valid_triangle(float x, float y, float z)
{
    // only positive sides
    if (x <= 0 || y <= 0 || z <= 0)
    {
        return false;
    }
    // sum of the lengths of any two sides of the triangle must be greater 
    // than the length of the third side.
    else if ( x + y <= z || x + z <= y || y + z <= x)
    {
        return false;
    }
    else
    {
        return true;
    }
}

int main(void) 
{
    float a = 4.0f, b = 5.0f, c = 6.0f;
    printf( "%.1f, %.1f, %.1f are sides of a triangle - %s\n", a, b, c,
            valid_triangle( a, b, c ) ? "true" : "false" );
            
    return 0;
}

程序输出为

4.0, 5.0, 6.0 are sides of a triangle - true

// 此代码应作为 valid_triangle 程序的解决方案。希望对你有帮助

#include <cs50.h>
#include <stdio.h>

//先声明函数,最后分号

bool valid_triangle(float x, float y, float z);

int main(void)
{
 // Ask users for inputs
 float a = get_float("Give me the first integer to form a triangle:  ");
 float b = get_float("Give me the second integer to form a triangle:  ");
 float c = get_float("Give me the third integer to form a triangle:  ");


// function call
bool result=valid_triangle(a, b, c);

//set condition for your boolean variable 'result'
 if (result == true)
 {
  printf("True \n");
 }
else
 {
 printf("False \n");
 }

}


//function definition, without any semi-colon at the end

bool valid_triangle(float x, float y, float z)
{
//checking if any of the sides is negative or equal to zero
if (x<=0 || y<=0 || z<=0)
{
    return false;
}

// sum of the lengths of any two sides of the triangle must be greater than the length 
//of the third side.

else if ((x+y<=z) || (x+z<=y) || (z+y<=x))
{
    return false;
 }
 return true;
}