Home > Back-end >  C language subject, a great god for help
C language subject, a great god for help

Time:09-27

CodePudding user response:

E//input precision, use the following formula for cosine approximation of x, accurate to the last item on the absolute value is less than e,
//Cos (x)=0 x0/! - x2/2! + 4 x4/! - the x6/6! +...
//for x n and o factorial and cosine function implementation, using a custom function
//in this case as a reference code, it is necessary to perfect the mycos, the main function
#include
#include
Int fac (int n);//factorial function declaration
Double CF (double x, int n);//x ^ n power to the power function declaration
Double mycos (double x, double e);//custom cosine function declaration
//factorial function definition
Int fac (int n)
{
int i;
Double fact=1;
for(i=1; I<=n; I++) fact *=I;
Return the fact;
}
//x ^ n power to the power function definition
Double CF (double x, int n)
{
Double f=1;
If (n> 0)
For (; n> 0; F *=n -) x;
Else if (n<0)
For (; N<0; N++)=f/x;
Return the f;
}
//custom cosine function x for Angle value, e for precision values
Double mycos (double x, double e) {
Double m, w=0;
Int c=0;
M=x * 3.1415926/180;//Angle radian
While (CF/fac (m, 2 * c) (2) * c & gt;=e)//accuracy limit
{
W +=CF (1, c) * CF (m, 2 * c)/fac (2 * c);//series summation
c++;
}//series summation, satisfies the requirement of accuracy of the e end
Return w;//returns the cosine value of the return
}
//the main function test custom cosine function
Int main ()
{
Double x, x0, e, g=0;
Int n=0;
Printf (" testing custom cosine function mycos \ n ");
Printf (" please enter the Angle value with accuracy, separated by a space \ n ");
Lf the scanf (" % % lf ", & amp; X, & amp; e);
X0=x * 3.1415926/180;//Angle radian

While (CF (x0, 2 * n)/fac (2 * n) & gt; E)//accuracy limit
{
G +=CF (1, n) * CF (x0, 2 * n)/fac (2 * n);//series summation
N++;
}
//display custom cosine function and system function value, comparing input the e=0.001, 0.000001
Printf (" mycos lf (lf) %. 1=% \ \ t tcos lf (lf) %. 1=% \ n ", x, g, x, cos (x0));
return 0;
}
The code here!!!!!!

CodePudding user response:

What mean, custom cosine function, modify the mycos and main, look at what you want?
//factorial function definition 
Int fac (int n)
{
int i;
Double fact=1;
for(i=1; I<=n; I++) fact *=I;
Return the fact;
}
//x ^ n power to the power function definition
Double CF (double x, int n)
{
Double f=1;
If (n> 0)
For (; n> 0; F *=n -) x;
Else if (n<0)
For (; N<0; N++)=f/x;
Return the f;
}
//custom cosine function x for Angle value, e for precision values
Double mycos (double x, double e) {
X=x * 3.1415926/180;//Angle radian
Double c=0, s=0, f=1, k;
While ((k=CF/fac (x, c) (c)) & gt;=e)//accuracy limit
{
S +=f * * k;//series summation
C +=2;
F *=1;
}//series summation, satisfies the requirement of accuracy of the e end
return s;//returns the cosine value of the return
}
//the main function test custom cosine function
Int main ()
{
Double x, x0, e;
Printf (" testing custom cosine function mycos \ n ");
Printf (" please enter the Angle value with accuracy, separated by a space \ n ");
Lf the scanf (" % % lf ", & amp; X, & amp; e);
X0=x * 3.1415926/180;
Printf (" mycos lf (lf) %. 1=% \ \ t tcos lf (lf) %. 1=% \ n ", x, mycos (x, e), x, cos (x0));
return 0;
}

CodePudding user response:

This is the source code, sent you a wrong on the second floor,,,

Cos/* o * x approximation of the modular design/
E//input precision, use the following formula for cosine approximation of x, accurate to the last item on the absolute value is less than e,
//Cos (x)=0 x0/! - x2/2! + 4 x4/! - the x6/6! +...
//for x n and o factorial and cosine function implementation, using a custom function
//in this case as a reference code, it is necessary to perfect the mycos, the main function
#include
#include
Int fac (int n);//factorial function declaration
Double CF (double x, int n);//x ^ n power to the power function declaration
Double mycos (double x, double e);//custom cosine function declaration
//factorial function definition
Double fac (int n)
{
int i;
Double fact=1;
for(i=1; I<=n; I++) fact *=I;
Return the fact;
}
//x ^ n power to the power function definition
Double CF (double x, int n)
{
Double f=1;
If (n> 0)
For (; N> 0; F *=n -) x;
Else if (n<0)
For (; N<0; N++)=f/x;
Return the f;
}
//custom cosine function x for Angle value, e for precision values
Double mycos (double x, double e) {
//Angle radian
//series summation, satisfies the requirement of accuracy of the e end
//returns the cosine value of the return
}
//the main function test custom cosine function
Void main ()
{
Double x, x0, e, g=0;
Int n=0;
Printf (" testing custom cosine function mycos \ n ");
Printf (" please enter the Angle value with accuracy, separated by a space \ n ");
Lf the scanf (" % % lf ", & amp; X, & amp; e);
X0=x * 3.1415926/180;//Angle radian

While (CF (x0, 2 * n)/fac (2 * n) & gt; E)//accuracy limit
{
G +=CF (1, n) * CF (x0, 2 * n)/fac (2 * n);//series summation
N++;
}
//display custom cosine function and system function value, comparing input the e=0.001, 0.000001
Printf (" mycos lf (lf) %. 1=% \ \ t tcos lf (lf) %. 1=% \ n ", x, g, x, cos (x0));
}
  • Related