Họ Và Tên: Lê Phương Thy MSSV: 33103055 Trường: Đhsp Tphcm Lớp: Tin2

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

HỌ VÀ TÊN: LÊ PHƯƠNG THY

MSSV: 33103055
TRƯỜNG: ĐHSP TPHCM
LỚP: TIN2

#include<iostream>
using namespace std;

struct data{
int heso;
size_t somu;
char bien;
char dau_mu;
};
struct nut{
data info;
nut* next;
};
struct dathuc{
nut* dau;
nut* cuoi;
};

nut* tao_nut(data x){


nut* p;
p=new nut;
if(p==NULL)
return p;
p->info =x;
p->next =NULL;
return p;
}
bool chen_nut(dathuc& l,data x){
nut* new_ele=tao_nut(x);
if(new_ele==NULL) return 0;
if(l.dau ==NULL){
l.dau =new_ele;
l.cuoi =l.dau ;
}
else{
l.cuoi->next =new_ele;
l.cuoi=new_ele;
}
return 1;
}
istream& operator>>(istream& in,data& x){
do{
cout<<"<->";
in>>x.heso ;
in>>x.bien ;
in>>x.dau_mu ;
in>>x.somu ;
if((x.bien!='x'&&x.bien!='X')||(x.dau_mu!='^')){
cout<<"->";
cout<<"khong cho phep nhap nhu vay. ";
cout<<"Xin hay nhap lai"<<endl;
}
}while((x.bien!='x'&&x.bien!='X')||(x.dau_mu!='^'));
in.clear ();
return in;
}
size_t kich_thuoc(dathuc l){
nut* p=l.dau;size_t i=0;
while(p!=NULL){
if(p->info.heso!=0) ++i;
p=p->next;
}
return i;
}
void xuat_dathuc(dathuc l){
nut* p=NULL;
p=l.dau ;
bool h=0,k=0;
while(p!=NULL){
if(p->info.heso==0){
if(p==l.dau) h=1;
p=p->next;
if(kich_thuoc(l)==0&&k==0){
cout<<"0";
k=1;
}
continue;
}
if(p->info.somu==0){
if(((p->info).heso >0)&&p!=l.dau&&h!=1)
cout<<"+"<<p->info.heso;
else
cout<<p->info.heso;
p=p->next;
continue;
}
if(p->info.somu==1){
if(((p->info).heso >0)&&p!=l.dau&&h!=1)
cout<<"+"<<p->info.heso<<p->info.bien;
else
cout<<p->info.heso<<p->info.bien;
p=p->next;
continue;
}
if(((p->info).heso >0)&&p!=l.dau&&h!=1)
cout<<"+"<<p->info.heso<<p->info.bien <<p-
>info.dau_mu<<p->info.somu;
else{
cout<<p->info.heso<<p->info.bien <<p->info.dau_mu<<p-
>info.somu;
h=0;
}
p=p->next;
}
cout<<endl;
}
void xoa_nut(dathuc& l){
nut* p=l.dau ;
nut* q=NULL;
while(p!=NULL)
{
if(p->info.dau_mu =='@') break;
q=p;p=p->next ;
}
if(q!=NULL&&p!=NULL)
{
if(p==l.cuoi )
l.cuoi =q;
q->next =p->next ;
delete p;
}
}
void thu_gon(dathuc& l){
nut* p=l.dau;int lan=0;
while(p!=NULL){
for(nut* q=p->next;q!=NULL;){
if(p->info.somu==q->info.somu){
p->info.heso+=q->info.heso;
q->info.dau_mu='@';lan++;
q=q->next ;
}
else{
q=q->next;
}
}
p=p->next;
}
for(int i=1;i<=lan;++i) xoa_nut(l);
}
double tong_heso(dathuc l){
double tong=0;
nut* p=l.dau;
while(p!=NULL){
tong+=p->info .heso;
p=p->next;
}
return tong;
}
double giatridathuc(dathuc l,int x){
nut* p=l.dau ;double value=0;
while(p!=NULL){
double j=1;
for(int i=1;i<=p->info.somu;++i){
j*=x;
}
value+=p->info.heso * j;
p=p->next;
}
return value;
}
int main(){
dathuc list;
data x;
cout<<"//cach nhap: nhap lan luot tung phan tu cua da thuc"<<endl;
cout<<"//chu y: he so la so nguyen, so mu la so tu nhien"<<endl;
cout<<"//vi du: 2x^2 hay 2X^2"<<endl;
cout<<"//neu nhap khac voi vi du se gay loi\n"<<endl;
list.cuoi=NULL;
list.dau =NULL;
while(cin>>x){
bool k;
k=chen_nut(list,x);
if(k==1) cout<<"<-nut da duoc chen"<<endl;
else cout<<"->nut chua duoc chen"<<endl;
char traloi;
cout<<"ban co muon chen tiep hay khong?(c/k)";
cin>>traloi;
if(traloi=='k'||traloi=='K') break;
}
cout<<"\n\n"<<endl;
cout<<"*****************************************"<<endl;
cout<<"*F(x)=";
//thu gon da thuc
thu_gon(list);
//hien thi da thuc ra man hinh
xuat_dathuc(list);
//tinh so phan tu cua da thuc da thu gon
cout<<"*So phan tu cua da thuc la: "<<kich_thuoc(list)<<endl;
//tinh tong cac he so
cout<<"*Tong cac he so bang "<<tong_heso(list)<<endl;
//tinh gia tri da thuc khi nhap mot so
cout<<"*TINH GIA TRI CUA DA THUC TREN:"<<endl;
cout<<" khi x=";
int tam;
cin>>tam;
cout<<" thi F("<<tam<<")="<<giatridathuc(list,tam)<<endl;
system("pause");
return 0;
}

You might also like