bai2_dathuc_f

Màu nền
Font chữ
Font size
Chiều cao dòng

#include"stdio.h"

#include"conio.h"

#include"iostream.h"

#include"iomanip.h"

#include"math.h"

class dathuc

    {

            private:

                    float a[50];

                        int n;

                public:

                    friend void doc(char *tentep,dathuc &dt1,dathuc &dt2);

                        friend void ghi(char *tentep);

                        void nhap();

                        void xuat();

                        float tinh(float x);

                        friend dathuc tong(dathuc dt1,dathuc dt2);

                        friend dathuc tich(dathuc dt1,dathuc dt2);

                };

void dathuc::nhap()

    {

            int i;

                cout<<"

Nhap so bac cho da thuc:"; cin>>n;

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

                {

                    cout<<"

a["<<i<<"]=";

                        cin>>a[i];

                        }

                }

void doc(char *tentep,dathuc &dt1,dathuc &dt2)

      {

             FILE *f;

             f=fopen(tentep,"rb");

             fread(&dt1,sizeof(dathuc),1,f);

             fread(&dt2,sizeof(dathuc),1,f);

                fclose(f);

          }

void taofile(char *tentep)

    {

            FILE *f;

                dathuc tg;

                f=fopen(tentep,"wb");

                cout<<"

Da thuc thu 1 la:";

                tg.nhap();

                fwrite(&tg,sizeof(dathuc),1,f);

        cout<<"

Da thuc thu 2 la:";

                tg.nhap();

                fwrite(&tg,sizeof(dathuc),1,f);

                fclose(f);

        }

void dathuc::xuat()

    {

            int i;

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

                if(i<n)

                cout<<setw(5)<<a[i]<<"x^"<<i<<" + ";

                else

                cout<<setw(5)<<a[i]<<"x^"<<i;

                }

void xuatfile(char *tentep)

    {

            FILE *f;

                dathuc tg;

                f=fopen(tentep,"rb");

                fread(&tg,sizeof(dathuc),1,f);

                cout<<"

Da thuc 1 la:";

                tg.xuat();

                fread(&tg,sizeof(dathuc),1,f);

                cout<<"

Da thuc 2 la:";

                tg.xuat();

                fclose(f);

                }

float dathuc::tinh(float x)

    {

            int i,p;

                p=0;

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

                p+=a[i]*pow(x,i);

                return p;

        }

dathuc tong(dathuc dt1,dathuc dt2)

    {

            dathuc dt; int i;

                if(dt1.n>dt2.n)

                {

                    for(i=0;i<=dt2.n;i++)

                        dt.a[i]=dt1.a[i]+dt2.a[i];

                        for(i=dt2.n+1;i<=dt1.n;i++)

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

                        dt.n=dt1.n;

                        }

                else

                {

                    for(i=0;i<=dt1.n;i++)

                        dt.a[i]=dt1.a[i]+dt2.a[i];

                        for(i=dt1.n+1;i<=dt2.n;i++)

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

                        dt.n=dt2.n;

                    }

                        return dt;

        }

dathuc tich(dathuc dt1,dathuc dt2)

            {

                    dathuc t;

                        int k,i,j;

                            for(k=0;k<=dt1.n+dt2.n;k++)

                                 {        t.a[k]=0;

                                       for(i=0;i<=dt1.n;i++)

                                            for(j=0;j<=dt2.n;j++)

                                               if((i+j)==k)

                                               t.a[k]=t.a[k]+dt1.a[i]*dt2.a[j];

                                      }

                                 t.n=dt1.n+dt2.n;

                                 return t;

                            }

void main()

    {

            dathuc dt1,dt2,dt;

                char *tentep1;

                clrscr();

                float x;

                cout<<"

Nhap ten file :";gets(tentep1);

                taofile(tentep1);

                cout<<"

Du lieu trong file la:";

                xuatfile(tentep1);

                cout<<"

";

                doc(tentep1,dt1,dt2);

                cout<<"

Du lieu trong lop:";

                cout<<"

Dt1=";

                dt1.xuat();

                cout<<"

Dt2=:";

                dt2.xuat();

                cout<<"

Nhap x="; cin>>x;

                cout<<"

Gia tri cua da thuc 1 la:"<<dt1.tinh(x);

                cout<<"

Gia tri cua da thuc 2 la:"<<dt2.tinh(x);

                dt=tong(dt1,dt2);

                cout<<"

Da thuc tong la:

";

                dt.xuat();

                cout<<"

Gia tri cua da thuc tong:"<<dt.tinh(x);

                dt=tich(dt1,dt2);

                cout<<"

Da thuc tich la:

";

                dt.xuat();

                cout<<"

Gia tri cua da thuc tich:"<<dt.tinh(x);

                getch();

        }

Bạn đang đọc truyện trên: Truyen2U.Pro

#123