LawOfCosines 求解 c,但得到奇怪的答案
LawOfCosines solving for c, but getting odd answer
我一直在尝试编写一个可以使用余弦定律求解 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() 函数将输入作为弧度而不是度数。
尝试将度数转换为弧度,然后将其作为输入提供。
在 class 函数 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;
}
我一直在尝试编写一个可以使用余弦定律求解 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() 函数将输入作为弧度而不是度数。
尝试将度数转换为弧度,然后将其作为输入提供。
在 class 函数 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;
}