我正在与C++的任务。基本上我采取双方,假设他们的较小的一面或斜边,然后用功能吐出剩余的一面。看起来很简单,我99%确定我的数学是正确的功能部分,但我不断得到奇怪的大答案。代码来计算毕达哥拉斯定理
#include<iostream>
#include<cmath>
using namespace std;
double otherSideLength(double, double);
int main() {
double a_small_side;
double hypotenuse;
double swapStore = 0;
double otherSide = 0;
cin >> a_small_side;
cin >> hypotenuse;
if(a_small_side == hypotenuse){
cout << "ERROR" << endl;
cout << "0";
}
if(a_small_side < hypotenuse){
otherSide = otherSideLength(a_small_side, hypotenuse);
cout << otherSide;
}
if(a_small_side > hypotenuse){
swapStore = a_small_side;
a_small_side = hypotenuse;
hypotenuse = swapStore;
otherSide = otherSideLength(a_small_side, hypotenuse);
cout << otherSide;
}
}
double otherSideLength(double a_small_side, double hypotenuse){
//a^2+b^2=c^2,
//b^2 = c^2 + a^2
//b = sqrt(c^2 + a^2)
double b = sqrt(pow(hypotenuse, 2) + pow(a_small_side, 2));
return b;
}
如果有人想快速浏览一下,那很棒。
当您移动^ 2到方程的右手侧应该是负的。换句话说,当你应该减去^ 2时,你正在添加^ 2。 – csmckelvey
“但我不断得到奇怪的大答案” - 比如?小心分享?示例应该是*问题的一部分*。 –
请不要将“已解决”添加到标题中。只要接受帮助你的答案。 –