定律余弦求解c,但得到奇怪的答案

LawOfCosines solving for c, but getting odd answer

本文关键字:答案 余弦求 定律      更新时间:2023-10-16

我一直在尝试编写一个可以使用余弦定律求解c的程序。程序运行正常,但我得到的答案大得离谱,以科学记数法的方式表示。 这是我的代码:

#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private: 
double a;
double b;
double y;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
A = a;
}
void setb(double B)
{
B = b;
}
void sety(double Y)
{
Y = y;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3);
triangle1.setb(4);
triangle1.sety(60);
cout << "c is equal to " << triangle1.LawOfCos() << endl;

return 0;
}

那里的 cos(( 函数将输入作为弧度而不是度数。

尝试将度数转换为弧度,然后将其作为输入提供。

在类函数 seta、setb 和 sety 中,你写了 A = a、B = b 和 Y = y。 您必须将它们更改为 a = A、b = B 和 Y = y。

因此,在应用所有更改后,代码应该像

#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private: 
double a = 0;
double b = 0;
double y = 0;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void sety(double Y)
{
y = Y*3.14/180;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3.0);
triangle1.setb(4.0);
triangle1.sety(60.0);
cout << "c is equal to " << triangle1.LawOfCos() << endl;

return 0;
}