Cấu trúc dữ liệu Đa thức bằng mảng

#include <stdio.h>
#include <conio.h>
#include <malloc.h>

#define Max 9
typedef int DaThuc[Max];

void
ktRong(DaThuc &dt){
for
(int i=0;i<Max;i++)
dt[i]=0;

}

void
them(int hs,int sm, DaThuc &dt){
dt[sm]=hs;
}

void
congDT(DaThuc dt1,DaThuc dt2,DaThuc &dt3)
{

for
(int i=0;i<Max;i++)
dt3[i]=dt1[i]+dt2[i];
}

void
truDT(DaThuc dt1,DaThuc dt2,DaThuc &dt3)
{

for
(int i=0;i<Max;i++)
dt3[i]=dt1[i]-dt2[i];
}

void
inDT(DaThuc dt){
for
(int i=Max-1;i>=0;i--){
if
(dt[i]!=0){
if
(i==1)
printf("%dx",dt[i]);
else if
(i==0)
printf("%d",dt[i]);
else

printf("%dx^%d",dt[i],i);

for
(int j = i-1;j>=0;j--){
if
(dt[j]<0)
break
;
if
(dt[j]>0){
printf("+");
break
;
}
}


}
}
}


void
nhanDT(DaThuc dt1,DaThuc dt2,DaThuc &dt3){
for
(int i=0;i<Max-1;i++){
if
(dt1[i]!=0){
DaThuc t;
ktRong(t);
for
(int j = 0;j<Max-1;j++){
if
(dt2[j]!=0){
int
hs = dt1[i]*dt2[j];
int
sm = i + j;
them(hs,sm,t);
}
}

congDT(t,dt3,dt3);
}
}
}

int
main(){
DaThuc dt1,dt2;
ktRong(dt1);
ktRong(dt2);


them(2,0,dt1);
them(3,1,dt1);
them(6,4,dt1);
them(6,1,dt2);
them(10,2,dt2);
printf("da thuc 1: ");
inDT(dt1);
printf("\n");
printf("da thuc 2: ");
inDT(dt2);

DaThuc dt3;
ktRong(dt3);
congDT(dt1,dt2,dt3);
printf("\nda thuc 3: ");
inDT(dt3);

DaThuc dt4;
ktRong(dt4);
truDT(dt1,dt2,dt4);
printf("\nda thuc 4: ");
inDT(dt4);


DaThuc dt5;
ktRong(dt5);
nhanDT(dt1,dt2,dt5);
printf("\nda thuc 5: ");
inDT(dt5);
return
0;
}

Bài liên quan

Bài liên quan