Created
November 10, 2014 04:41
-
-
Save jofese/1924aad21c62ae77710f to your computer and use it in GitHub Desktop.
Implementacion de Un automata con Pila en c++
This file contains 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
/* | |
Autor: Joel Cesar Fernandez Segura | |
Curso: Tecnicas De Construccion de Programas | |
Ejercicio: Implementacion de un Automata Con Pila | |
IDE: CodeBlocks | |
Pagina Web: http://codebotic.blogspot.com | |
*/ | |
#include<iostream> | |
#include<cstdlib> | |
using namespace std; | |
struct nodo{ | |
char a;//campo donde se almacenara el caracter | |
struct nodo *sgte; | |
}; | |
typedef struct nodo *pila; | |
/*---------------Funcion para Apilar un Caracter--------*/ | |
void apila(pila &p,const char a){ | |
pila q=new (struct nodo) ; | |
q->a=a; | |
q->sgte=p; | |
p=q; | |
} | |
/*------------- Funcion para Desapilar un elemento -----*/ | |
void desapila(pila &p){ | |
int n=p->a; | |
pila q=p; | |
p=p->sgte; | |
delete(q); | |
} | |
/*------Funcion que decidir si se apila o desapila----*/ | |
int recorrer(int e,char letra, pila &p){ | |
const char a='x'; | |
if(e==0&&letra=='a'){ | |
apila(p,a); | |
return 0; | |
} | |
if(e==0&&letra=='b'){ | |
desapila(p); | |
return 1; | |
} | |
if(e==1&&letra=='b'){ | |
desapila(p); | |
return 1; | |
} | |
return 0; | |
} | |
/*---------- Lee la palabra Ingresada -------------*/ | |
void leer(char pal[], pila &p){ | |
int estado=0; | |
int i=0; | |
cout<<"\te. inicial//letra\n"; | |
while(pal[i]){ | |
cout<<"\t"<<estado<<" "<<pal[i]<<endl; | |
estado=recorrer(estado,pal[i],p); | |
i++; | |
} | |
if(estado==1&&p==NULL) | |
cout<<"\n\tPalabra aceptada"; | |
else | |
cout<<"\n\tPalabra rechazada"; | |
} | |
/*----------------Funcion Principal----------------*/ | |
int main(void){ | |
system("color 0a"); | |
pila p=NULL;//inicializamos la pila | |
cout<<"\n\n\t*****AUTOMATA CON PILA*******\n\n"; | |
char w[50]="aabb";//Cambiar la Palabra Aqui...!!!! | |
leer(w,p); | |
cout<<endl<<endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment