1 #include2 #include 3 4 double funcos(double e, double x); 5 6 int main() 7 { 8 double e, x; 9 10 scanf_s("%lf %lf", &e, &x);11 printf("cos(%.2f) = %.6f\n", x, funcos(e, x));12 13 return 0;14 }15 16 double funcos(double e, double x)17 {18 double item = 1;19 double numerator; //分子20 double denominator; //分母21 double result = 0;22 int i = 0;23 double flag = 1.0;24 25 while (fabs(item) > e)26 {27 numerator = 1;28 for (int j = 1; j <= i; j++)29 {30 numerator = numerator * x;31 }32 33 denominator = 1;34 for (int k = 1; k <= i; k++)35 {36 denominator = denominator * k;37 }38 39 item = flag * numerator / denominator;40 41 result = result + item;42 43 flag = -flag;44 45 i = i + 2;46 }47 48 return result;49 50 }