Correction TD3 SD
Correction TD3 SD
Correction TD3 SD
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define m 60
//---------------------------------------
typedef struct element
{
char chaine;
struct element *suiv;
}element;
//---------------------------------------
typedef struct
{
element *Pr;
element *Dr;
int nb;
}pile;
//---------------------------------------
int est_vide(pile *pi)
{
if(pi->nb == 0) return 1;
return 0;
}
//---------------------------------------
void initialiser_pile(pile *pi)
{
pi->Dr = NULL;
pi->Pr = NULL;
pi->nb = 0;
}
//---------------------------------------
pile *creer_pile()
{
pile *pi;
pi = (pile*)malloc(sizeof(pile));
initialiser_pile(pi);
return pi;
}
//---------------------------------------
element *creer_Element(char chaine)
{
element *e;
e = (element*)malloc(sizeof(element));
e->chaine = chaine;
e->suiv = NULL;
return e;
}
//---------------------------------------
void empiler(pile *pi,char chaine)
{
element *e;
e = creer_Element(chaine);
if(est_vide(pi))
{
pi->Pr = e;
pi->Dr = e;
}
else
{
e->suiv = pi->Pr;
pi->Pr = e;
}
pi->nb++;
}
//---------------------------------------
void affichage_pile(pile *pi)
{
if(est_vide(pi))
{
printf("rien a afficher \n");return;
}
element *e;e = pi->Pr;
while(e != NULL)
{
printf("%c ",e->chaine);
e = e->suiv;
}
printf("\n");
}
//---------------------------------------
int depiler(pile *pi)
{
if(est_vide(pi)) return -1;
element *e;e = pi->Pr;
if(pi->nb == 1)
{
initialiser_pile(pi);
}
else
{
pi->Pr = e->suiv;
pi->nb--;
}
free(e);
e = NULL;
return 0;
}
//---------------------------------------
char sommet(pile *pi)
{
return pi->Pr->chaine;
}
//---------------------------------------
void suppression_pile(pile *pi)
{
while(!est_vide(pi))
depiler(pi);
}
//---------------------------------------
int valide(pile *pi,char *chaine) {
int i=0;
int lenght;
lenght=strlen(chaine);
for (i = 0; i < lenght; i++)
{
if( chaine[i] == '[' || chaine[i] == '(' || chaine[i] == '{' )
empiler(pi,chaine[i]);
else if( chaine[i] == ']')
{
if(!est_vide(pi) && sommet(pi) == '[') depiler(pi);
else return 0;
}
else if( chaine[i] == ')')
{
if(!est_vide(pi) && sommet(pi) == '(') depiler(pi);
else return 0;
}
else if(chaine[i] == '}')
{
if(!est_vide(pi) && sommet(pi) == '{') depiler(pi);
else return 0;
}
}
return est_vide(pi);
}
//---------------------------------------
int menu()
{
int choix;
//-----------------------------------------
int estOperateur(char *chaine)
{
if(strcmp(chaine,"+")==0)
return 1;
if(strcmp(chaine,"-")==0)
return 1;
if(strcmp(chaine,"/")==0)
return 1;
if(strcmp(chaine,"*")==0)
return 1;
return 0;
}
//-----------------------------------------
int calculer(char *op,char *a,char *b)
{
int C1=atoi(a);
int C2=atoi(b);
if(strcmp(op,"+")==0)
return C1+C2;
if(strcmp(op,"-")==0)
return C1-C2;
if(strcmp(op,"*")==0)
return C1*C2;
if(strcmp(op,"/")==0 && C2!=0)
return C1/C2;
}
//-----------------------------------------
int polonaise(char **T,int n,pile *pi) {
int i;
for(i=0;i<n;i++) {
char *resultat;
resultat =(char*)malloc(n*sizeof(char));
int res;
if(estOperateur(T[i])) {
char *b=sommet(pi);
depiler(pi);
char *a=sommet(pi);
depiler(pi);
res=calculer(T[i],a,b);
itoa(res,resultat,10);
empiler(pi,resultat);
}
else
{
empiler(pi,T[i]);
}
}
return atoi(sommet(pi));
}
//-----------------------------------------
char **saisirTab(int *n)
{
printf("\n Saisir la dimension du tableau : ");
scanf("%d",n);
char **T;
int i;
T = (char**)malloc((*n)*sizeof(char*));
for(i =0;i<*n;i++)
{
T[i] = (char*)malloc((*n)*sizeof(char));
printf("Saisir T[%d] : ",i);
fflush(stdin);
gets(T[i]);
}
return T;
}
//-----------------------------------------
int menu() {
int choix;
printf("\n\n\t******************* MENU *******************\n");Sleep(200);
printf("\n\t 1 ) Calculer avec la notation polonaise\n");Sleep(200);
printf("\n\t 0 ) Quitter\n");Sleep(200);
printf("\n\n\t\t Saisir votre choix : ");
scanf("%d",&choix);
return choix;
}