Advertisement
ferseg

fuerza bruta

Nov 12th, 2014
4,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. /*
  2.  
  3.   Ejercicio: Algoritmo de Fuerza Bruta
  4.   Autor:   Joel Fernandez
  5.   Fecha: 13/06/2014 8:45 p.m.
  6.   IDE: CodeBlocks
  7.   Pagina Web: http://codebotic.blogspot.com
  8.   Descripcion: Implementacion de algoritmo Fuerza Bruta de casamiento de cadenas
  9.  
  10.  */
  11. #include<iostream>
  12. #include <conio.h>
  13. #include<string.h>
  14. #include<time.h>
  15.  
  16. #define maxPatron 50
  17. #define maxTexto 500
  18.  
  19.  using namespace std;
  20.  
  21. void FuerzaBruta(char [],char [], int, int); //declaramos la funcion FuerzaBruta
  22.  
  23. int main(void)
  24. {
  25. system("color 0a");
  26. char Texto[maxTexto];
  27. char Patron[maxPatron];
  28. float Tiempo,inicio,fin;
  29. int n,m;
  30. cout<<"\t****************************************************\n";
  31. cout<<"\t***\t\tALGORITMO FUERZA BRUTA \t\t ***\n";
  32. cout<<"\t****************************************************\n\n\n";
  33.  
  34. cout<<"INGRESE TEXTO:";
  35. gets(Texto);
  36. n=strlen(Texto);
  37. do{ cout<<"\n\nINGRESE PATRON:";
  38.     cin>>Patron;
  39.     m=strlen(Patron);
  40.     if( m > n )
  41.     cout<<"'\n\n\t\t#Error# El texto debe ser mayor o igual que el Patron. \n\n";
  42.     }while(m>n); //validamos que el patron sea menor que el texto
  43.  
  44. inicio=clock();                  //inicio del tiempo
  45. FuerzaBruta(Texto,Patron,n,m);   //llamamos a la funcion FuerzaBruta
  46. fin=clock();                     //captura el tiempo realizado hasta esta linea
  47. Tiempo=((fin-inicio)/CLOCKS_PER_SEC);//calculamos el tiempo
  48. cout<<"\n\nTIEMPO:"<<Tiempo<<endl;
  49.  
  50. getch();
  51. return 0;
  52.  
  53. }
  54. /////////FUERZA BRUTA//////////////
  55.  
  56. void FuerzaBruta(char texto[],char patron[], int n, int m)
  57. { int i,j,k, cont=0;
  58.   char temp[100];
  59.   for(i=0; i<=n;i++)
  60.     { for(j=i,k=0;j<m;j++,k++)
  61.       temp[k]=texto[i+k];
  62.       temp[k]='\0';
  63.       if(strcmp(patron,temp)==0)
  64.          {cout<<"\nCASAMIENTO EN LA POSICION  : "<<i<<"\n";
  65.          cont++;}
  66.          m++;
  67.       }
  68.       cout<<"\n\nOCURRENCIAS :"<<cont<<endl;
  69.  
  70.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement