# include <iostream>
# include <math.h>
# include <cstdlib>
using namespace std;
double cosin_value(double value);
double sin_value(double value);
double big_degree (double value);
double big_radian (double value);
double x;
double value;
double degree;
double radian;
const double PI = 3.14159;
char choice;
char yes ;
int main()
{
cout << "Please enter an angle value => ";
cin >> value;
cout << "Is the angle in Degree or Radian?" << endl;
cout << "\t" << "Type D if it is in Degree" << endl;
cout << "\t" << "Type R if it is in Radian" << endl;
cout << "Your response => ";
cin >> choice; //degree or radian?
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(10);
if (choice == 'D' || choice == 'd')
{
big_degree (value);
cout << " " << "sin(x) = " << "\t" << sin_value(degree) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(degree) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(degree)/cosin_value(degree) << endl;
}
else if (choice == 'R' || choice == 'r')
{
cout << " " << "sin(x) = " << "\t" << sin_value(radian) << endl;
cout << " " << "cos(x) = " << "\t" << cosin_value(radian) << endl;
cout << " " << "tan(x) = " << "\t" << sin_value(radian)/cosin_value(radian) << endl;
}
return 0;
}
// Sine,cosine functions
// angle -360<value<360
double sin_value(double value)
{
int count=1;
double sine, num, dem, sign, term;
sine=0;
sign = 1;
num = value;
dem = count;
while (count <= 20)
{
term = (num/dem);
sine = sine + term*sign;
num = num*value*value;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}
return (sine);
}
double cosin_value(double value)
{
int count=0;
double cosine, num, dem, sign, term;
cosine=0;
sign = 1;
num = 1;
dem = 1;
while (count <= 20)
{
term = (num/dem);
cosine = cosine + term*sign;
num = num*value*value;
count = count + 2;
dem = dem * count * (count-1);
sign = -sign;
}
return (cosine);
}
double big_degree (double value)
{
int result;
const int angle=360;
if (value >= 360 || value <= -360)
{
result=value/angle;
degree=(value-(result* angle))*PI/180;
}
else
{
degree = (value*PI)/180;
}
return (degree);
}
double big_radian (double value)
{
int result;
if (value >= 2*PI || value <= -2*PI)
{
result=value/(2*PI);
radian=(value-(result* 2*PI));
}
else
{
radian = value;
}
return (radian);
}
嗨,這是基本上整個程序我使用我用C知道的程度的知識計算三角函數值寫++作爲初學者。爲了更好的觀點,你可以參考以上關於我的代碼的鏈接:codepad.org 從114行開始的行是我創建的函數。那裏有一個問題,當數值爲90度或pi/2弧度時,如何計算我的cosx爲0? 因爲程序仍然會計算坦克對我來說甚至是90度的值。 讓我們給予價值90度的程序說,它會給我的0.0000013268,而不是0.000000 遺憾的價值,因爲我只是一個初學者,代碼看起來怪異你們。 我感謝你的指導! 雙big_degree(雙精度值)當值是> = 360或< = -360 *計算COS(X)爲0時x是90度或pi/2弧度在C++
你爲什麼希望我們有100多行的代碼,當你涉水通過不準備將問題縮小一點? –
你應該包括math.h和使用** #define M_PI \t \t 3.14159265358979323846 **從它 – Marichyasana