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
| #include <stdio.h>
#include <stdlib.h>
typedef struct polynome{
int degre;
float *coeff;
}polynome;
polynome *initialiser()
{
polynome *p;
int deg;
float *tab;
int i;
printf("Degre =\n");
scanf("%d", °);
tab = (float *) malloc(deg * sizeof(float));
for (i=0; i<=deg; i++)
{
printf("Coeff x^%d = ", i);
scanf("%f", tab+i);
}
p = (polynome *) malloc(sizeof(polynome));
p->degre = deg;
p->coeff = tab;
return p;
}
void afficher(polynome *p)
{
int i;
printf("P(x) = ");
for (i=p->degre; i>0; i--)
{
if (p->coeff[i] != 0)
printf("%f*x^%d + ", p->coeff[i], i);
}
printf("%fx^0\n\n", p->coeff[0]);
}
polynome *ajouter(polynome *p1, polynome *p2)
{
polynome *res;
int deg_max;
float *tab;
int i;
if (p1->degre < p2->degre)
deg_max = p2->degre;
else
deg_max = p1->degre;
tab = (float *) malloc(deg_max * sizeof(float));
for (i=0; i<=deg_max; i++)
tab[i] = p1->coeff[i] + p2->coeff[i];
res = (polynome *) malloc(sizeof(polynome));
res->degre = deg_max;
res->coeff = tab;
return res;
}
int main(int argc, char **argv)
{
polynome *p1, *p2, *p3;
p1 = initialiser();
afficher(p1);
p2 = initialiser();
afficher(p2);
p3 = ajouter(p1, p2);
afficher(p3);
free(p1);
free(p2);
free(p3);
exit(0);
} |
Partager