Created
September 9, 2010 20:32
-
-
Save StuMx/572502 to your computer and use it in GitHub Desktop.
Convertir notación infija a posfija.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
typedef struct Nodo{ | |
int info; | |
struct Nodo *sig; | |
} *Pila; | |
Pila empty(){ | |
return NULL; | |
} | |
Pila push(int e, Pila p){ | |
Pila t=(Pila)malloc(sizeof(struct Nodo)); | |
t->info=e; | |
t->sig=p; | |
return t; | |
} | |
int isempty(Pila p){ | |
return (p==NULL); | |
} | |
int top(Pila p){ | |
return (p->info); | |
} | |
Pila pop(Pila p){ | |
return (p->sig); | |
} | |
int esoperando(char); | |
int esoperador(char); | |
int precedencia(char); | |
int esmayoroigual(int, int); | |
char esizq(char); | |
int main(int argc, char *argv[]) | |
{ | |
char *s,c; | |
Pila p=empty(); | |
s=*++argv; | |
while((c=*s++)!='\0'){ | |
if (esizq(c)) | |
p=push(c,p); | |
else if (esoperando(c)) | |
printf("%c", c); | |
else if (esoperador(c)) | |
if (isempty(p)|| esizq(top(p))) | |
p=push(c,p); | |
else{ | |
while(esmayoroigual(precedencia(top(p)),precedencia(c))){ | |
printf("%c",top(p)); | |
p=pop(p); | |
}; | |
p=push(c,p); | |
} | |
else{ | |
while(top(p)!='('){ | |
printf("%c", top(p)); | |
p=pop(p); | |
} | |
p=pop(p); | |
} | |
} | |
while(!isempty(p)){ | |
printf("%c",top(p)); | |
p=pop(p); | |
} | |
return 0; | |
} | |
char esizq(char c){ | |
return c=='('; | |
} | |
int esoperando(char a){ | |
return (a>='a' && a<='z')||(a>='A' && a<='Z')||(a>='0' && a<='9'); | |
} | |
int esoperador(char a){ | |
return (a=='+'|| a=='-' || a=='*' || a=='/' || a=='!'); | |
} | |
int precedencia(char a){ | |
switch (a){ | |
case '(': return 0; break; | |
case '+': return 1; break; | |
case '-': return 1; break; | |
case '*': return 2; break; | |
case '/': return 2; break; | |
case '!': return 3; break; | |
} | |
} | |
int esmayoroigual(int x, int y){ | |
return (x>=y); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment