计算sinx的值,c语言编程问题
发布网友
发布时间:2022-04-28 11:41
我来回答
共4个回答
热心网友
时间:2023-10-07 00:16
#include<iostream.h>
////////////////////////
//计算x^n //
//////////////////////
double fun1(int x,int n)
{ if(n<0)
exit(-1);
else if(n<=1)
return (x);
else
return x*fun1(x,n-1);
}
////////////////////////////////////
///计算 n! /
double fun2(int n)
{
if(n<0)
exit(-1);
else if(n<=1)
return (1);
else
return (n*fun2(n-1));
}
////////////////////////////////////
main()
{
int x,n; double s,f=0;
cout<<"输入x的值 和 n 的值;";
cin>>x>>n;
for(int i=1,j=1;i<=2*n-1;i=i+2,j=-j)
{
s=fun1(x,i)/fun2(i);
f+=j*s;
} cout<<"sin x= "<<f<<endl;
}
热心网友
时间:2023-10-29 01:14
#include<iostream.h>
////////////////////////
//计算x^n //
//////////////////////
double fun1(int x,int n)
{ if(n<0)
exit(-1);
else if(n<=1)
return (x);
else
return x*fun1(x,n-1);
}
////////////////////////////////////
///计算 n! /
double fun2(int n)
{
if(n<0)
exit(-1);
else if(n<=1)
return (1);
else
return (n*fun2(n-1));
}
////////////////////////////////////
main()
{
int x,n; double s,f=0;
cout<<"输入x的值 和 n 的值;";
cin>>x>>n;
for(int i=1,j=1;i<=2*n-1;i=i+2,j=-j)
{
s=fun1(x,i)/fun2(i);
f+=j*s;
} cout<<"sin x= "<<f<<endl;
}
热心网友
时间:2023-10-29 01:14
#include
<iostream>
#include
<math.h>
#include
<conio.h>
int
fact(int
n)
{
if(n
==
0)
return
1;
return
n
*
fact(n
-
1);
}
double
mysin(double
x)
{
const
double
PRECISION
=
100.;
double
sum
=
x,
preTerm
=
x;
for(int
i
=
1;;
i++)
{
double
currentTerm
=
(i
%
2
==
1
?
-1
:
1)
*
pow(x,
(2
*
i
+
1))
/
fact(2
*
i
+
1);
sum
+=
currentTerm;
if(fabs(preTerm
+
currentTerm)
*
PRECISION
>
1.)
preTerm
=
currentTerm;
else
break;
}
return
sum;
}
int
main(int
argc,
char*
argv)
{
std::cout
<<
mysin(3.14
/
2)
<<
std::endl
<<
sin(3.14
/
2)
<<
std::endl;
getch();
return
0;
}
热心网友
时间:2023-10-07 00:17
#include
<iostream>
#include
<math.h>
#include
<conio.h>
int
fact(int
n)
{
if(n
==
0)
return
1;
return
n
*
fact(n
-
1);
}
double
mysin(double
x)
{
const
double
PRECISION
=
100.;
double
sum
=
x,
preTerm
=
x;
for(int
i
=
1;;
i++)
{
double
currentTerm
=
(i
%
2
==
1
?
-1
:
1)
*
pow(x,
(2
*
i
+
1))
/
fact(2
*
i
+
1);
sum
+=
currentTerm;
if(fabs(preTerm
+
currentTerm)
*
PRECISION
>
1.)
preTerm
=
currentTerm;
else
break;
}
return
sum;
}
int
main(int
argc,
char*
argv)
{
std::cout
<<
mysin(3.14
/
2)
<<
std::endl
<<
sin(3.14
/
2)
<<
std::endl;
getch();
return
0;
}
热心网友
时间:2023-10-07 00:17
#include <iostream>
#include <math.h>
#include <conio.h>
int fact(int n) {
if(n == 0) return 1;
return n * fact(n - 1);
}
double mysin(double x) {
const double PRECISION = 100.;
double sum = x, preTerm = x;
for(int i = 1;; i++) {
double currentTerm = (i % 2 == 1 ? -1 : 1) * pow(x, (2 * i + 1)) / fact(2 * i + 1);
sum += currentTerm;
if(fabs(preTerm + currentTerm) * PRECISION > 1.) preTerm = currentTerm;
else break;
}
return sum;
}
int main(int argc, char* argv) {
std::cout << mysin(3.14 / 2) << std::endl << sin(3.14 / 2) << std::endl;
getch();
return 0;
}
热心网友
时间:2023-10-07 00:18
这个不就是高等数学里边那个公式么?
15分,好多啊,我要不起哦。哈哈。。。
热心网友
时间:2023-10-07 00:16
#include<iostream.h>
////////////////////////
//计算x^n //
//////////////////////
double fun1(int x,int n)
{ if(n<0)
exit(-1);
else if(n<=1)
return (x);
else
return x*fun1(x,n-1);
}
////////////////////////////////////
///计算 n! /
double fun2(int n)
{
if(n<0)
exit(-1);
else if(n<=1)
return (1);
else
return (n*fun2(n-1));
}
////////////////////////////////////
main()
{
int x,n; double s,f=0;
cout<<"输入x的值 和 n 的值;";
cin>>x>>n;
for(int i=1,j=1;i<=2*n-1;i=i+2,j=-j)
{
s=fun1(x,i)/fun2(i);
f+=j*s;
} cout<<"sin x= "<<f<<endl;
}
热心网友
时间:2023-10-29 01:15
#include <iostream>
#include <math.h>
#include <conio.h>
int fact(int n) {
if(n == 0) return 1;
return n * fact(n - 1);
}
double mysin(double x) {
const double PRECISION = 100.;
double sum = x, preTerm = x;
for(int i = 1;; i++) {
double currentTerm = (i % 2 == 1 ? -1 : 1) * pow(x, (2 * i + 1)) / fact(2 * i + 1);
sum += currentTerm;
if(fabs(preTerm + currentTerm) * PRECISION > 1.) preTerm = currentTerm;
else break;
}
return sum;
}
int main(int argc, char* argv) {
std::cout << mysin(3.14 / 2) << std::endl << sin(3.14 / 2) << std::endl;
getch();
return 0;
}
热心网友
时间:2023-10-29 01:15
这个不就是高等数学里边那个公式么?
15分,好多啊,我要不起哦。哈哈。。。
热心网友
时间:2023-10-07 00:17
#include
<iostream>
#include
<math.h>
#include
<conio.h>
int
fact(int
n)
{
if(n
==
0)
return
1;
return
n
*
fact(n
-
1);
}
double
mysin(double
x)
{
const
double
PRECISION
=
100.;
double
sum
=
x,
preTerm
=
x;
for(int
i
=
1;;
i++)
{
double
currentTerm
=
(i
%
2
==
1
?
-1
:
1)
*
pow(x,
(2
*
i
+
1))
/
fact(2
*
i
+
1);
sum
+=
currentTerm;
if(fabs(preTerm
+
currentTerm)
*
PRECISION
>
1.)
preTerm
=
currentTerm;
else
break;
}
return
sum;
}
int
main(int
argc,
char*
argv)
{
std::cout
<<
mysin(3.14
/
2)
<<
std::endl
<<
sin(3.14
/
2)
<<
std::endl;
getch();
return
0;
}
热心网友
时间:2023-10-07 00:17
#include <iostream>
#include <math.h>
#include <conio.h>
int fact(int n) {
if(n == 0) return 1;
return n * fact(n - 1);
}
double mysin(double x) {
const double PRECISION = 100.;
double sum = x, preTerm = x;
for(int i = 1;; i++) {
double currentTerm = (i % 2 == 1 ? -1 : 1) * pow(x, (2 * i + 1)) / fact(2 * i + 1);
sum += currentTerm;
if(fabs(preTerm + currentTerm) * PRECISION > 1.) preTerm = currentTerm;
else break;
}
return sum;
}
int main(int argc, char* argv) {
std::cout << mysin(3.14 / 2) << std::endl << sin(3.14 / 2) << std::endl;
getch();
return 0;
}
热心网友
时间:2023-10-07 00:18
这个不就是高等数学里边那个公式么?
15分,好多啊,我要不起哦。哈哈。。。