Liste chainée scanf float

prog3000 -  
 loupius -
URGENT SVP

Bonjour,
J'ai développé le code suivant concernant la gestion des polynômes le problème est que si je prend le coéfficient de type int pas de problème par contre si je le prend float ou double il me donne des résultats erronés
voila le code :

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct poly
{
float cof;
int degre;
struct poly *suivant;
};
struct poly *initialiser (struct poly *debut)
{
debut=NULL;
return(debut);
}
int vide(struct poly *p)
{
if(p==NULL)
return(1);
else
return(0);
}
struct poly *ajouter(struct poly *debut,struct poly *p,int d,float c)
{
int trv=0;
if(vide(p))
{
p=debut;
p->suivant=(struct poly*)malloc(sizeof(struct poly));
p->degre=d;
p->cof=c;
p=p->suivant;
p->suivant=NULL;
}
else
{
p=debut;
do
{
if(p->degre==d)
{
trv=1;
p->cof+=c;
}
p=p->suivant;
}while(p->suivant!=NULL);
if(trv==0)
{
p->suivant=(struct poly*)malloc(sizeof(struct poly));
p->degre=d;
p->cof=c;
p=p->suivant;
p->suivant=NULL;
}
}
return(p);
}
void afficher(struct poly *debut,struct poly *p)
{
int i=0;
printf("\nP= ");
p=debut;
if (p == NULL )
{
printf("Le polynome n'est pas d‚fini");
exit (0);
}
else
{
while (p->suivant!= NULL )
{
if (p->cof<0)
printf(" %fX^%d",p->cof,p->degre);
else
if (p->cof>0)
if(i==0)
printf("%fX^%d",p->cof,p->degre);
else
printf("+ %fX^%d",p->cof,p->degre);
i++;
p = p->suivant;
}
}
}
int menu()
{
int ch;
textcolor(4);
textbackground(6);
cprintf("\n\n**** OPERATIONS SUR LES POLYNOMES ****\n\n");
printf("\r\n");
textbackground(7);
cprintf("1- ADDITION\r\n");
textbackground(7);
cprintf("2- SOUSTRACTION\r\n");
textbackground(7);
cprintf("3- MULTIPLICATION\r\n");
textbackground(7);
cprintf("4- DIVISION\r\n");
textbackground(7);
cprintf("5- DERIVATION DE P(X)\r\n");
textbackground(7);
cprintf("6- INTEGRATION DE P(X)\r\n");
textbackground(7);
cprintf("7- AFFICHAGE\r\n");
textbackground(7);
cprintf("8- Quitter\r\n");
printf("\n");
printf("Votre choix : ");
scanf("%d",&ch);
return(ch);
}
void saisie(struct poly *debut,struct poly *p)
{
int dg,rep;
float cf;
do{
fflush(stdin);
printf("Co‚fficient : ");
scanf("%f",&cf);
fflush(stdin);
printf("Degr‚ : ");
scanf("%d",&dg);
p=ajouter(debut,p,dg,cf);
fflush(stdin);
printf("Continuer : ");
scanf("%d",&rep);
}while(rep!=0);
}
struct poly *somme(struct poly *pol1,struct poly *pol2,struct poly *db3,struct poly *pol3)
{
fflush(stdin);
do{
pol3=ajouter(db3,pol3,pol1->degre,pol1->cof);
pol1=pol1->suivant;
}while(pol1->suivant!=NULL);
fflush(stdin);
do{
pol3=ajouter(db3,pol3,pol2->degre,pol2->cof);
pol2=pol2->suivant;
}while(pol2->suivant!=NULL);
return(pol3);
}
struct poly *mul(struct poly *db1,struct poly *pol1,struct poly *db2,struct poly *pol2,struct poly *db3,struct poly *pol3)
{
for(pol1=db1;pol1->suivant!=NULL;pol1=pol1->suivant)
for(pol2=db2;pol2->suivant!=NULL;pol2=pol2->suivant)
pol3=ajouter(db3,pol3,pol1->degre+pol2->degre,pol1->cof*pol2->cof);
return(pol3);
}
int degre_max(struct poly *debut,struct poly *p)
{
int max;
p=debut;
max=p->degre;
do
{
if(p->degre>=max)
max=p->degre;
p=p->suivant;
}while(p->suivant!=NULL);
return(max);
}
struct poly *chgs(struct poly *debut ,struct poly *p)
{
p=debut;
do{
p->cof=-p->cof;
p=p->suivant;
}while(p->suivant!=NULL);
return(p);
}
int retc(struct poly *debut,struct poly *p)
{
float cf;
p=debut;
do
{
if(p->degre==degre_max(debut,p))
cf=p->cof;
p=p->suivant;
}while(p->suivant!=NULL);
return(cf);
}
poly *div(struct poly *db1,struct poly *pol1,struct poly *db2,struct poly *pol2,struct poly *db3,struct poly *pol3)
{
poly *dbrst,*rst;
pol3=initialiser(db3);
rst=initialiser(dbrst);
do{
if(degre_max(db2,pol2)>=degre_max(db1,pol1))
{
pol3=ajouter(db3,pol3,degre_max(db2,pol2)-degre_max(db1,pol1),retc(db2,pol2)/retc(db1,pol1));
pol3=chgs(db3 ,pol3);

rst=somme(mul(db3,pol3,db1,pol1,db3,pol3),pol2,dbrst,rst);
}
}while(degre_max(dbrst,rst)<degre_max(db1,pol1));
return(pol3);
}
void main()
{
clrscr();
struct poly *debut1,*debut2,*debut3,*p1,*p2,*p3;
int rep,choix;
p1=initialiser(debut1);
saisie(debut1,p1);
p2=initialiser(debut2);
saisie(debut2,p2);

p3=initialiser(debut3);
p1=debut1;
p2=debut2;
fflush(stdin);
p3=somme(p1,p2,debut3,p3);
afficher(debut3,p3);
getch();
}

</gras>
A voir également:

1 réponse

loupius
 
Au lieu d'utiliser les balises <gras>, tu ferais mieux d'utiliser les balises <code>. C'est beaucoup plus sympa pour les éventuels chercheurs de problèmes dont j'aurais pu faire partie... ;-) Là, je ne chercherais même pas car je n'y arrive pas.
Désolé, ce sera peut-être pour une prochaine fois.
0

Discussions similaires