Cài đặt phương pháp Newton tiến

Mã:
#include "conio.h"
#include "iostream.h"

typedef struct Diem{
	double x,y;
}Diem;

Diem KhoiTao(double x, double y){
	Diem Temp;
	Temp.x = x;
	Temp.y = y;
	return Temp;
}

Diem*NhapDiem(int n){
	Diem*Temp = new Diem [n+1];
	for(int i=0; i<=n; i++){
		cout<<"x"<<i<<" = ";
		cin>>Temp[i].x;
		cout<<"y"<<i<<" = ";
		cin>>Temp[i].y;
	}
	return Temp;
}

double*NhapDaThuc(int n) {
	double* Temp = new  double [n+1];
	for(int i= 0; i<=n; i++) {
		cout<<"a"<<i<<" = ";
		cin>>Temp[i];
	}
	return Temp;
}

void XuatDaThuc(double*fx,int Bacf, char*s) {
	cout<<s<<" = "<<fx[0];
	for(int i=1; i<=Bacf; i++){
		if(fx[i]>0) {
			cout<<" + "<<fx[i]<<"x^"<<i;
		}
		if(fx[i]<0) {
			cout<<fx[i]<<"x^"<<i;
		}
	}
}

double*Cong(double*fx,int Bacf,double*gx,int Bacg, int&BacTong){
	double*Temp;
	if(Bacf<Bacg){
		BacTong = Bacg;
		Temp = new double [BacTong+1];
		for(int i = 0; i<=Bacf; i++)
			Temp[i] = fx[i]+gx[i];
		for(i = Bacf+1;i<=Bacg; i++)
			Temp[i] = gx[i];
	}else{
		BacTong = Bacf;
		Temp = new double [BacTong+1];
		for(int i = 0; i<=Bacg; i++)
			Temp[i] = fx[i]+gx[i];
		for(i = Bacg+1;i<=Bacf; i++)
			Temp[i] = fx[i];
	}
	return Temp;
}

double*Nhan(double*fx,int Bacf,double c){
	double * Temp = new double [Bacf+1];
	for(int i=0; i<=Bacf; i++)
		Temp[i]=c*fx[i];
	return Temp;
}

double*Nhan(double*fx,int Bacf){
	double*Temp = new double [Bacf+2];
	for(int i=0; i<=Bacf; i++)
		Temp[i+1]=fx[i];
	Temp[0] = 0;
	return Temp;
}

double*Nhan(double*fx, int Bacf, double a, double b){
	double*Temp = Nhan(fx,Bacf);
	Temp = Nhan(Temp,Bacf+1,a);
	Temp = Cong(Temp,Bacf+1,Nhan(fx,Bacf,b),Bacf,Bacf);
	return Temp;
}

/*-------------------SAI PHAN--------------------*/
double**SaiPhan(Diem*P,int n){
	double**Temp;
	*Temp = new double [n+1];
	for(int i=0; i<=n; i++)
		Temp[i] = new double [n+2-i];
	for(i=0; i<=n; i++){
		Temp[i][0] = P[i].x;
		Temp[i][1] = P[i].y;
	}
	for(i=2; i<=n+1;i++)
	for(int j=0; j<=n+1-i;j++)
		Temp[j][i] =(Temp[j+1][i-1]-Temp[j][i-1])/(Temp[j+i-1][0]-Temp[j][0]);
	return Temp;
}

void XuatBangSaiPhan(double**A, int n){
	for(int i=0; i<=n; i++) {
		cout<<endl;
		for(int j=0; j<=n+1-i; j++)
			cout<<A[i][j]<<"\t";
	}
}

/*---------------------NEWTON TIEN----------------*/
double*DiemTien(Diem*P,int n){
	double*Temp = new double[n+1];
	double**A = SaiPhan(P,n);
	for(int i=0; i<=n; i++)
		Temp[i] = A[0][i+1];
	return Temp;
}


double*NewtonTien(Diem*P,int n){
	double*Temp,*Temp1;
	double*Y = DiemTien(P,n);
	int BacT = 0,BacT1;
	*Temp = Y[0];
	for(int i = 1; i<=n; i++) {
		BacT1 = 0;
		*Temp1=1;
		for(int j=0; j<i;j++) {
			Temp1 = Nhan(Temp1,BacT1,1,-P[j].x);
			BacT1++;
		}
		Temp1 = Nhan(Temp1,BacT1,Y[i]);
		Temp = Cong(Temp,BacT,Temp1,BacT1,BacT);
	}
	return Temp;
}

/*---------------------NEWTON LUI----------------*/
double*DiemLui(Diem*P,int n){
	double*Temp = new double[n+1];
	double**A = SaiPhan(P,n);
	for(int i=n+1; i>=1; i--)
		Temp[i-1] = A[n+1-i][i];
	return Temp;
}

double*NewtonLui(Diem*P,int n){
	double*Temp,*Temp1;
	double*Y = DiemLui(P,n);
	int BacT = 0,BacT1;
	*Temp = Y[0];
	for(int i = 1; i<=n; i++) {
		BacT1 = 0;
		*Temp1=1;
		for(int j=0; j<i;j++) {
			Temp1 = Nhan(Temp1,BacT1,1,-P[n-j].x);
			BacT1++;
		}
		Temp1 = Nhan(Temp1,BacT1,Y[i]);
		Temp = Cong(Temp,BacT,Temp1,BacT1,BacT);
	}
	return Temp;
}

void Exit() {
		asm {
		mov ah,4ch;
		int 21h
	}
}

void main(){
	clrscr();
	int n;
	cout<<"Nhap So Diem Can Noi Suy:";
	cin>>n;
	n--;
	Diem*P = NhapDiem(n);
	double* Px = NewtonTien(P,n);
	cout<<"\nDA THUC NOI SUY PHUONG PHAP NEWTON TIEN";
	XuatDaThuc(Px,n,"\np(x)");
	double* Hx = NewtonLui(P,n);
	cout<<"\nDA THUC NOI SUY PHUONG PHAP NEWTON LUI";
	XuatDaThuc(Hx,n,"\nh(x)");
	getch();
	Exit();
}