实验5-10 使用函数求余弦函数的近似值 (15 分)
作者:互联网
#include <stdio.h> #include <math.h> double funcos(double e, double x); int main() { double e, x; scanf("%lf %lf", &e, &x); printf("cos(%.2f) = %.6f\n", x, funcos(e, x)); system("pause"); return 0; } /* 你的代码将被嵌在这里 */ double funcos(double e, double x) { double item = 1.0, cosx = 1.0, fenzi = 1.0, fenmu = 1.0; int flag = -1; for (int i = 2; fabs(item) >= e; i += 2) { //fabs(),求出函数的绝对值 fenmu *= (i * (i - 1)); fenzi *= (x * x); item = 1.0 * flag * fenzi / fenmu; cosx += item; flag *= -1; } return cosx; }
fenmu *= (i * (i - 1)); fenzi *= (x * x); item = 1.0 * flag * fenzi / fenmu;
重点!!!
标签:10,1.0,函数,double,item,flag,fenmu,15,fenzi 来源: https://www.cnblogs.com/Robert-De-Niro/p/15955527.html