1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| #include<stdio.h>
#define n 5 /* le plus grand degre possible pour les polynomes c'est n-1 */
struct poly
{
int dx; /* plus grand degre de X */
int dy; /* plus grand degre de Y */
int coef[2*n][2*n]; /*le tableau des coefficients du polynome on declare avec le cas generale de 2*n
car on vas utiliser cette meme structure pour representer le poly de multiplication*/
};
/*______________________________________________________________*/
void constructeur( poly *P )
{ int i,j;
/* l'initialisation */
for(i=0;i<2*n;i++)
for(j=0;j<2*n;j++)
P->coef[i][j] = 0;
printf("le plus grand exposent de x : " ); scanf("%d",&P->dx);
printf("le plus grand exposent de y : " ); scanf("%d",&P->dy);
for(i=0;i<=P->dx;i++)
32. for(j=0;j<=P->dy;j++)
{
printf(" le coeficien du monome ( x^%d y^%d ) : ",i,j);
scanf("%d",&P->coef[i][j]);
}
}
/*______________________________________________________________*/
void addition( poly *A , poly *B , poly *P )
{
int i,j;
/*il faut determiner la dimention de la matrice avec la puisance du poly */
if(A->dx > B->dx) P->dx = A->dx; else P->dx = B->dx;
if(A->dy > B->dy) P->dy = A->dy; else P->dy = B->dy;
for(i=0;i<=P->dx;i++)
for(j=0;j<=P->dy;j++)
P->coef[i][j] = A->coef[i][j] + B->coef[i][j];
}
/*_______________________________________________________________*/
void soustraction( poly *A , poly *B , poly *P )
{
int i,j;
for(i=0;i<=P->dx;i++)
for(j=0;j<=P->dy;j++)
P->coef[i][j] = A->coef[i][j] - B->coef[i][j];
}
/*______________________________________________________________*/
void multiplication( poly *A , poly *B , poly *P )
{
int i,j,g,z;
for(i=0;i<=P->dx;i++)
for(j=0;j<=P->dy;j++)
P->coef[i][j] = 0;
for(i=0;i<=A->dx;i++)
for(j=0;j<=A->dy;j++)
for(g=0;g<=B->dx;g++)
for(z=0;z<=B->dy;z++)
P->coef[i+g][j+z] += A->coef[i][j] * B->coef[g][z];
}
/*______________________________________________________________*/
void Affich_poly( poly *PP )
{
int i,j;
for(i=0;i<=PP->dx;i++)
for(j=0;j<=PP->dy;j++)
if(PP->coef[i][j] != 0)
printf(" +(%d) X%d Y%d",PP->coef[i][j],i,j);
}
/*______________________________________________________________*/
void ligne(void)
{ printf("\n\n ------------------------------------------\n\n" ); }
/*______________________________________________________________*/
int main()
{
poly P1 , P2 , P3 , P4 , P5;
constructeur(&P1);
printf("\nP1(X,Y) =" );
Affich_poly( &P1 );
ligne(); /*______________________________________________*/
constructeur( &P2 );
printf("\nP2(X,Y) =" );
Affich_poly( &P2 );
ligne(); /*______________________________________________*/
addition( &P1 , &P2 , &P3 );
printf("\nP1 + P2 =" );
Affich_poly( &P3 );
ligne(); /*______________________________________________*/
P4.dx = P3.dx;
P4.dy = P3.dy; /*la puissance du poly soustraction est egale a celle de l'addition*/
soustraction( &P1 , &P2 , &P4 );
printf("\nP1 - P2 =" );
Affich_poly( &P4 );
ligne(); /*______________________________________________*/
/* prevoir les dimentions (de X et de Y) pour la multiplication */
P5.dx = P1.dx + P2.dx;
P5.dy = P1.dy + P2.dy;
multiplication( &P1 , &P2 , &P5 );
printf("\nP1 * P2 =" );
Affich_poly( &P5 );
return 0;
} |