fffffffffffffffff

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<iostream.h>

  class dathuc{

        private:

                double *a;

        public:

               dathuc();

               dathuc( int nn);

               void nhap();

               void in();

               ~dathuc();

               dathuc operator -();

               dathuc operator +(const dathuc &dt);

               dathuc operator -(const dathuc &dt);

               dathuc operator *(const dathuc &dt);

               double operator [](int i);

               friend double gt(dathuc dt, double &x);

               };

  dathuc::~dathuc(){

                    delete[] a;

                    }

  void dathuc::nhap(){

       cout <<"

Nhap bac da thuc : n = ";

       cin >>n;

       a = new double[100];

       cout <<"

Nhap cac he so : ";

       for( int i = 1 ; i <= n + 1 ; i++ ){

            cout <<"

He so cua x^"<<i-1<<" : ";

            cin >>a[i];

            }

       }

  void dathuc::in(){

      // a = new double[100];

       for( i = 1 ; i <= n + 1 ; i++ )

            cout <<" "<<a[i]<<".x^"<<i-1<<" +";

            printf("\b ");

       }

  dathuc::dathuc(){

                   n = 0 ;

                   a = 0;

                   }

  dathuc::dathuc( int nn){

                  n = nn;

                  }

  dathuc dathuc::operator -(){

         dathuc mdt;

         mdt.a = new double[100];

         mdt.n = this->n;

         for( int i = 1 ; i <= mdt.n+1 ; i++ )

              mdt.a[i] = -this->a[i];

         return mdt;

         }

  dathuc dathuc::operator +(const dathuc &dt){

         dathuc mdt;

         mdt.a = new double[100];

         if( dt.n > this->n ){

             mdt.n = dt.n;

             for ( i = 1 ; i <= dt.n+1 ; i++)

                   mdt.a[i] = dt.a[i];

             for ( i = 1 ; i <= this->n+1 ; i++)

                   mdt.a[i] = this->a[i]+dt.a[i];

             }

         else{

              mdt.n = this->n;

              for ( i = 1 ; i <= this->n+1 ; i++)

                    mdt.a[i] = this->a[i];

              for ( i = 1 ; i <= dt.n+1 ; i++)

                    mdt.a[i] = this->a[i]+dt.a[i];

             }

         return mdt;

         }

  dathuc dathuc::operator -(const dathuc &dt){

         dathuc mdt;

         mdt.a = new double[100];

         if( dt.n > this->n ){

             mdt.n = dt.n;

             for ( i = 1 ; i <= dt.n+1 ; i++)

                   mdt.a[i] = dt.a[i];

             for ( i = 1 ; i <= this->n+1 ; i++)

                   mdt.a[i] = this->a[i]-dt.a[i];

             }

         else{

              mdt.n = this->n;

              for ( i = 1 ; i <= this->n+1 ; i++)

                    mdt.a[i] = this->a[i];

              for ( i = 1 ; i <= dt.n+1 ; i++)

                    mdt.a[i] = this->a[i]-dt.a[i];

             }

         return mdt;

         }

  dathuc dathuc::operator *(const dathuc &dt){

         dathuc mdt,ntp;

         mdt.n = this -> n;

         ntp.n =  mdt.n + dt.n;

         mdt.a = new double[100];

         ntp.a = new double[100];

         for( int i = 1  ; i <= mdt.n + 1 ; i++ )

              mdt.a[i] = this-> a[i];

         for( int i = 1 ; i <= mdt.n + 1; i++ )

              for( int j = 1 ; j <= dt.n + 1 ; j++ )

                   ntp.a[i+j-1] += mdt.a[i]*dt.a[j];

         return ntp;  

         }

  double dathuc::operator [](int i){

         return a[i];

         }

 /* double gt(dathuc dt, double &x){

         double b=0;

         for( int i = dt.n + 1 ; i >= 1 ; i-- )

                  b = dt[i] + b*x;

         return b;

         }*/

         double gt(dathuc dt, double &x)

{

   double b=0;

   for(int i=dt.n+1;i>=1;i--)

   b=dt[i]+b*x;

   return b;

}

  int main(){

      dathuc dt1,dt,dt2;

      double x;

      dt1.nhap();

      dt2.nhap();

      cout <<"

Tong 2 da thuc : ";

      dt =  dt1 +dt2;

      dt.in();

      cout <<"

tru 2 da thuc : ";

      dt =  dt1 -dt2;

      dt.in();

       cout <<"

Nhan 12 da thuc : ";

      dt =  dt1 *dt2;

      dt.in();

      //test di

      cout <<"

Nhap gia tri x : ";

      cin >>x;

      cout <<"

Gia tri cua da thuc thu 1 :"<<gt(dt1,x);

      cout <<"

Gia tri cua da thuc thu 2 :"<<gt(dt2,x);

      getch();

      }

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: