编程实现:输入一个角度值x,计算该角度的余弦值并输出。本题不能调用系统函数。

编程实现:输入一个角度值x,计算该角度的余弦值并输出。本题不能调用系统函数。要求使用泰勒公式,截断误差小于十的七次方
(注意:不用手动输入精度!不手动输入精度!)
(注意:要根据下图的提示写!根据下图提示!看图啊!看图!)

img

img


img

完全按照你的题目要求和提示来写的

#include <iostream>

#define PI 3.141593

double MyCos(double d){
    double x = (PI / 180) * d;
    double sum = 0;
    double term = 1;
    int n = 0;
    do{
        if(n % 2){
            sum -= term;
        }else{
            sum += term;
        }
        n ++;
        term = term * x * x / (2 * n) / (2 * n - 1);        
    }while(term >= 1e-7);
    return sum;
}

int main(){
    double x,y;
    std::cout << "请输入一个角度值: ";
    std::cin >> x;
    y = MyCos(x);
    std::cout << "cos(" << x << ")=" << y << std::endl;    
    return 0;
}
#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    double angle,x;
    const double pi=3.1415926535;
    
    cin>>x;
    
    angle=x*pi/180; 
    double cosine=0, term=1;
    for(int i=0;fabs(term)>=1e-7;i+=2){
        cosine+=term;
        term=-term*angle*angle/(i+2)/(i+1);
    }
    
    cout<<cosine<<endl; 
    return 0;
}

基于new Bing加以自己编写后的回答:

img

img

img

img


#include <iostream>
#include <cmath>
using namespace std;

// 子函数,计算余弦值
double cosine(double degree) {
    // 将角度值转换为弧度值
    double radian = degree * 3.141593 / 180;
    // 初始化通项
    double term = 1;
    // 初始化和
    double sum = 0;
    // 初始化 n
    int n = 0;
    // 当通项的绝对值大于 10^(-7) 时,继续计算
    while (abs(term) > 1e-7) {
        // 累加通项
        sum += term;
        // 增加 n 的值
        n++;
        // 计算下一个通项
        term *= -radian * radian / (2 * n - 1) / (2 * n);
    }
    // 返回余弦值
    return sum;
}

// 主函数
int main() {
    // 定义角度值变量
    double degree;
    // 输入角度值
    cout << "请输入一个角度值: ";
    cin >> degree;
    // 调用子函数计算余弦值并输出结果
    cout << "cos(" << degree << ")=" << cosine(degree) << endl;
    return 0;
}