我想调用一个函数来计算毕达哥拉斯,并看看三角形是否是一个使用三个输入的直角三角形。我对C ++很陌生,并会对此表示感谢:
这是我的代码,它运行但无法正常运行:
#include <iostream>
#include <cmath>
using namespace std;
double pythagorusTheorem(double a, double b, double c);
int main(){
double a;
double b;
double c;
cout << "Write the three sides of the triangle, enter biggest first and shorter sides after: " << endl;
cin >> a >> b >> c;
if (double val=pythagorusTheorem(a,b,c) == true){
cout << "This is a right-angle triangle " << endl;
}
if (double val=pythagorusTheorem(a,b,c) == false) {
cout << "This is not a right angled triangle " << endl;
}
return 0;
}
double pythagorusTheorem(double a, double b, double c){
a = pow(b,2) + pow(c,2);
}
答案 0 :(得分:1)
您不会从SlickReadSideImpl
返回值。这是未定义的行为。另外:
pythagorusTheorem
......真的没有做你期望的事。请记住,if (double val=pythagorusTheorem(a,b,c) == true)
用于比较两个值是否相等,但==
用于为变量赋值。
通过将函数更改为返回true或false的函数,您可以更好地适应,具体取决于您的值是否满足所需条件:
=
然后将您的比较更改为:
bool pythagorasTheorem(double a, double b, double c)
{
return a*a == b*b + c*c;
}
我必须推荐你这个lovely list of C++ books - 你应该特别阅读函数。 This也可能会有所帮助。
答案 1 :(得分:0)
如果你写这样的函数:
bool pythagorasTheorem(double a, double b, double c)
{
a *= a;
b *= b;
c *= c;
return a == b + c || b == c + a || c == a + b;
}
使用@hnefatl解决方案,您不必先进入最长边