Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "TADSubstring.h"
- //-------------------------------------------------------------------------------------------------------------------------
- FILE * IOAbreArquivo(char nomeDoArq[MAX], char *mode){
- FILE *fp;
- fp=fopen (nomeDoArq , mode);
- if (!fp)
- {
- printf ("Erro na abertura do arquivo. Fim de programa.\n");
- getc(stdin);
- exit (1);
- }
- return(fp);
- }
- //------------------------------------------------------------------------------------------------------------------------
- void LeEntrada(FILE* Arq, char* texto, int* tamanho){
- fscanf(Arq,"%d",&*tamanho);
- fscanf(Arq, "%s",&*texto);
- }
- int CalculaIndice (char* substring){
- printf("%s\n", substring);
- int i, valor, aux;
- printf("%d\n", strlen(substring));
- valor=0;
- for(i=0; i<strlen(substring); i++){
- aux=((substring[i]- 97 )*i)%100;
- valor+=aux;
- }
- return valor;
- }
- void InsereSubstring(char* substring, char** tabela, int* ocorrencias ){
- int indice;
- printf("%s\n", substring);
- indice=CalculaIndice(substring);
- printf("%d\n", indice);
- if(tabela[indice]!=NULL){
- if(!strcmp(tabela[indice], substring)) ocorrencias[indice]++;
- else{
- //while(tabela[indice]!=NULL)indice++;
- tabela[indice]=substring;
- ocorrencias[indice]++;
- }
- }
- else {
- tabela[indice]=substring;
- ocorrencias[indice]++;
- }
- printf("indice: %d string:%s\n",indice, tabela[indice]);
- }
- void LeitorDeSubstring(char* texto, int tamanho, int* proximo, char** string){
- printf("\n\n\a%s\n", texto);
- int i,j,k;
- j=0;
- char substring[tamanho+1];
- k=*proximo;
- for(i=0;i<tamanho;i++){
- substring[j]=texto[k];
- printf("substring[%d]: %c\n", j,texto[k]);
- j++;
- k++;
- }
- (*proximo)+=1;
- substring[j]='\0';
- *string=substring;
- }
- void Imprime (char **tabela, int*vetor, int tamTexto, int tamanho){
- int indice;
- FILE* saida;
- saida=IOAbreArquivo("output.txt","a");
- indice=ProcuraIndiceSenha(vetor,tamTexto,tamanho);
- fprintf(saida,"%s\n", tabela[indice]);
- printf("%s\n", tabela[12]);
- printf("%d\n", indice);
- fclose(saida);
- }
- void HashSenha(FILE* Arq, int tamanho,int* ocorrencias,int *tamTexto, char* caminho[]){
- int i;
- int *temp;
- char **tabela, *texto;
- char **string, *stringaux;
- Arq=IOAbreArquivo(caminho[1],"r");
- texto=malloc(2000*sizeof(char));
- fscanf(Arq,"%d", &i);
- printf("%d\n", i);
- fscanf(Arq,"%s",&*texto);
- printf("%s\n", texto);
- tabela=malloc(sizeof(char**));
- for(i=0; i<=100; i++) {
- tabela[i]=malloc(sizeof(char*));
- tabela[i]=NULL;
- }
- temp=malloc(sizeof(int));
- stringaux=malloc(sizeof(char*));
- string=malloc(sizeof(char**));
- for(i=0; i<tamanho; i++)string[i]=malloc(sizeof(char*));
- *temp=0;
- *tamTexto=strlen(texto);
- while(*temp<*tamTexto-tamanho+1){
- LeitorDeSubstring(texto, tamanho,temp, string);
- printf("a: %s\n", *string);
- //InsereSubstring(*string,tabela,ocorrencias);
- int indice;
- strncpy(stringaux,*string,tamanho);
- printf("b : %s\n", stringaux);
- printf("a: %s\n", *string);
- indice=CalculaIndice(stringaux);
- printf("a: %s\n", stringaux);
- printf("c : %s\n", tabela[indice]);
- if(tabela[indice]!=NULL){
- if(!strcmp(tabela[indice], *string)) ocorrencias[indice]++;
- else{
- while(tabela[indice]!=NULL)indice++;
- tabela[indice]=stringaux;
- ocorrencias[indice]++;
- }
- }
- else {
- tabela[indice]=stringaux;
- ocorrencias[indice]++;
- }
- printf("indice: %d string:%s\n",indice,tabela[indice]);
- }
- Imprime(tabela,ocorrencias,*tamTexto,tamanho);
- }
- int ProcuraIndiceSenha(int* vetor, int tamTexto, int tamanho ){
- int temp, i,j;
- j=0;
- i=1;
- temp=vetor[0];
- while(i!=100){
- if(temp<vetor[i]){
- temp=vetor[i];
- printf("temp: %d %d\n",i, temp);
- j=i;
- }
- i++;
- }
- for(i=0;i<tamTexto;i++)printf("%d ",vetor[i]);
- printf("\n%d\n",tamTexto);
- return j;
- }
Add Comment
Please, Sign In to add comment