-2
# 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*

4

3 回答 3

4

我没有在我的大脑中为 pi 的数字分配任何堆空间,但我确实记得.atan(1) == pi / 4

像这样改变你的PI常数:

const double PI = atan(1) * 4;

拿你的代码,做出改变,我明白了

Please enter an angle value => 90
Is the angle in Degree or Radian?
    Type D if it is in Degree
    Type R if it is in Radian
Your response => d
 sin(x)   =     1.0000000000
 cos(x)   =     0.0000000000
 tan(x)   =     15555226593901466.0000000000
于 2013-07-23T04:54:14.530 回答
0

问题不在于您的代码。您提供的输入不够准确。计算 pi/2 的正确值,您将得到一个足够准确的值。

此外,如果你想四舍五入你可以使用

#rounded off value=Math.Round(#old value, 4)

于 2013-07-23T04:29:13.740 回答
0
const double PI = 3.14159;

这个定义越精确0,get 的值就越接近cos PI/2

如果您以弧度获取输入本身,则也适用相同的标准。

于 2013-07-23T04:32:55.067 回答