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

📄 linearstep.cpp

📁 这事lanrange插值算法实例
💻 CPP
字号:
#include <iostream.h>
#include <math.h>
#include <iomanip.h>

void bubble_sort(double *array,int n)
{
	int count=0, flag;
	for(int i=1;i<n;i++)
	{
		flag=0;
		count++;
		for(int j=n-1;j>=i;j--)
			if(array[j-1]>array[j])	//ascending sort
			{
				flag=1;
				double temp=array[j-1];
				array[j-1]=array[j];
				array[j]=temp;
			}
		if(flag==0)	//sorted in advance
			break;
	}
	cout<<"sort by "<<count<<" loops"<<endl;
	return;
}

double LinearStep(double *x,double *y,int n,double xx)
{
	bubble_sort(x,n);	//sort x[]
	bubble_sort(y,n);	//sort y[]

	for(int i=1;i<n;i++)
		if(xx<x[i])
			break;
	if(i==n)
		i--;

	cout<<"insert between x["<<i-1<<"] and x["<<i<<"]"<<endl;
	double yy=y[i]+(y[i]-y[i-1])*(xx-x[i])/(x[i]-x[i-1]);
	return yy;
}


void main()
{
	double x=135;
	double x0[]={121,100,169,144};
	double y0[]={10,11,12,13};

	double y=LinearStep(x0,y0,4,x);
	cout<<"the square root of the "<<x<<": "<<y<<endl;
}

⌨️ 快捷键说明

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