work2.cpp
来自「作业: 牛顿插值法 运行环境:vc 程序语言:c++ 程序源代码:work」· C++ 代码 · 共 68 行
CPP
68 行
// work2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "iostream.h"
#define MAX_N 20
struct point_type
{
double x;
double y;
}point[MAX_N+1];
int main(int argc, char* argv[])
{
int n;
int i,j;
double cs[MAX_N+1];
double x,temp,newton;
//输入数据的个数
cout<<"输入点个数n(从零开始记数):";
cin>>n;
//确认输入的数在正确输入范围
while ((n>MAX_N)||(n<0))
{
cout<<"输入点个数必须在范围 1 到"<<MAX_N<<endl;
cout<<"重新输入n:";
cin>>n;
}
//输入数据
cout<<"输入x_i,y_i,i=0.."<<n<<": "<<endl;
for (i=0;i<=n;i++)
{
cin >>point[i].x>>point[i].y;
}
cout<<"输入要求的x : "<<endl;
cin>>x;
//初始化cs[i]
for (i=0;i<=n;i++)
{
cs[i]=point[i].y;
}
//求出最高阶差商
for(i=0;i<n;i++)
{
for(j=n;j>i;j--)
{
cs[j]=(cs[j]-cs[j-1])/(point[j].x-point[j-1-i].x );
}
}
temp=1;
newton=cs[0];
for (i=0;i<n;i++)
{
temp=temp*(x-point[i].x);
newton=newton+temp*cs[i+1];
}
cout<<"newton("<<x<<")="<<newton;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?