Admin Admin
Tổng số bài gửi : 34 Join date : 05/02/2010 Age : 37
| Tiêu đề: Bài Đa Thức. Nhớ dẫn tui ăn chè nhe (^ _ ^)! ! ! Fri Feb 26, 2010 7:44 pm | |
| - Code:
-
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <alloc.h>
typedef struct node { float heso; int somu; struct node *next; }; typedef node *node_ptr; typedef node_ptr Dathuc; typedef node_ptr position;
//Khoi tao da thuc void make_null(Dathuc *dt) { (*dt)=(position)malloc(sizeof(node)); (*dt)->next=NULL; }
//Them vao sau Dathuc void insert_last(float heso, int somu, Dathuc *dt) { position p, temp; temp=(position)malloc(sizeof(node)); p=(*dt); while(p->next!=NULL) p=p->next; temp->heso=heso; temp->somu=somu; temp->next=NULL; p->next=temp; }
//Duyet Da thuc void duyetdathuc(Dathuc *dt) { position p; p=*dt; if(p==NULL) printf("Da thuc rong."); else { printf("\n"); p=p->next; while(p!=NULL) { printf("%0.0fX^%d ",p->heso,p->somu); p=p->next; } } }
// Sap xep Da thuc void sapxep(Dathuc *dt) { position p,q; float heso; int somu; p=(*dt)->next; if(p!=NULL) { while(p->next!=NULL) { q=p->next; while(q!=NULL) { if(p->somu<q->somu) { heso=p->heso;somu=p->somu; p->heso=q->heso;p->somu=q->somu; q->heso=heso;q->somu=somu; } q=q->next; } p=p->next; } } }
// Rut gon Da thuc void rutgon(Dathuc dt,Dathuc *dtrg) { position p,q; float heso; int somu; sapxep(&dt); p=dt->next; make_null(dtrg); while(p!=NULL) { heso=p->heso; somu=p->somu; while(p->next!=NULL && p->next->somu==somu) { heso+=p->next->heso; p=p->next; } if(heso!=0) insert_last(heso,somu,dtrg); p=p->next; }
}
//Cong da thuc Dathuc congdathuc(Dathuc dt1, Dathuc dt2) { Dathuc dtrg1, dtrg2, dttong; position p,q; float heso; int somu; rutgon(dt1,&dtrg1); rutgon(dt2,&dtrg2); p=dtrg1->next; q=dtrg2->next; make_null(&dttong); while(p!=NULL&&q!=NULL) { if(p->somu<q->somu) { heso=q->heso; somu=q->somu; q=q->next; } else if(p->somu==q->somu) { heso=q->heso+p->heso; somu=q->somu; q=q->next; p=p->next; } else { heso=p->heso; somu=p->somu; p=p->next; } insert_last(heso,somu,&dttong); } while(p!=NULL) { insert_last(p->heso,p->somu,&dttong); p=p->next; } while(q!=NULL) { insert_last(q->heso,q->somu,&dttong); q=q->next; } return dttong; }
//Tru da thuc Dathuc trudathuc(Dathuc dt1, Dathuc dt2) { Dathuc dtrg1, dtrg2, dthieu; position p,q; float heso; int somu; rutgon(dt1,&dtrg1); rutgon(dt2,&dtrg2); p=dtrg1->next; q=dtrg2->next; make_null(&dthieu); while(p!=NULL&&q!=NULL) { if(p->somu<q->somu) { heso=-(q->heso); somu=q->somu; q=q->next; } else if(p->somu==q->somu) { heso=p->heso-q->heso; somu=p->somu; q=q->next; p=p->next; } else { heso=p->heso; somu=p->somu; p=p->next; } insert_last(heso,somu,&dthieu); } while(p!=NULL) { insert_last(p->heso,p->somu,&dthieu); p=p->next; } while(q!=NULL) { insert_last(q->heso,q->somu,&dthieu); q=q->next; } return dthieu; }
//Nhan da thuc Dathuc nhandathuc(Dathuc dt1, Dathuc dt2) { Dathuc dtrg1, dtrg2, dtti,dttam; position p,q; float heso; int somu; rutgon(dt1,&dtrg1); rutgon(dt2,&dtrg2); p=dtrg1->next; make_null(&dtti); while(p!=NULL) { make_null(&dttam); q=dtrg2->next; while(q!=NULL) { heso=p->heso*q->heso; somu=p->somu+q->somu; insert_last(heso,somu,&dttam); q=q->next; } dtti=congdathuc(dtti,dttam); p=p->next; } return dtti;
}
// Lay so mu lon nhat int max_somu(Dathuc dt) { Dathuc dtrg; position p; int max=0; rutgon(dt,&dtrg); p=dtrg->next; while(p!=NULL) { if(max<p->somu) max=p->somu; p=p->next; } return max; }
//Chia da thuc,chi lay thuong, so du ko in ra Dathuc chiadathuc(Dathuc dt1, Dathuc dt2) { Dathuc dtrg1, dtrg2, dtth, dttich, donthuc; position p,q; float heso; int somu; rutgon(dt1,&dtrg1); rutgon(dt2,&dtrg2); p=dtrg1->next; q=dtrg2->next; make_null(&dtth); if(p->somu>=q->somu) { while(p->somu>=q->somu) { heso=p->heso/q->heso; somu=p->somu-q->somu; insert_last(heso,somu,&dtth); make_null(&dttich); make_null(&donthuc); insert_last(heso,somu,&donthuc); dttich=nhandathuc(donthuc,dtrg2); dtrg1=trudathuc(dtrg1,dttich); rutgon(dtrg1,&dtrg1); p=dtrg1->next; } } return dtth;
} void main() { Dathuc dt1,dt2; float heso; int somu,n; char cn; make_null(&dt1); make_null(&dt2); do{ printf("\n1: Tao 2 Da thuc\n"); printf("2: Xem 2 da thuc\n"); printf("3: Cong 2 Da thuc\n"); printf("4: Tru 2 Da thuc\n"); printf("5: Nhan 2 Da thuc\n"); printf("6: Chia 2 Da thuc\n"); printf("0: Ket thuc ChTr\n"); printf("Chuc nang da chon\n"); scanf("%d",&cn); switch(cn) { case 1: { int i=0; printf("\nSo don thuc trong da thuc 1: ");scanf("%d",&n); while(i<n) { printf("\nX%d: ",i+1); printf("\n\tHe so: "); scanf("%f",&heso); printf("\n\tSo mu: "); scanf("%d",&somu); insert_last(heso,somu,&dt1); ++i; } i=0; printf("\nSo don thuc trong da thuc 2: ");scanf("%d",&n); while(i<n) { printf("\nX%d: ",i+1); printf("\n\tHe so: "); scanf("%f",&heso); printf("\n\tSo mu: "); scanf("%d",&somu); insert_last(heso,somu,&dt2); ++i; }
break; } case 2: { printf("\nDathuc 1: "); duyetdathuc(&dt1); printf("\nDathuc 2: "); duyetdathuc(&dt2); break;
} case 3: { Dathuc tong; printf("\nCong 2 Da thuc"); printf("\nDathuc 1: "); duyetdathuc(&dt1); printf("\nDathuc 2: "); duyetdathuc(&dt2); tong=congdathuc(dt1,dt2);
printf("\nKet qua"); duyetdathuc(&tong); break; } case 4: { Dathuc hieu; printf("\nTru 2 Da thuc"); printf("\nDathuc 1: "); duyetdathuc(&dt1); printf("\nDathuc 2: "); duyetdathuc(&dt2); hieu=trudathuc(dt1,dt2); rutgon(hieu,&hieu);
printf("\nKet qua"); duyetdathuc(&hieu); break; } case 5: { Dathuc tich; printf("\nNhan 2 Da thuc"); printf("\nDathuc 1: "); duyetdathuc(&dt1); printf("\nDathuc 2: "); duyetdathuc(&dt2); tich=nhandathuc(dt1,dt2);
printf("\nKet qua"); duyetdathuc(&tich); break; } case 6: { Dathuc thuong; printf("\nChia 2 Da thuc"); printf("\nDathuc 1: "); duyetdathuc(&dt1); printf("\nDathuc 2: "); duyetdathuc(&dt2); thuong=chiadathuc(dt1,dt2);
printf("\nKet qua"); duyetdathuc(&thuong); break; } } }while(cn!=0);
}
| |
|