Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <time.h>
  6.  
  7. #define MAXSENHAS 430000
  8.  
  9. struct senha{
  10. char *palavra;
  11. int tamanho;
  12. int frequencia;
  13. };
  14. typedef struct senha Senha;
  15.  
  16. void LerSenhas(Senha **senhas, char *filepath)
  17. {
  18. FILE *fp;
  19. int n = 0, tam, freq;
  20. char pass[200];
  21.  
  22. fp = fopen(filepath, "r");
  23.  
  24. while (fscanf(fp, "%d %d %[^\n]", &tam, &freq, pass) == 3)
  25. {
  26. senhas[n] = (Senha*)malloc(sizeof(Senha));
  27. senhas[n]->tamanho = tam;
  28. senhas[n]->frequencia = freq;
  29. senhas[n]->palavra = (char*)malloc((tam+1) * sizeof(char));
  30. pass[tam] = '\0';
  31. strcpy(senhas[n]->palavra, pass);
  32. n++;
  33. }
  34.  
  35. fclose(fp);
  36. }
  37.  
  38. void BubbleSort(Senha **senhas)
  39. {
  40. int n;
  41. char *temp;
  42. bool troca = true;
  43. int nSenhas = MAXSENHAS;
  44. while (nSenhas > 0 && troca == true)
  45. {
  46. troca = false;
  47. for (n = 0; n < nSenhas - 1; n++)
  48. {
  49. if (strcmp(senhas[n]->palavra, senhas[n+1]->palavra) > 0)
  50. {
  51. troca = true;
  52. temp = senhas[n]->palavra;
  53. senhas[n]->palavra = senhas[n+1]->palavra;
  54. senhas[n+1]->palavra = temp;
  55. }
  56. }
  57. nSenhas--;
  58. }
  59. }
  60.  
  61. int main(){
  62. int n;
  63.  
  64. Senha** senhas;
  65. senhas = (Senha**)malloc(MAXSENHAS * sizeof(Senha*));
  66. LerSenhas(senhas, "senhas.txt");
  67.  
  68. clock_t begin = clock();
  69. BubbleSort(senhas);
  70. clock_t end = clock();
  71. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  72. printf("%f\n", time_spent);
  73. printf("%s", senhas[0]->palavra);
  74.  
  75. for (n = 0; n < MAXSENHAS ; n++)
  76. free(senhas[n]);
  77. free(senhas);
  78.  
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement