2015-02-17 36 views
-6

我找不出反餘弦部分來使它工作。或者我犯了更多的錯誤。在C++ visual studio中的cos定律

using namespace std;

int main() 

{ 

    double a,b,c,angle,acos; 

    cout << "Enter the value of a"<<endl; 
    cin >>a; 

    cout << " Enter the value of b "<<endl; 
    cin >> b; 

    cout << "Enter the value of c " <<endl; 
    cin >> c; 

    angle = acos*(-0.6481) * ((a*a+b*b-c*c)/2*a*b); 

    cout << "The angle of cos is " <<angle<< endl; 

    system ("pause"); 

} 
+2

不知道你在問什麼,但是當你第一次使用它時,'acos'是未初始化的。 – juanchopanza 2015-02-17 23:12:56

+0

我正在尋找角度。 – 2015-02-17 23:15:04

+0

如果'acos'初始化爲0,則角度將爲0。 – 2015-02-17 23:19:59

回答

0

law of cosines告訴我們的是,如果A,B,C是一個三角形的三個邊的長度,所述邊緣的角度的面向c中的餘弦,是:

cos(angle) = (a² + b² - c²)/2ab // mathematical equation, not c++ code !) 

的cosinus的反函數是arccosinus,即在C++庫中的acos()。所以我猜你的公式應該是:

angle = acos((a*a+b*b-c*c)/2*a*b); 

這個結果用弧度表示。明知PI弧度爲180度,則可以將此轉換爲度:

#include <cmath> 
... 
const double pi=3.14159265; 
double angle_in_deg = acos((a*a+b*b-c*c)/2*a*b) * 180/pi; 

請注意,你已經在你原來的代碼中使用的語法定義acos是一個變量(這仍然unititalized)。這就是爲什麼原始公式不能給出任何正確的結果。