Bài này làm còn một phần thương chưa xong, mình đang có gắng khắc phục, khi nào xong sẽ post lên liền. Các bạn tham khảo nha!
- Code:
#include "iostream.h"
typedef struct
{
int heso,somu;
}DonThuc;
typedef struct
{
DonThuc a[100];
int n;
}DaThuc;
void nhapDonThuc(DonThuc &a);
void xuatDonThuc(DonThuc a);
int tim(DaThuc &A,int mu,int &vt);
void nhap(DaThuc &A);
void xuat(DaThuc A);
void sapxep(DaThuc &A);
void gan(DonThuc &a,DonThuc &b);
void xoa(DaThuc &A,int vt);
void rutgon(DaThuc &A);
DaThuc Doi(DaThuc A);
DaThuc Tong(DaThuc A,DaThuc B);
DaThuc Hieu(DaThuc A,DaThuc B);
DaThuc Tich(DaThuc dt1,DaThuc dt2);
DaThuc Thuong(DaThuc bichia,DaThuc chia);
main()
{
DaThuc A,B,C,D,E,F;
cout<<"Da thuc A co ";
nhap(A);
xuat(A);
cout<<"\nDa thuc B co ";
nhap(B);
xuat(B);
cout<<"\nTong cua hai da thuc: ";
C=Tong(A,B);
xuat(C);
cout<<"\nHieu cua hai da thuc: ";
E=Hieu(A,B);
xuat(E);
cout<<"\nTich cua hai da thuc: ";
D=Tich(A,B);
xuat(D);
cout<<"\nThuong cua hai da thuc: ";
F=Thuong(A,B);
xuat(F);
cout<<"\n";
}
void nhapDonThuc(DonThuc &a)
{
cout<<"Nhap he so: "; cin>>a.heso;
cout<<"Nhap mu: "; cin>>a.somu;
}
void xuatDonThuc(DonThuc a)
{
if(a.somu==0)
if(a.heso==0);
else cout<<a.heso;
else
if(a.somu==1)
if(a.heso==1) cout<<"x";
else
if(a.heso==-1)cout<<"-x";
else cout<<a.heso<<"x";
else
if(a.heso==1) cout<<"x^"<<a.somu;
else
if(a.heso==-1)cout<<"-x^"<<a.somu;
else cout<<a.heso<<"x^"<<a.somu;
}
int tim(DaThuc &A,int mu,int &vt)
{
for(int i=0;i<A.n;i++)
if(A.a[i].somu == mu){vt=i;return 1;} return 0;
}
void nhap(DaThuc &A)
{
int i,vt,n;
A.n=0;
cout<<"bao nhieu don thuc: "; cin>>n;
for(i=0;i<n;i++)
{
if(i==0)
{
nhapDonThuc(A.a[A.n]);
A.n++;
}
else
{
nhapDonThuc(A.a[A.n]);
if(tim(A,A.a[A.n].somu,vt)==1)
A.a[vt].heso=A.a[A.n].heso+A.a[vt].heso;
else A.n++;
}
}
}
void xuat(DaThuc A)
{
sapxep(A);
int i;
for(i=0;i<A.n;i++)
{
if(A.a[i].heso==0);
else
if(i==A.n-1)
{
xuatDonThuc(A.a[i]);
}
else
{
xuatDonThuc(A.a[i]);
if(A.a[A.n-1].heso==0 || A.a[i+1].heso<0);
else cout<<" +";
}
}
}
void sapxep(DaThuc &A)
{
DonThuc tam;
int i,j;
for(i=0;i<A.n-1;i++)
for(j=i+1;j<A.n;j++)
if(A.a[i].somu<A.a[j].somu)
{
gan(tam,A.a[i]);
gan(A.a[i],A.a[j]);
gan(A.a[j],tam);
}
}
void gan(DonThuc &a,DonThuc &b)
{
a.heso=b.heso;
a.somu=b.somu;
}
void xoa(DaThuc &A,int vt)
{
for(int i=vt;i<A.n;i++)
{
A.a[i]=A.a[i+1];
}
A.n--;
}
void rutgon(DaThuc &A)
{
DonThuc tam;
int vt, i = 0;
sapxep(A);
do
{
if ((tim(A,A.a[i].somu,vt) == 1) && (vt != i))
{
tam.heso=A.a[i].heso+A.a[vt].heso;
tam.somu=A.a[i].somu;
gan(A.a[i],tam);
xoa(A,vt);
i = 0;
}
else i++;
}while(i<A.n);
}
DaThuc Doi(DaThuc A)
{
for(int i=0;i<A.n;i++)
{
A.a[i].heso=-A.a[i].heso;
}
return A;
}
DaThuc Tong(DaThuc A,DaThuc B)
{
int i,j;
for(j=0;j<B.n;j++)
{
gan(A.a[A.n],B.a[j]);
A.n++;
}
rutgon(A);
return A;
}
DaThuc Hieu(DaThuc A,DaThuc B)
{
return Tong(A,Doi(B));
}
DaThuc Tich(DaThuc dt1, DaThuc dt2)
{
DaThuc dt3,temp;
int vt;
dt3.n = 0;
for(int i = 0; i < dt1.n; i++)
{
for (int j = 0; j < dt2.n; j++)
{
dt3.a[dt3.n].heso=dt2.a[j].heso * dt1.a[i].heso;
dt3.a[dt3.n].somu = dt2.a[j].somu + dt1.a[i].somu;
dt3.n++;
}
}
rutgon(dt3);
return dt3;
}
DaThuc Thuong(DaThuc bichia,DaThuc chia)
{
DaThuc tam,A,B; A.n=0;B.n=0;
int i=0,bac=bichia.a[0].somu;
if(bac<chia.a[0].somu) cout<<"Khong the chia";
while(bac>=chia.a[0].somu)
{
tam.n=1;
tam.a[0].heso=bichia.a[0].heso/chia.a[0].heso;
tam.a[0].somu=bac-chia.a[0].somu;
A=Tong(A,tam);A.n++;
bichia=Hieu(bichia,Tich(tam,chia));
bac=bichia.a[0].somu;
if(bichia.n==1&&bichia.a[0].heso==0) break;
}
sapxep(A);
return A;
}