ce code permet l evaluation d une expression par génération d'arbre interne!!
Source / Exemple :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include <math.h>
#define MAXPILE 200
#define VIDE -1
typedef float type_val;
const type_val PAR_FERMEE=1;
const type_val PAR_OUVERTE=2;
const type_val POWER=3;
const type_val FOIS=4.1;
const type_val DIV=4.2;
const type_val MOD=4.3;
const type_val PLUS=5.1;
const type_val MOINS=5.2;
struct pile
{
type_val val[MAXPILE];
int sommet;
};
/************************************************************************
int sommet(struct pile *p,type_val *x);
int est_vide(struct pile *p);
int est_pleine(struct pile *p);
/*1*/void initialiser_pile(struct pile *p);
/*2*/int push(struct pile *p,type_val x);
/*3*/int pop(struct pile *p,type_val *x);
/*4*/void saisir_exp(char expression[]);
/*5*/int calculer(type_val x, char op, type_val y, struct pile *p_val);
/***/int calcul_prio(type_val opt, struct pile *p_val);
/*6*/int get_operand(char exp[],int *i_deb,struct pile *p_val,struct pile *p_op);
/*8*/float coder_operateur(char op);
/*9*/char decoder_operateur(type_val cod);
/************************************************************************
*
void aide()
{
clrscr();
puts("ÉÍÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»");
puts("ºÂÁÂÁÂÁÂÁÂÁºOp?rateurs math?matiques : + - * / % ^ e º");
puts("ºÂÁÂÁÂÁÂÁÂÁÌÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹");
puts("ºEvaluateurºparenthèses autorisées : ( ) º");
puts("ºÄÄÄÄd'ÄÄÄÄÌÍÍÍÍÍÍÍÍÍËÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͹");
puts("ºexpressionºCommande º FIN : met fin ? la d?monstration.º");
puts("ºÂÁÂÁÂÁÂÁÂÁº º HLP : affiche cette aide. º");
puts("ºÂÁÂÁÂÁÂÁÂÁº º CLS : efface l'?cran. º");
puts("ÈÍÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÊÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ");
}
/************************************************************************
*
int sommet(struct pile *p,type_val *x)
{
if (p->sommet>=0)
{
return 1;
}
else
return 0;
}
/************************************************************************
*
int est_vide(struct pile *p)
{
return (p->sommet<0);
}
/************************************************************************
*
int est_pleine(struct pile *p)
{
return (p->sommet==MAXPILE-1);
}
/************************************************************************
*
void initialiser_pile(struct pile *p)
{
p->sommet=VIDE;
}
/************************************************************************
- 2- empiler dans la pile 2
*
int push(struct pile *p,type_val x)
{
if (p->sommet< MAXPILE-1)
{
p->val[++(p->sommet)]=x;
return 1;
}
else
return 0;
}
/************************************************************************
*
int pop(struct pile *p,type_val *x)
{
if (p->sommet>=0)
{
return 1;
}
else
return 0;
}
/************************************************************************
*
void pass_espace(char exp[],int *i_deb)
{
while(exp[*i_deb]==' ')
(*i_deb)++;
}
/************************************************************************
*
void saisir_exp(char exp[])
{
int i=0;
char c;
textcolor(15);
cprintf("\rexpression>> ");
textcolor(7);
gets(exp);
/*
do
{
c=getch();
if(isdigit(c) ||c=='^'||c=='*' ||c=='/' ||
c=='+' ||c=='-' ||c=='(' || c==')' || c=='.')
{
exp[i]=c;
putchar(c);
i++;
}
else
if(c==8)
i--;
}
while(c!=13 );
exp[i]='\0';
putchar('\n');*/
}
/************************************************************************
*
int get_operand(char exp[],int *i_deb,struct pile *p_val,struct pile *p_op) {
type_val val=0,diz=10,par,e;
char signe=1;
pass_espace(exp,i_deb);
if(*i_deb==0)
if(exp[*i_deb]=='-')
{
push(p_op,FOIS);
push(p_val,-1);
(*i_deb)++;
}
else if(exp[*i_deb]=='+')
{
(*i_deb)++;
}
pass_espace(exp,i_deb);
while(exp[*i_deb]=='(')
{
if(!push(p_op,PAR_OUVERTE))
{
printf("Erreur153: depassement de capacit?\a\n");
return 0;
}
(*i_deb)++;
pass_espace(exp,i_deb);
}
if(!*i_deb || (sommet(p_op,&par) && par == PAR_OUVERTE))
if(exp[*i_deb]=='+')
(*i_deb)++;
else
if(exp[*i_deb]=='-')
{
(*i_deb)++;
signe=-signe;
}
pass_espace(exp,i_deb);
if(isdigit(exp[*i_deb]))
{
diz=10;
while(isdigit(exp[*i_deb]))
{val=val*diz+exp[(*i_deb)++]-48;
// pass_espace(exp,i_deb);
}
if(exp[*i_deb]=='.')
{
(*i_deb)++ ;
// pass_espace(exp,i_deb);
if (isdigit(exp[*i_deb]))
{
diz=1.0;
while(isdigit(exp[*i_deb])&& diz<=1000000.0) //precision float
{
diz*=10;
val=val+(exp[(*i_deb)++]-48)/diz;
// pass_espace(exp,i_deb);
}
if(diz>1000000.0)
{
printf("180 precision erreur : 6 chiffre après la virgule\n");
getch();
return 0;
}
}
}
if(exp[*i_deb]=='e')
{
(*i_deb)++ ;
pass_espace(exp,i_deb);
if (isdigit(exp[*i_deb]))
{
e=0;
while(isdigit(exp[*i_deb])&& e<38)
{ e=e*10+exp[(*i_deb)++]-48;
// pass_espace(exp,i_deb);
}
if (e>=38)
{
printf("erreur 243:exposant trop grand\n");
return 0;
}
else
val=val*pow(10,e);
}
else
{
printf(" erreur 239: exposant invalide\n");
return 0;
}
}
if(push(p_val,val*signe))
return 1;
else
{
printf("Erreur200: mémoire insuffisante\a\n");
return 0;
}
}
else
{
printf("198 erreur :operande introuvable\n");
return 0;
}
}
/************************************************************************
*
type_val get_operator(char exp[],int *i)
{
pass_espace(exp,i);
if(exp[*i]=='(')return ((*i)++,PAR_OUVERTE);
else
if(exp[*i]==')')return ((*i)++,PAR_FERMEE);
else
if(exp[*i]=='^')return ((*i)++,POWER);
else
if(exp[*i]=='*')return ((*i)++,FOIS);
else
if(exp[*i]=='/')return ((*i)++,DIV);
else
if(exp[*i]=='%')return ((*i)++,MOD);
else
if(exp[*i]=='+')return ((*i)++,PLUS);
else
if(exp[*i]=='-')return ((*i)++,MOINS);
else
return 0;
}
/************************************************************************
*
char decoder_operateur(type_val cod)
{
if(cod>0)
{
if(cod<=PAR_OUVERTE)return '(';
else
if (cod<=PAR_FERMEE)return ')';
else
if(cod<=POWER)return'^';
else
if(cod<=FOIS)return '*';
else
if(cod<=DIV)return '/';
else
if(cod<=MOD)return '%';
else
if(cod<=PLUS)return '+';
else
if(cod<=MOINS)return'-';
else
return 0;
}
else
return 0;
}
/************************************************************************
*
int calculer(type_val x,float op, type_val y, struct pile *p_val)
{
type_val res;
if(op>PAR_OUVERTE)
{
if(op<=POWER)
{if(!(y-int(y)))
{
res=pow(x,y);
}
else
{
printf("Erreur 347: la puissance n'est un entier\n");
return 0;
}
}
else
if(op<=FOIS)
res=x*y;
else
if(op<=DIV)
{ if(y==0)
{
printf("272 Erreur : division par 0");
return 0;}
res=x/y;
}
else
if(op<=MOD)
{if(y)
{
if(!(x-int(x)) && !(y-int(y)))
{
if(y<0)
{y=-y;
x=-x;
}
res=(int(x)%int(y)+int(y))%int(y);
}
else
{
printf("Erreur 376: modulo operande =>entiers \n");
return 0;
}
}
else
{
printf("Erreur 388: modulo 0 \n");
return 0;
}
}
else
if(op<=PLUS)
res=x+y;
else
if(op<=MOINS)
res=x-y;
return push(p_val,res );
}
else
return 0;
}
/**************************************************************************
*
int calcul_prio(type_val opt, struct pile *p_val)
{
type_val opd1,opd2;
if (pop(p_val,&opd2) && pop(p_val,&opd1))
return(calculer(opd1,opt,opd2,p_val));
else
return 0;
}
/**************************************************************************
*
int evaluer_expression(char exp[])
{
struct pile p_val,p_op;
float opt1,opt2,opd1,opd2,res;
int i=0;
textcolor(14);
cprintf("\rresultat>>>> ");
initialiser_pile(&p_op);
initialiser_pile(&p_val);
while(exp[i])
{
if(get_operand(exp,&i,&p_val,&p_op) )
{
if((opt2=get_operator(exp,&i))>PAR_OUVERTE)
{
while(opt1=0,pop(&p_op,&opt1)&& int(opt1)<=int(opt2) && opt1>PAR_OUVERTE)
calcul_prio(opt1, &p_val);
if (int(opt1)>int(opt2) || opt1==PAR_OUVERTE)
push(&p_op, opt1);
push(&p_op, opt2);
}
else
if(opt2== PAR_FERMEE)
{
while(opt2==PAR_FERMEE)
{
while(pop( &p_op,&opt1)&& opt1>PAR_OUVERTE)
calcul_prio(opt1, &p_val);
if(opt1==PAR_OUVERTE)
{
if((opt2=get_operator(exp,&i))>PAR_OUVERTE)
{
while(opt1=0,pop(&p_op,&opt1)&& int(opt1)<=int(opt2) && opt1>PAR_OUVERTE)
calcul_prio(opt1, &p_val);
if (int(opt1)>int(opt2)||opt1==PAR_OUVERTE)
push(&p_op, opt1);
push(&p_op, opt2);
}
}
else
if(exp[i]||opt1<PAR_OUVERTE)
{printf(" erreur349 parenthese\n");return 0;}
}
}
else if(exp[i])
{printf("ERREUR 399 : operateur invalide\n"); return 0;}
}else if(exp[i])return 0;
}
if(!est_vide(&p_val))
{
while(pop(&p_val,&opd2))
{
if(pop(&p_val,&opd1))
if(pop(&p_op,&opt2)&& opt2>PAR_OUVERTE)
{
if(!calculer(opd1,opt2, opd2, &p_val))
return 0;
}
else
{
printf("ERREUR 421: parenthese\n");
return 0;
}
}
if(est_vide(&p_op))
{
if(opd2>-1000000 && opd2<1000000)
cprintf("%f\n",opd2);
else
cprintf("%e\n",opd2);
textcolor(7);
return 1;
}
else
{
printf("380 erreur parenthese\n");
return 0;
}
}
else printf("Taper une expression\n");
}
/*************************************************************************
*
int main(void)
{
struct pile pile_val,pile_op;
type_val val;
int i;
char choix[4][4]={"FIN","HLP","CLS"};
char exp[100];
clrscr();
textcolor(7);
aide();
do
{
saisir_exp(exp);
i=0;
while(i<3 && strcmpi(exp,choix[i]))i++;
switch(i)
{
case 0:break;
case 1:aide();
break;
case 2:clrscr();
break;
default: { evaluer_expression(exp);
}
}
}
while(i);
}
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.