Chuong trinh xu ly da thuc
CODE:
#include<iostream.h>
#include<process.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
class dathuc{
public:
int bac;
float heso[80];
void nhapdt();
void chuanhoa();
void xuatdt();
void gioithieu();
dathuc operator + (dathuc);
dathuc operator - (dathuc);
dathuc operator * (dathuc);
dathuc operator / (dathuc);
dathuc operator - ();
};
void dathuc::nhapdt(){
int i=0;
cout<<" *Nhap bac toi da cua da thuc: ";
cin>>bac;
for(i=bac;i>=0;i--){
cout<<"He so cua X"<<i<<":";
cin>>heso[i];}
}
void dathuc::chuanhoa()
{
while (bac>0&&heso[bac]==0) bac--;
}
void dathuc::xuatdt(){
if (heso[bac]!=1||(heso[bac]==1&&bac==0))
cout<<heso[bac];
cout<<(bac>0?"X":"");
if (bac>0) cout<<bac;
int i=bac-1;
while (i>0){
if (heso[i] !=0){
cout<<(heso[i]>0?"+":"");
if (heso[i]!=1)
cout<<heso[i];
cout<<"X"<<i;
}
i--;
}
if (i==0&&heso[i]!=0)
{
cout<<(heso[i]>0?"+":"");
cout<<heso[i];
}
cout<<endl;
}
dathuc dathuc::operator /(dathuc b)
{
dathuc dt3,dt4,dt5,dt;
int i,j,k;
b.chuanhoa();
if (b.heso[b.bac]!=0)// neu b khong phai la da thuc 0
if (bac<b.bac){
dt3.bac=0;
dt3.heso[0]=0;
}//if
else
{
dt3.bac=bac-b.bac;
for (i=0;i<=bac;i++) dt3.heso[i]=0;
dt3.heso[bac-b.bac]=heso[bac]/b.heso[b.bac];
dt=dt3*b;
dt.chuanhoa();
dt4=*this-dt;
dt4.chuanhoa();
k=dt4.bac;
for (i=bac-1;i>=b.bac;i--)
{
if (dt4.bac>=b.bac)
{
dt3.heso[i-b.bac]=dt4.heso[k--]/b.heso[b.bac];
dt5.bac=dt4.bac-b.bac;
for (j=dt5.bac;j>=0;j--){dt5.heso[j]=0;}
dt5.heso[dt5.bac]=dt3.heso[i-b.bac];
dt5.chuanhoa();
dt=dt5*b;
dt.chuanhoa();
dt4=dt4-dt;
dt4.chuanhoa();
}//if
}//for
}//else
else// neu b la dt 0
{
cout<<"Khong the chia cho da thuc 0"<<endl;
dt3.bac=0;dt3.heso[0]=0;
}
return dt3;
}
dathuc dathuc::operator + (dathuc b)
{
dathuc tam;
tam.bac=(bac<b.bac?b.bac:bac);//gan tam.bac=1 trong hai gia tri
for (int i=0;i<=(bac>b.bac?b.bac:bac);i++)//hoac i<=tam.bac
tam.heso[i]=heso[i]+b.heso[i];
while (i<=(bac<b.bac?b.bac:bac))
tam.heso[i++]=(bac<b.bac?b.heso[i]:heso[i]);
return tam;
}
dathuc dathuc::operator - (dathuc b){
dathuc tam;
tam.bac=(bac<b.bac?b.bac:bac);
int i;
for (i=0;i<=(bac>b.bac?b.bac:bac);i++)
tam.heso[i]=heso[i]-b.heso[i];
while (i<=(bac<b.bac?b.bac:bac))
tam.heso[i++]=(bac<b.bac?-b.heso[i]:heso[i]);
return tam;
}
dathuc dathuc::operator * (dathuc b){
dathuc tam;
tam.bac=bac+b.bac;
int i,j=0;
for (i=0;i<=tam.bac;i++)
tam.heso[i]=0;
for (i=0;i<=bac;i++)
for (j=0;j<=b.bac;j++)
tam.heso[i+j]+=heso[i]*b.heso[j];
return tam;
}
dathuc dathuc::operator -()
{
for (int i=0;i<=bac;i++)
heso[i]=-heso[i];
return *this;
}
void dathuc::gioithieu()
{
//textcolor(CYAN+BLINK);
cout<<"\n *************CHUONG TRINH XU LY DA THUC************";
cout<<"\n * *";
cout<<"\n * Sinh vien thuc hien: dang van lap * " ;
cout<<"\n * *";
cout<<"\n * Giao vien huong dan:..09059962226. *";
cout<<"\n * *";
cout<<"\n ***************************************************";
}
void main()
{
dathuc a,b,tong,hieu,tich,thuong,du,gt;
textbackground(1);
char chon;
int exit=0;
clrscr();
gt.gioithieu();
while (!exit)
{
cout<<"\n -------------------- ";
cout<<"\n | MENU | ";
cout<<"\n -----------------------------------";
cout<<"\n | 1. Nhap da thuc |";
cout<<"\n | 2. Da thuc chuan hoa |";
cout<<"\n | 3. Da thuc tong |";
cout<<"\n | 4. Da thuc hieu |";
cout<<"\n | 5. Da thuc tich |";
cout<<"\n | 6. Da thuc thuong |";
cout<<"\n | 7. Da thuc dao dau |";
cout<<"\n | 8. Xoa man hinh |";
cout<<"\n | 9. thoat chuong trinh |";
cout<<"\n -----------------------------------";
cout<<"\n moi ban chon cong viec:"; cin>>chon;
switch (chon) {
case '1': a.nhapdt();
b.nhapdt();
getchar();
break;
case '2': a.chuanhoa();
b.chuanhoa();
cout<<"\n";
cout<<"Da thuc thu 1 chuan hoa:";
a.xuatdt();
cout<<"\n";
cout<<"Da thuc thu 2 chuan hoa:";
b.xuatdt();
getchar();
break;
case '3': cout<<"\n";
cout<<"Da thuc tong la:"<<endl;
a.xuatdt();
cout<<"+"<<endl;
b.xuatdt();
cout<<"-----------------------------"<<endl;
tong=a+b;tong.chuanhoa();tong.xuatdt();
break;
case '4': cout<<"\n";
cout<<"Da thuc hieu la:"<<endl;
a.xuatdt();
cout<<"-"<<endl;
b.xuatdt();
cout<<"-----------------------------"<<endl;
hieu=a-b;hieu.chuanhoa();hieu.xuatdt();
getchar();
break;
case '5': cout<<"\n";
cout<<"Da thuc tich la:"<<endl;
a.xuatdt();
cout<<"*"<<endl;
b.xuatdt();
cout<<"-----------------------------"<<endl;
tich=a*b;tich.chuanhoa();tich.xuatdt();
getchar();
break;
case '6': cout<<"\n";
cout<<"Da thuc thuong la: ";
thuong=a/b;thuong.chuanhoa();
if (b.bac!=0||(b.bac==0&&b.heso[0]!=0))
{
thuong.xuatdt();
cout<<"Da thuc du trong phep chia la: ";
du=a-(thuong*b);
du.chuanhoa();du.xuatdt();
}//if
getchar();
break;
case '7': cout<<"\n";
cout<<"Da thuc 1 sau khi doi dau: ";
-a;a.chuanhoa();a.xuatdt();
cout<<"Da thuc 2 sau khi doi dau: ";
-b;b.chuanhoa();b.xuatdt();
getchar();
break;
case '8': clrscr();
break;
case '9': exit=1;
break;
default : ;
}
}
}