c ++ LawOfCosines解决c,但得到奇怪的答案

问题描述 投票:0回答:1

我一直在尝试编写可以使用余弦定律解决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;
    }

`

c++ trigonometry
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.