⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lagrange.cpp

📁 Lagrange插值
💻 CPP
字号:
//Lagrange插值法
#include <iostream.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>


double Lagrange (double *p, double x, int n)
{
	int i, j;
	double temp1, temp2 = 0;

	for (i = 0; i < n; i++)
	{
		temp1 = 1;
		for(j = 0; j < n; j++)
		{
			if(j != i)
			{
				temp1 *= (x-p[2*j]) / (p[2*i]-p[2*j]);
			}
		}

		temp2 += temp1 * p[2*i+1]; 
	}

	return temp2;
}


void main()
{
	int n;
	double x;
	double *p;
	char c;

	cout<<"请输入插值结点数:\n";
	cin>>n;

	p = new double[2*n];

	cout<<"请输入数据:\n";

	for(int i = 0; i<2*n; i++)
	{
		cin>>p[i];
	}

state1:
	cout<<"请输入插值点x0:\n";
	cin>>x;
    cout<<"结果为:\n"<<Lagrange(p,x,n)<<endl;

state2:
	cout<<"是否退出:y/n:";
	cin>>c;
	if(c=='n')
	{
		goto state1;
	}
	else if(c=='y')
	{
		delete p;
	}
	else
	{
		cout<<"请选折:y/n:"<<endl;
		goto state2;
	}

}
	



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -