bai2_dathuc
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#include"iomanip.h"
#include"math.h"
class dathuc
{
private:
float a[50];
int n;
public:
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 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;
}
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]+dt1.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 dt; int i;
if(dt1.n>dt2.n)
{
for(i=0;i<=dt2.n;i++)
dt.a[i]=a[i]*dt1.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]=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;
}*/
void main()
{
dathuc dt1,dt2,dt;
clrscr();
float x;
cout<<"
Nhap da thuc 1:";
dt1.nhap();
cout<<"
Nhap da thuc 2:";
dt2.nhap();
cout<<"
Da thuc 1 la:";
dt1.xuat();
cout<<"
Da thuc 2 la:";
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: AzTruyen.Top