Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
NOTATION POLONAISE INVERSE
Information sur la source
Description
Ce code réalise ce qu'on appelle la notation polonaise inverse. elle consiste à effectuer des opérations arithmétiques étant donné des opérateurs et des opérandes.les opérandes son tplacés avant les opérateurs(par exemple 5 6 + càd 5+6) (autre exemple 5 6 + 2 3 * - càd (5+6)-(2*3)).Pour cela j'ai mis en oeuvre la notion de pile.SI vous executer ce programme essayer de respecter la régle de saisie comme indiqué dans le s2 exemples précedents. Vous verez que vous n'aurez le droit de saisir que des entiers entre 0 et 9.si vous examinez bien le code vous verez pourqoui et vous verez aussi qu'il est très facile de le modifier si vous voulez entrez des entiez positifs supérieur à 9. Pour simplifier on utilise que des entiers positifs.
Source
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
-
-
- struct ma_pile{
- char info;
- struct ma_pile *suiv;
- };
-
- /****************** declaration Variables globales *****************/
-
- struct ma_pile *sommet;
- char p1;
- char ch[20],c;
- int i=0,code=0;
- char tempo[2];
-
- /****************** ce qui suit est des fonctions *********************/
-
-
- struct ma_pile *Init(struct ma_pile **sommet)
- {
- *sommet=NULL;
- return *sommet;
- }
-
-
- int char_to_int(char c)
- {
- if(c=='0')
- return code==0;
- else
- {
- code=c-48;
- return code;
- }
- }
-
-
- char int_to_char(int int_to_ch)
- {
- int_to_ch+=48;
- c=(char)int_to_ch; // converti l'ascii int_to_char en caractere
- return c;
- }
-
-
- void EMP(char x,struct ma_pile **sommet)
- {
- struct ma_pile *p;
-
- p=(struct ma_pile *)malloc(sizeof(struct ma_pile));
- p->info=x;
- p->suiv=*sommet;
- *sommet=p;
- }
-
-
- int DEP(struct ma_pile **sommet)
- {
- if(*sommet==NULL)
- printf("E R R E U R ! LA PILE EST VIDE");
- else
- {
- struct ma_pile *p=*sommet;
- p1=p->info;
- *sommet=p->suiv;
- free(p);
- printf("\nOPERATION SUCCESS\n");
- printf("VALEUR DEPILEE :%d\n",char_to_int(p1));
- return p1;
- }
-
- }
-
-
- void saisie(char t[1])
- {
- char c;
- int j1=0;
-
- do
- {
- c=getch();
- if(((c>='0')&& (c<='9')) || ((c=='*')|| (c=='-')|| (c=='+')|| (c=='/')))
- {
- if((c>='0')&& (c<='9'))
- {
- j1++; //comptabilise les caracteres entre '0' et '9'
- if(j1>2)
- printf("ACTION NON AUTORISEE\n");
- else
- {
- t[i]=c;
- printf("%c\n",t[i]);
- i++;
- }
- }
-
- if((c=='*')|| (c=='-')|| (c=='+')|| (c=='/'))
- {
- j1=0;
- t[i]=c;
- printf("%c\n",t[i]);
- i++;
-
- }
- }
- }
- while(c!=13);
- }
-
- void VISU(struct ma_pile *sommet)
- {
- if(sommet!=NULL)
- {
- struct ma_pile *p=sommet;
-
- while(p) {
- printf("%c ->",p->info);
- p=p->suiv; }
- }
- else
- printf("E R R E U R ! VEUILLEZ D'ABORD REMPLIR LA PILE ");
- }
-
-
- void Traitement(char t[1])
- {
- int k,k1,code2=0,code1=0,resultat=0;
-
- char c;
- for(k=0;k<i;k++)
- {
- if ((t[k]>='0') &&(t[k]<='9'))
- EMP(t[k],&sommet);
-
- if((t[k]=='*')|| (t[k]=='-')|| (t[k]=='+')|| (t[k]=='/'))
- {k1=1; //on commence … d‚piler
-
- while((sommet!=NULL) && (k1>=0)) //ne depile que les 2 1ere valeurs
- {
- DEP(&sommet);
- tempo[k1]=p1;
- k1--;
- }
- code1=char_to_int(tempo[0]);
- code2=char_to_int(tempo[1]);
-
- switch (t[k])
- {
- case '+':resultat=code1+code2;printf("%d+%d=%d\n",code1,code2,resultat);
- break;
- case '-':resultat=code1-code2;printf("%d-%d=%d\n",code1,code2,resultat);
- break;
- case '*':resultat=code1*code2;printf("%d*%d=%d\n",code1,code2,resultat);
- break;
- case '/':resultat=code1/code2;printf("%d/%d=%d il s'agit d'une division entiŠre\n",code1,code2,resultat);
- break;
- }
-
- EMP(int_to_char(resultat),&sommet);
-
- }
-
- }
-
- }
-
-
-
-
- /***********************
- Fin des fonctions **********/
-
-
- /***********************
- Ceci est mon programme****/
-
- void main()
- {
- clrscr();
- saisie(ch);
- Init(&sommet);
- Traitement(ch);
- getch();
- }
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct ma_pile{
char info;
struct ma_pile *suiv;
};
/****************** declaration Variables globales *****************/
struct ma_pile *sommet;
char p1;
char ch[20],c;
int i=0,code=0;
char tempo[2];
/****************** ce qui suit est des fonctions *********************/
struct ma_pile *Init(struct ma_pile **sommet)
{
*sommet=NULL;
return *sommet;
}
int char_to_int(char c)
{
if(c=='0')
return code==0;
else
{
code=c-48;
return code;
}
}
char int_to_char(int int_to_ch)
{
int_to_ch+=48;
c=(char)int_to_ch; // converti l'ascii int_to_char en caractere
return c;
}
void EMP(char x,struct ma_pile **sommet)
{
struct ma_pile *p;
p=(struct ma_pile *)malloc(sizeof(struct ma_pile));
p->info=x;
p->suiv=*sommet;
*sommet=p;
}
int DEP(struct ma_pile **sommet)
{
if(*sommet==NULL)
printf("E R R E U R ! LA PILE EST VIDE");
else
{
struct ma_pile *p=*sommet;
p1=p->info;
*sommet=p->suiv;
free(p);
printf("\nOPERATION SUCCESS\n");
printf("VALEUR DEPILEE :%d\n",char_to_int(p1));
return p1;
}
}
void saisie(char t[1])
{
char c;
int j1=0;
do
{
c=getch();
if(((c>='0')&& (c<='9')) || ((c=='*')|| (c=='-')|| (c=='+')|| (c=='/')))
{
if((c>='0')&& (c<='9'))
{
j1++; //comptabilise les caracteres entre '0' et '9'
if(j1>2)
printf("ACTION NON AUTORISEE\n");
else
{
t[i]=c;
printf("%c\n",t[i]);
i++;
}
}
if((c=='*')|| (c=='-')|| (c=='+')|| (c=='/'))
{
j1=0;
t[i]=c;
printf("%c\n",t[i]);
i++;
}
}
}
while(c!=13);
}
void VISU(struct ma_pile *sommet)
{
if(sommet!=NULL)
{
struct ma_pile *p=sommet;
while(p) {
printf("%c ->",p->info);
p=p->suiv; }
}
else
printf("E R R E U R ! VEUILLEZ D'ABORD REMPLIR LA PILE ");
}
void Traitement(char t[1])
{
int k,k1,code2=0,code1=0,resultat=0;
char c;
for(k=0;k<i;k++)
{
if ((t[k]>='0') &&(t[k]<='9'))
EMP(t[k],&sommet);
if((t[k]=='*')|| (t[k]=='-')|| (t[k]=='+')|| (t[k]=='/'))
{k1=1; //on commence … d‚piler
while((sommet!=NULL) && (k1>=0)) //ne depile que les 2 1ere valeurs
{
DEP(&sommet);
tempo[k1]=p1;
k1--;
}
code1=char_to_int(tempo[0]);
code2=char_to_int(tempo[1]);
switch (t[k])
{
case '+':resultat=code1+code2;printf("%d+%d=%d\n",code1,code2,resultat);
break;
case '-':resultat=code1-code2;printf("%d-%d=%d\n",code1,code2,resultat);
break;
case '*':resultat=code1*code2;printf("%d*%d=%d\n",code1,code2,resultat);
break;
case '/':resultat=code1/code2;printf("%d/%d=%d il s'agit d'une division entiŠre\n",code1,code2,resultat);
break;
}
EMP(int_to_char(resultat),&sommet);
}
}
}
/***********************
Fin des fonctions **********/
/***********************
Ceci est mon programme****/
void main()
{
clrscr();
saisie(ch);
Init(&sommet);
Traitement(ch);
getch();
}
Conclusion
pour toutes suggestions ou remarques,veuillez me contactez
Sources de la même categorie
Commentaires
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | 1 | 2 | 3 | 4 | 5 | 6 |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | | | |
|
|