salykroui
2016-04-21, 10:46
مرحبا ممكن ترجمة ها>ا البرنامج الى ال pascalاو ******** algorithmique
typedef struct sElement
{
int nb;
struct sElement *suiv;
} TPile;
TPile * InitPile (TPile *sommet);
TPile * Empiler (TPile *sommet, int nb);
int PileVide (TPile *sommet);
TPile * Depiler (TPile *sommet, int *nb);
void AfficherPile (TPile *sommet);
/* pile.c */
#include <stdio.h>
#include <stdlib.h>
#include "pile.h"
TPile *InitPile (TPile *sommet)
{
sommet = NULL;
return sommet;
}
TPile * Empiler (TPile *sommet, int nb)
{
TPile *nouveau;
nouveau = (TPile *) malloc(sizeof (TPile));
if (nouveau == NULL)
printf("Erreur d'al******** de memoire -- pile pleine\n");
else
{
nouveau->nb = nb;
nouveau->suiv = sommet;
sommet = nouveau;
}
return sommet;
}
TPile * Depiler (TPile *sommet, int *nb)
{
TPile *elmt;
if (PileVide (sommet))
exit(1);
else
{
elmt = sommet;
*nb = elmt->nb;
sommet = elmt->suiv;
}
return sommet;
}
int PileVide (TPile *sommet)
{
return (sommet == NULL);
}
Postfixée 4
/* util.h */
int VerifierNombre(char *ch, int *Nb);
int VerifierOperateur(char *ch);
char LireNombreOperateur(int *Nb);
/* util.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"util.h"
int VerifierNombre(char *ch, int *Nb)
{
char type;
int i = 0;
*Nb = 0;
while (ch[i] != '\0')
{
if ((ch[i] >= '0') && (ch[i] <= '9'))
*Nb = *Nb * 10 + ch[i] - '0';
else if ( ch[i] == '-')
{
*Nb = (*Nb) * -1;
return 1;
}
else
return 0;
i++;
}
return 1;
}
int VerifierOperateur(char *ch)
{
if (strlen(ch)==1)
if ((ch[0] == '+') || (ch[0] == '-') || (ch[0] == '*') || (ch[0] =='/'))
return 1;
else
return 0;
return 0;
}
char LireNombreOperateur(int *Nb)
{
char ch[10];
char type;
printf("donnez un nombre ou un operateur: ");
scanf("%s",ch);
if(VerifierOperateur(ch))
return ch[0];
else
if(VerifierNombre(ch, Nb))
return 'N';
else
return '.';
}
Postfixée 5
/* postfixe.h */
TPile * postfixe(TPile *sommet,char Type, int Nb);
/* postfixe.c */
#include <stdio.h>
#include <stdlib.h>
#include "pile.h"
#include "util.h"
#include "postfixe.h"
TPile * postfixe(TPile *sommet, char Type, int Nb)
{
int Nb1,Nb2;
switch(Type)
{
case 'N': sommet=Empiler(sommet, Nb);
break;
case '+':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
Nb = Nb1 + Nb2;
sommet=Empiler(sommet,Nb);
break;
case '-':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
Nb = Nb1 - Nb2;
sommet=Empiler(sommet,Nb);
break;
case '*':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
Nb = Nb1 * Nb2;
sommet=Empiler(sommet,Nb);
break;
case '/':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
if ( Nb2 != 0)
{
Nb = Nb1 / Nb2;
sommet = Empiler(sommet,Nb);
}
else
exxit(1) ;
break;
}
return sommet;
}
Postfixée 6
/* principal.c */
#include <stdio.h>
#include <stdlib.h>
#include "pile.h"
#include "util.h"
#include "postfixe.h"
void AfficherPile (TPile *sommet)
{
TPile *courant;
int i;
printf("\n------ Contenu de la pile ------\n");
for (courant = sommet,i=0; courant != NULL; courant = courant->suiv,i++)
printf("element en position %d : %d\n", i, courant->nb);
printf("-------------- Fin -------------\n\n");
}
int main(void)
{
TPile *sommet;
int Nb;
char Type;
sommet = InitPile (sommet);
do
{
Type = LireNombreOperateur(&Nb);
sommet = postfixe(sommet,Type,Nb);
AfficherPile (sommet);
}while (Type != '.');
if ( ! PileVide(sommet))
printf("\nRESULTAT = %d\n", sommet->nb);
else
prtintf(“operation non valide \n”);
return 0;
}
/* makefile */
principal: principal.o pile.o util.o postfixe.o
gcc principal.o pile.o util.o postfixe.o -o principal
principal.o: principal.c pile.h util.h postfixe.h
gcc -Wall -c principal.c
pile.o: pile.c pile.h
gcc -Wall -c pile.c
util.o: util.c util.h
gcc -Wall -c util.c
postfixe.o: postfixe.c postfixe.h pile.h
gcc -Wall -c postfixe.c
typedef struct sElement
{
int nb;
struct sElement *suiv;
} TPile;
TPile * InitPile (TPile *sommet);
TPile * Empiler (TPile *sommet, int nb);
int PileVide (TPile *sommet);
TPile * Depiler (TPile *sommet, int *nb);
void AfficherPile (TPile *sommet);
/* pile.c */
#include <stdio.h>
#include <stdlib.h>
#include "pile.h"
TPile *InitPile (TPile *sommet)
{
sommet = NULL;
return sommet;
}
TPile * Empiler (TPile *sommet, int nb)
{
TPile *nouveau;
nouveau = (TPile *) malloc(sizeof (TPile));
if (nouveau == NULL)
printf("Erreur d'al******** de memoire -- pile pleine\n");
else
{
nouveau->nb = nb;
nouveau->suiv = sommet;
sommet = nouveau;
}
return sommet;
}
TPile * Depiler (TPile *sommet, int *nb)
{
TPile *elmt;
if (PileVide (sommet))
exit(1);
else
{
elmt = sommet;
*nb = elmt->nb;
sommet = elmt->suiv;
}
return sommet;
}
int PileVide (TPile *sommet)
{
return (sommet == NULL);
}
Postfixée 4
/* util.h */
int VerifierNombre(char *ch, int *Nb);
int VerifierOperateur(char *ch);
char LireNombreOperateur(int *Nb);
/* util.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"util.h"
int VerifierNombre(char *ch, int *Nb)
{
char type;
int i = 0;
*Nb = 0;
while (ch[i] != '\0')
{
if ((ch[i] >= '0') && (ch[i] <= '9'))
*Nb = *Nb * 10 + ch[i] - '0';
else if ( ch[i] == '-')
{
*Nb = (*Nb) * -1;
return 1;
}
else
return 0;
i++;
}
return 1;
}
int VerifierOperateur(char *ch)
{
if (strlen(ch)==1)
if ((ch[0] == '+') || (ch[0] == '-') || (ch[0] == '*') || (ch[0] =='/'))
return 1;
else
return 0;
return 0;
}
char LireNombreOperateur(int *Nb)
{
char ch[10];
char type;
printf("donnez un nombre ou un operateur: ");
scanf("%s",ch);
if(VerifierOperateur(ch))
return ch[0];
else
if(VerifierNombre(ch, Nb))
return 'N';
else
return '.';
}
Postfixée 5
/* postfixe.h */
TPile * postfixe(TPile *sommet,char Type, int Nb);
/* postfixe.c */
#include <stdio.h>
#include <stdlib.h>
#include "pile.h"
#include "util.h"
#include "postfixe.h"
TPile * postfixe(TPile *sommet, char Type, int Nb)
{
int Nb1,Nb2;
switch(Type)
{
case 'N': sommet=Empiler(sommet, Nb);
break;
case '+':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
Nb = Nb1 + Nb2;
sommet=Empiler(sommet,Nb);
break;
case '-':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
Nb = Nb1 - Nb2;
sommet=Empiler(sommet,Nb);
break;
case '*':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
Nb = Nb1 * Nb2;
sommet=Empiler(sommet,Nb);
break;
case '/':
sommet = Depiler(sommet, &Nb2);
sommet = Depiler(sommet,&Nb1);
if ( Nb2 != 0)
{
Nb = Nb1 / Nb2;
sommet = Empiler(sommet,Nb);
}
else
exxit(1) ;
break;
}
return sommet;
}
Postfixée 6
/* principal.c */
#include <stdio.h>
#include <stdlib.h>
#include "pile.h"
#include "util.h"
#include "postfixe.h"
void AfficherPile (TPile *sommet)
{
TPile *courant;
int i;
printf("\n------ Contenu de la pile ------\n");
for (courant = sommet,i=0; courant != NULL; courant = courant->suiv,i++)
printf("element en position %d : %d\n", i, courant->nb);
printf("-------------- Fin -------------\n\n");
}
int main(void)
{
TPile *sommet;
int Nb;
char Type;
sommet = InitPile (sommet);
do
{
Type = LireNombreOperateur(&Nb);
sommet = postfixe(sommet,Type,Nb);
AfficherPile (sommet);
}while (Type != '.');
if ( ! PileVide(sommet))
printf("\nRESULTAT = %d\n", sommet->nb);
else
prtintf(“operation non valide \n”);
return 0;
}
/* makefile */
principal: principal.o pile.o util.o postfixe.o
gcc principal.o pile.o util.o postfixe.o -o principal
principal.o: principal.c pile.h util.h postfixe.h
gcc -Wall -c principal.c
pile.o: pile.c pile.h
gcc -Wall -c pile.c
util.o: util.c util.h
gcc -Wall -c util.c
postfixe.o: postfixe.c postfixe.h pile.h
gcc -Wall -c postfixe.c