Bonjour a tous,
j'ai un problème avec mon programme en c, la fonction qui calcul la dérivée d'un polynôme ne me donne pas le résultat attendu, voila le code
bibliotheque.h
Code:struct poly{ int n; float *c; }; typedef struct poly poly; void affichage(poly p); float valeur(poly p, float x); poly creer(poly p, int n); poly saisir(poly p); poly derive(poly p); poly somme(poly p, poly q); poly produit(poly p, poly q);
bibliotheque.c
polynome.cCode:#include<stdio.h> #include<stdlib.h> #include<math.h> #include "bibliotheque.h" /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ void affichage(poly p) { int i; for(i=p.n;i>=0;i--) printf("%3.2f x^%d +",p.c[i],i); } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ float valeur(poly p, float x){ int i; float y=0; for(i=p.n;i>=0;i--) y+=(pow(x,i)*p.c[i]); return y; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ poly creer(poly p, int n){ p.n=n; p.c=(float*)malloc((p.n+1)*sizeof(float)); return p; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ poly saisir(poly p){ int i; for(i=p.n;i>=0;i--) { printf("\nSaisir a%d coeff de degre %d\n",i,i); scanf("%f",&p.c[i]); printf("P[%d] = %3.2f\n",i,p.c[i]); } return p; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ poly derive(poly p){ int i; poly q; q.n=p.n-1; q=creer(q,q.n); for(i=p.n;i>=0;i--) { q.c[i]=p.c[i]*i; } return q; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ poly somme(poly p, poly q){ int i; poly r; if(p.n>=q.n) { r.n=p.n; r=creer(r,r.n); printf("\nLe degre de p=%d >= au degre de q=%d\n",p.n,q.n); for(i=0;i<=q.n;i++) r.c[i]=p.c[i]+q.c[i]; for(i=q.n+1;i<=p.n;i++) r.c[i]=p.c[i]; } else { r.n=q.n; r=creer(r,r.n); printf("\nLe degre de p=%d < au degre de q=%d\n",p.n,q.n); for(i=0;i<=p.n;i++) r.c[i]=p.c[i]+q.c[i]; for(i=p.n;i<=q.n;i--) r.c[i]=q.c[i]; r.n=q.n; } return f; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/ poly produit(poly p, poly q){ int i,j,k; poly r; r.n=p.n+q.n; r=creer(r,r.n); for(i=0;i<=r.n;i++) r.c[i]=0; for(j=0;j<=p.n;j++) for(k=0;k<=q.n;k++) r.c[j+k]+=p.c[j]*q.c[k]; return r; } /*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
cordialementCode:#include<stdio.h> #include<stdlib.h> #include<math.h> #include "bibliotheque.h" void main(){ poly p,q,s,r; int n; printf("\nDonnez le degre du polynome :\n"); scanf("%d",&n); p=creer(p,n); p=saisir(p); affichage(p); printf("\nLe polynome derive est :\n"); p=derive(p); affichage(p); q=creer(q,n); q=saisir(q); affichage(q); printf("\nLa somme donne :\n"); s=somme(p,q); affichage(s); printf("\nLe produit donne :\n"); r=produit(p,q); affichage(r); printf("\nSaisir une valeur de x :\n"); scanf("%f",&x); printf("La valeur du polynome en x=%3.2f est P(x)=%3.2f\n",x,valeur(p,x)); }
-----