CENTRO UNIVERSITARIO UAEM
ATLACOMULCO
LICENCIATURA EN
INGENIERÍA EN COMPUTACIÓN
NOMBRE DEL DOCENTE:
INGENIERO.
HÉCTOR CABALLERO HERNÁNDEZ
NOMBRE MATERIA:
AUTÓMATAS Y LENGUAJES FORMALES
NOMBRE DE LA ALUMNA:
HEIVILINA PÉREZ ARIAS
GRUPO:
ICO-19
TURNO:
MATÚTINO
TRABAJO A ENTREGAR:
CODIGO EN C++ PARA
REALIZAR LA EVALUACION DE UN AUTOMATA FINITO DETERMINISTA
FECHA DE ENTREGA:
MAYO DE 2015.
CODIGO EN
C++ PARA REALIZAR LA EVALUACION DE UN AUTOMATA FINITO DETERMINISTA #include
<iostream>
#include
<vector>
#include
<algorithm>
#include
<string>
#include
<stdlib.h>
#define MAX
100
using
namespace std;
void
VisualizarEstados(vector<int> p){
cout << "\n\n LOS ESTADOS DEL
AUTOMATA SON \n\n";
for(int n=0;n<p.size(); n++){
cout << " " << n
<<" --> q" << p[n] << endl;
}
}
void
DigitarAutomata(vector<int> &E, vector<char> &S, int
T[MAX][MAX]){
int numeroDeEstados;
int numeroDeSimbolos;
char h;
cout << "\n Cuantos estados
desea introducir : ";
cin >> numeroDeEstados;
for(int n=0; n<numeroDeEstados; n++){
E.push_back(n);
}
cout << "\n Cuantos simbolos
desea introducir: "; cin >> numeroDeSimbolos;
cout << endl;
for(int n=0; n<numeroDeSimbolos; n++){
cout << "\t Simbolo "
<< n+1 << " : ";
cin >> h;
S.push_back(h);
}
sort(S.begin(),S.end()); // Orden de los simbolos
cout << "\n INTRODUCCION DE
TABLA DE TRANSICIONES \n\n";
for(int n=0;n<numeroDeEstados;n++){
for(int m=0; m<numeroDeSimbolos;
m++){
cout << "\t
T["<<n<<"]["<<m<<"] : ";
cin >> T[n][m];
}
}
cout<<"\n\n MUESTRA DE TABLA DE
TRANSICIONES \n\n";
for(int n=0; n<numeroDeEstados;n++){
for(int m=0;m<numeroDeSimbolos;m++){
cout << "\t"
<< T[n][m] << " ";
}
cout<<endl;
}
}
void menu(){
cout << "\n\t\t EVALUACION DE UN
AUTOMATA FINITO DETERMINISTA\n\n";
cout << "\t 1. Digitar Automata
\n";
cout << "\t 2. Evaluar
palabra \n";
cout << "\t 3. Salir \n";
cout << "\t Elija la opcion
deseada: ";
}
bool
RectificarPalabra(vector<int> w, int T[MAX][MAX], int q0,
vector<int>qf){
int q, s;
q = q0;
bool EstadoRectificacion = false;
for(int n=0;n<w.size();n++){
s = w[n];
q = T[q][s];
}
for(int n=0;n<qf.size();n++){
if(q==qf[n]){
EstadoRectificacion = true;
break;
}
}
return EstadoRectificacion;
}
void
EvaluarPalabra(string palabra, vector<char>S, vector<int>&w){
int n, k=0;
while(w.size()!=palabra.length()){
for(n=0;n<S.size();n++){
if(palabra[k]==S[n]){
w.push_back(n);
}
}
k++;
}
}
int main(){
cout<<"\nCENTRO
UNIVERSITARIO UAEM ATLACOMULCO"<<endl;
cout<<"\nAUTOMATAS Y LENGUAJES
FORMALES"<<endl;
cout<<"\nELABORADO POR:
"<<endl;
cout<<" ----> HEIVILINA PEREZ ARIAS"<<endl;
vector<int> Estados;
vector<char> Simbolos;
int Transiciones[MAX][MAX];
int q0;
vector<int>qf;
vector<int>w;
bool AutomataIntroducido = false;
int opcion, temp, tamano;
do{
menu();
cin>>opcion;
switch(opcion){
case 1:
Estados.clear();
Simbolos.clear();
qf.clear();
DigitarAutomata(Estados,
Simbolos, Transiciones);
VisualizarEstados(Estados);
cout << "\n Digite
su estado inicial: ";
cin >> q0;
cout << "\n Estados
finales con los que cuenta su automata: ";
cin >> tamano;
cout << endl;
for(int n=0;n<tamano;n++){
cout << "\t Cual
es el numero de estado final: ";
cin >> temp;
qf.push_back(temp);
}
AutomataIntroducido = true;
break;
case 2:
if(AutomataIntroducido){
string palabra;
w.clear();
bool EstadoRectificacion =
false;
cout << "\n
Ingrese palabra: ";
cin>> palabra;
EvaluarPalabra(palabra,
Simbolos, w);
EstadoRectificacion
=RectificarPalabra(w, Transiciones, q0, qf);
if(EstadoRectificacion){
cout <<
"\n\t La palabra ingresada a sido aceptada :) \n\n";
}
else{
cout <<
"\n\t La palabra ingresada a sido rechazada :( \n\n";
}
}
else{
cout << "\n Por
favor primero digite su automata a evaluar \n";
}
break;
case 3:
exit(0);
default:
cout << "\n\tOpcion
incorrecta... :( !\n";
}
}while(opcion!=3);
system("pause");
return EXIT_SUCCESS;
}
No hay comentarios:
Publicar un comentario