我有困难的时候搞清楚什么是错在这里:问题与COUT(C++)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double fact(double);
double sinTaylor(double);
double cosTaylor(double);
int main()
{
double number, sineOfnumber, cosineOfnumber;
cout << "Enter a number, then I will calculate the sine and cosine of this number" << endl;
cin >> number;
sineOfnumber = sinTaylor(number);
cosineOfnumber = cosTaylor(number);
cout << fixed << endl;
cout << cosineOfnumber << endl;
cout << sineOfnumber << endl;
return 0;
}
double fact(double n)
{
double product = 1;
while(n > 1)
product *= n--;
return product;
}
double sinTaylor(double x)
{
double currentIteration, sumSine;
for(double n = 0; n < 5; n++)
{
currentIteration = pow(-1, n)*pow(x, 2*n+1)/fact(2*n+1);
sumSine += currentIteration;
}
return sumSine;
}
double cosTaylor(double y)
{
double currentIteration, sumCosine;
for(double n = 0; n < 5; n++)
{
double currentIteration = pow(-1, n)*pow(y, 2*n)/fact(2*n);
sumCosine += currentIteration;
}
return sumCosine;
}
好了,这里是我的代码。我对它很满意。除了一件事情: sineOfnumber和cosOfnumber,在调用sinTaylor和cosTaylor之后,将在下面的cout行中相互添加,以便相互打印。 换句话说,如果number等于可以说,.7853,1.14将打印在打印余弦号的行中,并且sineOfnumber将正常打印结果。 任何人都可以帮助我确定这是为什么?非常感谢!
带'double'参数的factorial函数和带'double'计数器的for循环都是坏迹象。 – chris 2013-02-12 03:15:32
是的,我知道。老实说,这真的让我感到很难受,我认真地不能让程序做它应该做的事,除非一切都是双重的。这是不正确打印余弦数字的原因吗? – user2063355 2013-02-12 03:18:21
它可能是。这是'pow'功能给你一个难的时间吗?只要确保一个参数是双重的。例如,'pow(-1。,n)' – chris 2013-02-12 03:20:12