请问这个C程序怎么遍

来源:百度知道 编辑:UC知道 时间:2024/06/10 23:19:43
编写函数求解公式:s=xf(x)+x^2f (x^2) +x^3f (x^3) +…x^nf (x^n)。其中,x和n都作为函数的参数,s作为函数的返回值。而f(y)=sin(y)/cos(y)。编写主函数,请用户输入x和n,然后调用上面的函数。

#include <math.h>
#include <iostream>

using namespace std;

double f( double x )
{
return sin( x) / cos(x);
}

double s( double x, int n )
{
if( n == 0 )
return 1.0;
return s( x, n - 1 ) + pow( x, n ) * f( x );
}

int main()
{
double x;
int n;
cout << "Input x:" << endl;
cin >> x;
cout << "Input n:" << endl;
cin >> n;
cout << "The result of s is " << s(x,n) << endl;
return 0;
}