C ++ Sin / Cos计算器

时间:2016-12-29 22:18:46

标签: c++ c++11 sin

不关注余弦定律,我的代码试图解决给定的边和角度。如果还有办法,我该如何使用cin>>检索多个字符,如您所见,我要求用户输入“A' A'或者' S.'

Here is my code and the results

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    char solvefor;
    double A; // I used doubles to have specific answers
    double a;
    double B;
    double b;
    double C;
    double c;

    cout << "What are you trying to solve? <ASA(A) or SSA(S)>";
    cin >> solvefor;
    if (solvefor == 'S')
    {
        cout << "What is the value of b?" << endl;
        cin >> b;
        cout << "What is the angle B?" << endl;
        cin >> B;
        cout << "What is the side c?" << endl;
        cin >> c;
        C = asin((sin(B)/b) * c);
        cout << "Your missing C angle is " << C << endl;
        A = 180 - C - B;
        cout << "Your missing A angle is " << A << endl;
        a = (sin(A) *b) / sin(B);
    } else {
        return 0;       //I will work on law of cosine later
    } 
} 

我会以弧度而不是学位得到答案,有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

如何将值从度数转换为弧度

C = asin((sin(B)/b) * c);
double C_raidan = C * (PI / 180.0);
cout << "Your missing C angle is " << C_raidan << endl;
A = 180 - C - B;
double A_radian = A * (PI / 180.0);
cout << "Your missing A angle is " << A_radian << endl;

按如下方式修改您的代码。

What are you trying to solve? <ASA(A) or SSA(S)> S
What is the value of b?
8
What is the angle B?
31
What is the side c?
13
Your missing C angle is -0.0125009
Your missing A angle is 2.61304

输出:

asin

请仔细阅读 - Converting Radians to Degrees and vice-versa

如果您想了解sinM_PI在C ++中的工作原理,请参阅asinsin

修改

根据@ G.Sliepen建议,我们应该使用<cmath><math.h>中的#define _USE_MATH_DEFINES // for C++ #include <cmath>

我们只需要使用:

#define _USE_MATH_DEFINES // for C  
#include <math.h> 

OR

double C_raidan = C * (M_PI / 180.0); // M_PI is a math constant

然后我们可以使用:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccentBrightOpaque"/>
<padding
    android:bottom="0dip"
    android:left="0dip"
    android:right="0dip"
    android:top="0dip"/>
</shape>

请参阅reference