# 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 度,该程序仍会为我计算tanx 。假设通过给程序赋予90 度的值,它会给我0.0000013268而不是0.000000的值 对不起,因为我只是一个初学者,代码对你们来说看起来很奇怪。我很感激你的指导! double big_degree(double value)表示当值 >= 360 或 <= -360*