Thứ Hai, 24 tháng 11, 2008

Bai tap da thuc =C++

#include
#include
#include
class dt
{
private:
int n;// bac cua da thuc
double *a;//tro toi vung nho chua cac he so da thuc a0,a1..
public:
/*~dt()
{
this->n=0;
delete this->a;
}*/
dt()
{
this->n=0;
this->a=NULL;
}
dt(int n1)
{
this->n=n1;
this->a=new double[n1+1];
}
friend ostream& operator<<(ostream& os,const dt &d);//dung de xuat cac he so da thuc friend istream&operator>>(istream& is,dt &d);// dung de nhap cac he so da thuc
dt operator-();//doi dau cac da thuc
dt operator+(const dt &d2);//dung de cong hai da thuc
dt operator-(dt d2);//dung de tru hai da thuc
dt operator*(const dt &d2);//dung de nhan 2 da thuc
void xuatdt();//xuat da thuc
double operator^(const double &x);//dung de tinh gia tri da thuc
double operator[](int i)//dung de cho biet bac va he so cua da thuc
{
if(i<0) s="0.0,t="1.0;" n="int(d[-1]);" i="0;i<="n;++i)" i="0;i<="d.n;++i)"><<","; return os; } istream& operator>>(istream& is,dt &d)
{
if(d.a!=NULL)
delete d.a;
cout<<"bac da thuc:"; cin>> d.n;
d.a=new double[d.n+1];
cout<<"nhap cac he so da thuc:\n"; for(int i=0;i<=d.n;++i) { cout<<" he so bac "<<<"="; is>>d.a[i];
}
return is;
}
void dt::xuatdt()
{
for(int i=n;i>=0;i--)
{
if(i!=0)
cout<<"("<<<")x^"<<<"+"; else cout<<"("<<<")"; } } dt dt::operator -() { dt p(this->n);
for(int i=0;i<=n;++i) p.a[i]=-a[i]; return p; } dt dt::operator +(const dt &d2) { int k,i; k=n>d2.n?n:d2.n;// n>d2.n dung return n,sai return d2.n
dt d(k);
for(i=0;i<=k;++i) if(i<=n&&i<=d2.n) d.a[i]=a[i]+d2.a[i]; else if(i<=n)// neu i<=n nhung ko be hon d2.n thi thuc hien d.a[i]=a[i]; else d.a[i]=d2.a[i]; i=k; while(i>0&&d.a[i]==0.0)
--i;
d.n=i;
return d;
}
dt dt::operator -(dt d2)
{
return (*this+(-d2));
}
dt dt::operator *(const dt&d2)
{
int k,i,j;
k=n+d2.n;
dt d(k);
for(i=0;i<=k;++i) d.a[i]=0; for(i=0;i<=n;++i) for(j=0;j<=d2.n;++j) d.a[i+j]+=a[i]*d2.a[j]; return d; } double dt::operator ^(const double &x) { double s=0.0,t=1.0; for(int i=0;i<=n;++i) { s+=a[i]*pow(x,i); t*=x; } return s; } void main() { dt p,q,r,s,f,h; double x1,x2,g1,g2; cout<<"\n nhap da thuc p "; cin>>p;
cout<<"\n da thuc p: "; p.xuatdt(); cout<<"\n"<<<"\n nhap da thuc Q "; cin>>q;
cout<<"\n da thuc q: "; q.xuatdt(); cout<<"\n he so:"<<<"\n nhap da thuc R "; cin>>r;
cout<<"\n da thuc r:"; r.xuatdt(); cout<<"\n"<<<"\n nhap da thuc s "; cin>>s;
cout<<"\n da thuc s: "; s.xuatdt(); cout<<"\n"<<<"\n da thuc f: "; f.xuatdt(); cout<<"\n nhap so thuc x1:"; cin>>x1;
cout<<"\n nhap so thuc x2:"; cin>>x2;
g1=f^x1;
g2=F(r,x2);
cout<<"\n da thuc f"<
cout<<"\n f("<<<")="<
cout<<"\n f("<<<")="<
getch();
}[code]

Không có nhận xét nào: