//Truong Dinh Hung_310941073_DCT1091 #include <iostream> #include <conio.h> #include <stdio.h> typedef struct SoHang { double HeSo; int SoMu; }; typedef struct Node{ SoHang Data; Node *Next; }; Node *CreateNode(float Heso, int SoMu) { Node *p=new Node; p->Data.HeSo=Heso; p->Data.SoMu=SoMu; p->Next=NULL; return p; } void AddFirst(Node* &Head, Node *p) { if(Head==NULL) Head=p; else { p->Next=Head; Head=p; } } void AddLast(Node* &Head, Node *p) { if(Head==NULL) Head=p; else { AddLast(Head->Next,p); } } void XuatDaThuc(Node *p) { if(p!=NULL) { XuatDaThuc(p->Next); if(p->Next!=NULL) printf("+"); if(p->Data.HeSo>1) printf("%.1f",p->Data.HeSo); if(p->Data.HeSo>1 && p->Data.SoMu!=0) printf("*"); if(p->Data.SoMu!=0) printf("X"); if(p->Data.SoMu>0) printf("^%d",p->Data.SoMu); } } Node *Cong(Node *Px,Node *Qx) { Node *p=Px, *q=Qx,*KQ=NULL; while(p!=NULL && q!=NULL) { if(p->Data.SoMu==q->Data.SoMu) { AddLast(KQ,CreateNode(p->Data.HeSo+q->Data.HeSo,p->Data.SoMu)); p=p->Next; q=q->Next; } else if(p->Data.SoMu>q->Data.HeSo) { AddLast(KQ,CreateNode(p->Data.HeSo,p->Data.SoMu)); p=p->Next; } else { AddLast(KQ,CreateNode(q->Data.HeSo,q->Data.SoMu)); q=q->Next; } } while(p!=NULL) { AddLast(KQ,CreateNode(p->Data.HeSo,p->Data.SoMu)); p=p->Next; } while(q!=NULL) { AddLast(KQ,CreateNode(q->Data.HeSo,q->Data.SoMu)); q=q->Next; } return KQ; } Node *PickFirst(Node* &Head) { Node *p=Head; if(Head!=NULL) { Head=p->Next; p->Next=NULL; } return p; } void RemoveFirst(Node* &Head) { Node *p=PickFirst(Head); delete p; } void DaoHam(Node* &Head) { Node *p=Head; while(p!=NULL) { if(p->Data.SoMu==0) { RemoveFirst(Head); p=Head; } else { p->Data.HeSo*=p->Data.SoMu; p->Data.SoMu--; p=p->Next; } } } void main() { SoHang DaThucP[]={{1.2,0},{2,1},{1.4,2},{2.5,3}}; SoHang DaThucQ[]={{2.1,0},{3,1},{1.7,2}}; int Pn=4,Qm=3; Node *Px=NULL,*Qx=NULL; for(int i=0;i<Pn;i++) AddLast(Px,CreateNode(DaThucP.HeSo,DaThucP.SoMu)); for(int j=0;j<Qm;j++) AddLast(Qx,CreateNode(DaThucQ[j].HeSo,DaThucQ[j].SoMu)); printf("Da thuc Px: "); XuatDaThuc(Px); printf("\n"); printf("Da thuc Qx: "); XuatDaThuc(Px); printf("\n\n\n\n"); printf("Cong Hai da thuc: "); Node *KQ=Cong(Px,Qx); XuatDaThuc(KQ); printf("\n\n\n\n"); printf("Dao ham: "); DaoHam(KQ); XuatDaThuc(KQ); getch(); }
lập trình nên viết rõ ràng, mỗi lệnh chú ý nhấn tab cho dễ nhìn thì bạn đã tiết kiệm khá nhiều thời gian đấy. lần sau bạn nên bỏ vào thẻ code và định dạng lại thế này cho dễ nhìn. Mã: //Truong Dinh Hung_310941073_DCT1091 #include <iostream> #include <conio.h> #include <stdio.h> typedef struct SoHang { double HeSo; int SoMu; }; typedef struct Node { SoHang Data; Node *Next; }; Node *CreateNode(float Heso, int SoMu) { Node *p=new Node; p->Data.HeSo=Heso; p->Data.SoMu=SoMu; p->Next=NULL; return p; } void AddFirst(Node* &Head, Node *p) { if(Head==NULL) Head=p; else { p->Next=Head; Head=p; } } void AddLast(Node* &Head, Node *p) { if(Head==NULL) Head=p; else { AddLast(Head->Next,p); } } void XuatDaThuc(Node *p) { if(p!=NULL) { XuatDaThuc(p->Next); if(p->Next!=NULL) printf("+"); if(p->Data.HeSo>1) printf("%.1f",p->Data.HeSo); if(p->Data.HeSo>1 && p->Data.SoMu!=0) printf("*"); if(p->Data.SoMu!=0) printf("X"); if(p->Data.SoMu>0) printf("^%d",p->Data.SoMu); } } Node *Cong(Node *Px,Node *Qx) { Node *p=Px, *q=Qx,*KQ=NULL; while(p!=NULL && q!=NULL) { if(p->Data.SoMu==q->Data.SoMu) { AddLast(KQ,CreateNode(p->Data.HeSo+q->Data.HeSo,p->Data.SoMu)); p=p->Next; q=q->Next; } else if(p->Data.SoMu>q->Data.HeSo) { AddLast(KQ,CreateNode(p->Data.HeSo,p->Data.SoMu)); p=p->Next; } else { AddLast(KQ,CreateNode(q->Data.HeSo,q->Data.SoMu)); q=q->Next; } } while(p!=NULL) { AddLast(KQ,CreateNode(p->Data.HeSo,p->Data.SoMu)); p=p->Next; } while(q!=NULL) { AddLast(KQ,CreateNode(q->Data.HeSo,q->Data.SoMu)); q=q->Next; } return KQ; } Node *PickFirst(Node* &Head) { Node *p=Head; if(Head!=NULL) { Head=p->Next; p->Next=NULL; } return p; } void RemoveFirst(Node* &Head) { Node *p=PickFirst(Head); delete p; } void DaoHam(Node* &Head) { Node *p=Head; while(p!=NULL) { if(p->Data.SoMu==0) { RemoveFirst(Head); p=Head; } else { p->Data.HeSo*=p->Data.SoMu; p->Data.SoMu--; p=p->Next; } } } main() { SoHang DaThucP[]= {{1.2,0},{2,1},{1.4,2},{2.5,3}}; SoHang DaThucQ[]= {{2.1,0},{3,1},{1.7,2}}; int Pn=4,Qm=3; Node *Px=NULL,*Qx=NULL; for(int i=0; i<Pn; i++) AddLast(Px,CreateNode(DaThucP[i].HeSo,DaThucP[i].SoMu)); for(int j=0; j<Qm; j++) AddLast(Qx,CreateNode(DaThucQ[j].HeSo,DaThucQ[j].SoMu)); printf("Da thuc Px: "); XuatDaThuc(Px); printf("\n"); printf("Da thuc Qx: "); XuatDaThuc(Px); printf("\n\n\n\n"); printf("Cong Hai da thuc: "); Node *KQ=Cong(Px,Qx); XuatDaThuc(KQ); printf("\n\n\n\n"); printf("Dao ham: "); DaoHam(KQ); XuatDaThuc(KQ); getch(); }