oui c'est un peu gros je sais mais je remerci d'autant plus celui qui prendra le temps de m'expliquer ou je me plante ^^.
en gros ce programme doit organiser dans un arbre une expression de la forme 4+8*6-5+7*9 en respectant les prioritées.
ce que j'ais écris est sencé créer l'arbre et l'afficher. il ne gere (il ne compile pas donc le mot est un peu exagéré^^) que les signes * et / pour l'instant pas les - et +.
quand je compile: j'ai ces erreurs en boucle
calc.c: In function 'arbre':
calc.c:43: error: 'noeud' has no member named 'valD'
calc.c:47: error: 'noeud' has no member named 'valD'
calc.c:47: error: 'noeud' has no member named 'valG'
calc.c:60: error: 'noeud' has no member named 'filsG'
calc.c:61: error: 'noeud' has no member named 'filsD'
#include<stdio.h>
#include<string.h>
typedef struct{
void* noeud;
int num;
noeud filsG; /** je ne sais pas comment déclarer mes fils gauche et droit. */
noeud filsD;
float valG;
float valD;
} noeud;
noeud arbre(void *chaine);
void afficher_arbre ( noeud root);
int main(){
char chaine[2048];
printf("coucou, entre ton calcul.\n" );
scanf("%s",chaine);
afficher_arbre(arbre(chaine));
return 0;
}
noeud arbre(void *chaine){
static int cpt=1;
int lect=0;
char *chaineG;
char *chaineD;
char *cs='\0' ;
char *ns='\0' ;
noeud root;
root.num = cpt;
cpt++;
lect=sscanf(chaine,"%[* / + - ]%[* / + - ]",cs,ns);
/** pour sortir du récursif pblm ***************************/
if (lect==1){
float a,b;
sscanf(chaine,"%f%[+-*/]%f%[+-*/]",&a,cs,&b,ns);
if(ns=='\0')
root.valD=b;
return root;
}
else if (lect==0){
sscanf(chaine,"%f%*[+-*/]%f",&root.valD,&root.valG);
return root;
}
/** **************************************************************************/
/** gestion des prioritées entre le signe courant(cs) et le suivant(cn) ***********/
/** cas 1 ***********/
else if ((strcmp(cs,"*\0" )==0 ||strcmp(cs,"/\0" )==0 )&&(strcmp(ns,"*\0" )==0 ||strcmp(ns,"/\0" )==0 )){
chaineD=(cs+1);
strncpy(chaineG,chaine,strlen(chaine)-strlen(chaineD));
strcpy(root.noeud, cs);
root.filsG=(arbre(chaineG));
root.filsD=(arbre(chaineD));
return root;
}
/** cas 2 ***********/
/** cas 3 ***********/
/** cas 4 ***********/
/** fin des cas ***********/
return root;
}
void afficher_arbre ( noeud root){
if (root.filsG!=NULL && root.filsD!=NULL){
afficher_arbre ( root.filsG );
printf("%s",root.noeud);
afficher_arbre ( root.filsD );
}
else if (root.filsG!=NULL && root.filsD==NULL){
afficher_arbre ( root.filsG );
printf("%s",root.noeud);
afficher_arbre ( root.valD );
}
else if (root.filsG==NULL && root.filsD==NULL){
afficher_arbre ( root.valG );
printf("%s",root.noeud);
afficher_arbre ( root.valD );
}
}