c - 我应该在条件语句中更改哪些内容才能产生正确的三角形分类?

标签 c ubuntu-16.04

我想让用户输入任意三个值来指示三角形边的尺寸。代码中的条件语句将判断边是否构成三角形。如果它确实形成了一个三角形,它将显示该三角形是不等边三角形、直角三角形还是两者兼而有之。

#include <stdio.h>  

int main(void)  
{  
//Declare the variables for the sides of the triangle  
float a; //first side  
float b; //second side  
float c; //third side  
float scalene;  //check for scalene  
float right; //check for right  
float ans; //check if sides are a triangle  
float rep; //check if user wants to continue after the sides are not a 
triangle  

//Get user inputs for the sides of the triangle  
printf("Input the first side of the triangle: ");  
scanf("%f", &a);  

printf("Input the side for the second side of the triangle: ");  
scanf("%f", &b);  

printf("Input the last side of the triangle: ");  
scanf("%f", &c);  

//Conditional statements:  

//Determine if the sides make up a triangle  
>if ((a+b)<c || (b+c)<a || (a+c)<b)  
>{  
ans=0;  
}  
else  
{  
ans=1;  
}  
//If the sides make up a triangle, is the triangle scalene? If scalene, 
//each side is unique to the others  
for (ans=1;ans<3;ans++)  
{  
if (a==b || a==c || b==c)  
{  
scalene=0;  
>}  
else  
{  
scalene=1;  
}  
//if the sides make a right triangle, they would satisfy one of the 
//following Pythagorean theorem  
if ((a*a+b*b)==(c*c) || (b*b+c*c)==(a*a) || (a*a+c*c)==(b*b))  
{  
right=1;  
}  
else  
{  
right=0;  
}  

for (ans=0;ans<2;ans++)  
>{  
printf("Your sides do not make a triangle. Enter 1 if you would like to 
input new values. Enter any other number to finish: \n");  
scanf("%f", &rep);  
}  
if (rep==1)  //repeat the steps again  
{  
printf("Input the first side of the triangle: ");  
scanf("%f", &a);  

printf("Input the side for the second side of the triangle: ");  
scanf("%f", &b);  

printf("Input the last side of the triangle: ");  
scanf("%f", &c);  
>}  
else  
{  
printf("Thank you for using Triangle Check. Have a nice day!");  
return 0;  
}  
}  

//Display the results to the user  
if (scalene==1 && right==1)  
{  
printf("Your triangle is both a scalene and right!");  
return 0;  
}  
else if (scalene==0 && right==1)  
{  
printf("Based on your sides, it is a right triangle!");  
return 0;  
}  
else if (scalene==1 && right==0)  
{  
printf("Based on your sides, it is a right triangle!");  
return 0;  
}  
else  
{  
printf("Your triangle is neither right or scalene.");  
return 0;  
}  


 return 0;  
 }  

如果我输入 3, 4, 5 作为边,我应该得到这个三角形构成一个直角三角形。

但我知道边不构成三角形。

最佳答案

你的逻辑很困惑,所以你很困惑。摆脱 ans——你不需要它。构建您的程序以立即对事物做出响应。摆脱你不需要的循环。 (这个程序根本不需要任何循环。)

例如,当检查是否是三角形时,检查,如果失败,退出

if ((a+b)<c || (b+c)<a || (a+c)<b)
{
  puts( "Alas, your sides to not make a triangle." );
  return 1;
}

可以通过再次运行程序来让用户再次尝试。 (是的,您可以这样做,以便它再次询问,但这会增加现在不需要的复杂性。专注于给您的任务。)

如果代码继续经过这个点,那么您就知道它一定是一个三角形。下一个技巧是测试三角形的类型。例如:

if (a != b && b != c)
{
  puts( "The triangle is SCALENE." );
}

并继续下一个测试。

Flaggy 程序(您的程序使用 ansscaleneright 等,除了报告真值之外什么也不做)对您没有帮助。避免变得棘手。

祝你好运!

关于c - 我应该在条件语句中更改哪些内容才能产生正确的三角形分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47743715/

相关文章:

c - 警告 : comparison of unsigned expression >= 0 is always true

c - 在应用程序中使用Lua作为静态库和Lua加载模块

ubuntu-16.04 - E : Package 'oracle-java8-installer' has no installation candidate

ubuntu - apt-get update 无法获取存储库

php - 特定表上的 LEFT JOIN 速度极慢

Ubuntu timedatectl 在 Docker 容器中失败

c++ - 无法将 libc++ 与 clang++-5.0 一起使用

c - 如果 if 语句重复为真直到为假,则只增加计数器 (++) 一次?

c - 优化从 AVX2 寄存器中提取 64 位值

c - 更好的方法是什么?