Code:

#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*CongDaThuc(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*NhanHeSo(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*NhanBien(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*NhanDaThucCap1(double*fx, int Bacf, double a, double b){
   double*Temp = NhanBien(fx,Bacf);
   Temp = NhanHeSo(Temp,Bacf+1,a);
   Temp = CongDaThuc(Temp,Bacf+1,NhanHeSo(fx,Bacf,b),Bacf,Bacf);
   return Temp;
}

double*Yi_Pi(Diem*P, int n, int i){
   double* Temp = new double [n+1];
   double BacT = 0, MauSo = 1;
   Temp[0] = 1;
   for(int j=0; j<=n; j++)
   if(i!=j) {
      Temp = NhanDaThucCap1(Temp,BacT,1,-P[j].x);-
      BacT++;
      MauSo*=(P[i].x-P[j].x);
   }
   MauSo = P[i].y/MauSo;
   Temp = NhanHeSo(Temp,BacT,MauSo);
   return Temp;
}

double*Lagrange(Diem*P, int n){
   double*Temp = Yi_Pi(P,n,0);
   for(int i=1; i<=n; i++)
      Temp = CongDaThuc(Temp,n,Yi_Pi(P,n,i),n,n);
   return Temp;
}

void main(){
   clrscr();
   int n;
   cout<<"Nhap So Diem:";
   cin>>n;
   n--;
   Diem*P = NhapDiem(n);
   double* Px = Lagrange(P,n);
   XuatDaThuc(Px,n,"\nP(x)");
   getch();
}