Guest User

Untitled

a guest
Jun 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "TADSubstring.h"
  5.  
  6.  
  7. //-------------------------------------------------------------------------------------------------------------------------
  8.  
  9. FILE * IOAbreArquivo(char nomeDoArq[MAX], char *mode){
  10.  
  11. FILE *fp;
  12.  
  13. fp=fopen (nomeDoArq , mode);
  14.  
  15. if (!fp)
  16.  
  17. {
  18.  
  19. printf ("Erro na abertura do arquivo. Fim de programa.\n");
  20.  
  21. getc(stdin);
  22.  
  23. exit (1);
  24.  
  25. }
  26.  
  27. return(fp);
  28.  
  29. }
  30.  
  31. //------------------------------------------------------------------------------------------------------------------------
  32.  
  33. void LeEntrada(FILE* Arq, char* texto, int* tamanho){
  34. fscanf(Arq,"%d",&*tamanho);
  35. fscanf(Arq, "%s",&*texto);
  36. }
  37.  
  38.  
  39.  
  40. int CalculaIndice (char* substring){
  41. printf("%s\n", substring);
  42. int i, valor, aux;
  43. printf("%d\n", strlen(substring));
  44. valor=0;
  45. for(i=0; i<strlen(substring); i++){
  46. aux=((substring[i]- 97 )*i)%100;
  47. valor+=aux;
  48. }
  49. return valor;
  50. }
  51.  
  52. void InsereSubstring(char* substring, char** tabela, int* ocorrencias ){
  53. int indice;
  54. printf("%s\n", substring);
  55. indice=CalculaIndice(substring);
  56. printf("%d\n", indice);
  57. if(tabela[indice]!=NULL){
  58. if(!strcmp(tabela[indice], substring)) ocorrencias[indice]++;
  59. else{
  60. //while(tabela[indice]!=NULL)indice++;
  61. tabela[indice]=substring;
  62. ocorrencias[indice]++;
  63. }
  64. }
  65. else {
  66. tabela[indice]=substring;
  67. ocorrencias[indice]++;
  68. }
  69. printf("indice: %d string:%s\n",indice, tabela[indice]);
  70. }
  71.  
  72. void LeitorDeSubstring(char* texto, int tamanho, int* proximo, char** string){
  73. printf("\n\n\a%s\n", texto);
  74. int i,j,k;
  75. j=0;
  76. char substring[tamanho+1];
  77. k=*proximo;
  78. for(i=0;i<tamanho;i++){
  79. substring[j]=texto[k];
  80. printf("substring[%d]: %c\n", j,texto[k]);
  81. j++;
  82. k++;
  83. }
  84. (*proximo)+=1;
  85. substring[j]='\0';
  86. *string=substring;
  87. }
  88.  
  89. void Imprime (char **tabela, int*vetor, int tamTexto, int tamanho){
  90. int indice;
  91. FILE* saida;
  92. saida=IOAbreArquivo("output.txt","a");
  93. indice=ProcuraIndiceSenha(vetor,tamTexto,tamanho);
  94. fprintf(saida,"%s\n", tabela[indice]);
  95. printf("%s\n", tabela[12]);
  96. printf("%d\n", indice);
  97. fclose(saida);
  98. }
  99.  
  100. void HashSenha(FILE* Arq, int tamanho,int* ocorrencias,int *tamTexto, char* caminho[]){
  101. int i;
  102. int *temp;
  103. char **tabela, *texto;
  104. char **string, *stringaux;
  105. Arq=IOAbreArquivo(caminho[1],"r");
  106. texto=malloc(2000*sizeof(char));
  107. fscanf(Arq,"%d", &i);
  108. printf("%d\n", i);
  109. fscanf(Arq,"%s",&*texto);
  110. printf("%s\n", texto);
  111. tabela=malloc(sizeof(char**));
  112. for(i=0; i<=100; i++) {
  113. tabela[i]=malloc(sizeof(char*));
  114. tabela[i]=NULL;
  115. }
  116. temp=malloc(sizeof(int));
  117. stringaux=malloc(sizeof(char*));
  118. string=malloc(sizeof(char**));
  119. for(i=0; i<tamanho; i++)string[i]=malloc(sizeof(char*));
  120. *temp=0;
  121. *tamTexto=strlen(texto);
  122. while(*temp<*tamTexto-tamanho+1){
  123. LeitorDeSubstring(texto, tamanho,temp, string);
  124. printf("a: %s\n", *string);
  125. //InsereSubstring(*string,tabela,ocorrencias);
  126. int indice;
  127. strncpy(stringaux,*string,tamanho);
  128. printf("b : %s\n", stringaux);
  129. printf("a: %s\n", *string);
  130. indice=CalculaIndice(stringaux);
  131. printf("a: %s\n", stringaux);
  132. printf("c : %s\n", tabela[indice]);
  133. if(tabela[indice]!=NULL){
  134. if(!strcmp(tabela[indice], *string)) ocorrencias[indice]++;
  135. else{
  136. while(tabela[indice]!=NULL)indice++;
  137.  
  138. tabela[indice]=stringaux;
  139. ocorrencias[indice]++;
  140. }
  141. }
  142. else {
  143. tabela[indice]=stringaux;
  144. ocorrencias[indice]++;
  145. }
  146. printf("indice: %d string:%s\n",indice,tabela[indice]);
  147. }
  148. Imprime(tabela,ocorrencias,*tamTexto,tamanho);
  149. }
  150.  
  151. int ProcuraIndiceSenha(int* vetor, int tamTexto, int tamanho ){
  152. int temp, i,j;
  153. j=0;
  154. i=1;
  155. temp=vetor[0];
  156. while(i!=100){
  157. if(temp<vetor[i]){
  158. temp=vetor[i];
  159. printf("temp: %d %d\n",i, temp);
  160. j=i;
  161. }
  162. i++;
  163. }
  164. for(i=0;i<tamTexto;i++)printf("%d ",vetor[i]);
  165. printf("\n%d\n",tamTexto);
  166. return j;
  167. }
Add Comment
Please, Sign In to add comment