2016-12-29 174 views
-2

沒有關注餘弦定律,我的代碼波紋管試圖解決給定的邊和角度。如果還有一種方法,我如何使用cin >>來檢索多個字符,如您所見,我要求用戶輸入'A'或'S'。C++正弦/餘弦計算器

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 
    } 
} 

我會收到弧度而不是度的答案,任何幫助嗎?

+4

你的問題是什麼? – UnholySheep

+0

正弦和餘弦函數使用弧度等級 – eyllanesc

+0

不清楚你對字符輸入的要求,現在的方式很好 –

回答

1

如何從價值角度轉換成弧度

#define PI 3.14159265 
C_in_radian = C * (PI/180.0); // here C is in degree 

修改代碼如下。

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 

請通過本 - Converting Radians to Degrees and vice-versa

如果您想知道如何使用C++編寫asinsin,請參閱asinsin

編輯

據@ G.Sliepen建議,我們應該用M_PI<cmath><math.h>

我們只需要使用:

#define _USE_MATH_DEFINES // for C++ 
#include <cmath> 

OR

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

然後我們可以使用:

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

reference

+0

請不要**'#define PI'自己。大多數時候我看到人們這樣做,他們做錯了。要麼使用''這個宏'M_PI',或者如果在你的平臺上沒有這個''M_PI'',那麼請做一些類似'static const double PI = 4 * atan(1);' –

+0

的確,我注意到了這一點。謝謝你的評論。 –