C++ 确定C+中的等腰直角三角形+; 如你所知
等腰直角三角形具有直角三角形和等腰三角形特征 例如:C++ 确定C+中的等腰直角三角形+; 如你所知,c++,C++,等腰直角三角形具有直角三角形和等腰三角形特征 例如: int a,b,c; //a,b and c are three triangle edge. cout << "please insert a,b,c" << endl; cin >> a; cin >> b; cin >> c; if(a==b || b==c || c==a){ cout << "isosceles triangl
int a,b,c; //a,b and c are three triangle edge.
cout << "please insert a,b,c" << endl;
cin >> a;
cin >> b;
cin >> c;
if(a==b || b==c || c==a){
cout << "isosceles triangle";
}
else if(a*a==b*b+c*c ||b*b==c*c+a*a || c*c==a*a+b*b)
{
cout<<"right triangle");
}
inta、b、c//a、 b和c是三条三角形边。
库塔;
cin>>b;
cin>>c;
如果(a==b | | b==c | | c==a){
在您的示例中,所有边长,a
、b
和c
都具有类型int
。没有等腰直角三角形可以具有所有整数边长。您可能应该使用float
或double
如果您知道如何从数学角度进行测试,那么在代码中实现它应该非常简单。唯一要记住的是,在大多数情况下,由于舍入错误,浮点数的直接比较是没有意义的
数学等式a2=b2+c2应按如下方式进行检查:
std::abs(a*a - b*b - c*c) < EPSILON
std::abs(a*a-b*b-c*c)
其中,EPSILON
是一些小的数字,可以容忍浮点数的有限精度。根据Evg的评论,下面是一个示例实现:
bool IsRightTriangle(double a, double b, double c)
{
double max_allowed_error = 0.1;
return (std::abs(a*a - b*b - c*c) <= max_allowed_error) || (std::abs(b*b - c*c - a*a) <= max_allowed_error) || (std::abs(c*c - a*a - b*b) <= max_allowed_error) ;
}
bool IsIsosceles(double a, double b, double c)
{
return a == b || b == c || c == a;
}
int main()
{
double a, b, c;
cout << "\nEnter length of first side of the triangle ";
cin >> a;
cout << "\nEnter length of second side of the triangle ";
cin >> b;
cout << "\nEnter length of third side of the triangle ";
cin >> c;
bool iso = IsIsosceles(a, b, c);
bool rt = IsRightTriangle(a, b, c);
if (iso && rt) std::cout << "\n Triangle is a Right Isosceles Triangle\n";
else
if (iso) std::cout << "\n Triangle is an Isosceles Triangle \n";
else
if (rt) std::cout << "\n Triangle is a Right Triangle \n";
}
bool-IsRightTriangle(双a、双b、双c)
{
双最大允许误差=0.1;
返回(std::abs(a*a-b*b-c*c)c;
bool iso=IsIsosceles(a、b、c);
bool-rt=IsRightTriangle(a、b、c);
if(iso&&rt)std::cout“确定等腰直角三角形必须输入平方根数”。这句话是什么意思?我不明白你想在这里实现什么。你可以存储这些“特征”在bools中,如bool isRight=a*a==b*b+c*c | | b*b==c*c+a*a | c*c==a*a+b*b;
然后根据它们的值打印输出。顺便说一句,pow
函数采用浮点参数。您可能希望使用乘法,例如a*a
。由于浮点和整数之间的转换,可能会降低精度@geza表示等腰直角三角形的边最小值为1,1,平方根为2InIsRightTriangle
我建议返回(std::abs(…)@Evg:Edited。谢谢。
bool IsRightTriangle(double a, double b, double c)
{
double max_allowed_error = 0.1;
return (std::abs(a*a - b*b - c*c) <= max_allowed_error) || (std::abs(b*b - c*c - a*a) <= max_allowed_error) || (std::abs(c*c - a*a - b*b) <= max_allowed_error) ;
}
bool IsIsosceles(double a, double b, double c)
{
return a == b || b == c || c == a;
}
int main()
{
double a, b, c;
cout << "\nEnter length of first side of the triangle ";
cin >> a;
cout << "\nEnter length of second side of the triangle ";
cin >> b;
cout << "\nEnter length of third side of the triangle ";
cin >> c;
bool iso = IsIsosceles(a, b, c);
bool rt = IsRightTriangle(a, b, c);
if (iso && rt) std::cout << "\n Triangle is a Right Isosceles Triangle\n";
else
if (iso) std::cout << "\n Triangle is an Isosceles Triangle \n";
else
if (rt) std::cout << "\n Triangle is a Right Triangle \n";
}