Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct candidato{
  5. long long int inscr;
  6. char nome[44];
  7. int periodo;
  8. char turno[3];
  9. int posicao;
  10. char curso[30];
  11. };typedef struct candidato Candidato;
  12.  
  13. #define N 4073
  14. // considerando q sao 2036 registros, uma taxa de ocupaçao de 50% precisaria de 4072 posiçoes no vetor. 4073 é o menor numero primo que é maior de 4072
  15.  
  16.  
  17. /* -------------- PARTE DA HASH -------------- */
  18.  
  19. struct elementoLista{
  20. long long inscr;
  21. int posicao;
  22. struct elementoLista *prox;
  23. }; typedef struct elementoLista ElemLista;
  24.  
  25. typedef ElemLista* Hash[N]; //define o tipo Hash, um vetor com N posiçoes, com ponteiros para os structs ElemLista
  26.  
  27. int hashf(int num){ // funçao que calcula o indice.
  28. return (num % N);
  29. }
  30.  
  31. void zerarHash(Hash tab){ // bota toda a tabela hash como null, para nao dar problemas depois
  32. int i;
  33. for (i = 0; i < N; i++){
  34. tab[i] = NULL;
  35. }
  36. }
  37.  
  38. void insereNaHash(long long int inscr, int posicao, Hash tab){
  39. //pega a inscr e a posiçao em que está e coloca isso na tabela hash
  40. int h = hashf(inscr);
  41. ElemLista *aux, *novo = (ElemLista*)malloc(sizeof(ElemLista));
  42. novo->inscr = inscr; novo->posicao = posicao; novo->prox = NULL;
  43. if (tab[h] == NULL)
  44. tab[h] = novo;
  45. else {
  46. aux = tab[h];
  47. tab[h] = novo;
  48. novo->prox = aux;
  49. }
  50. }
  51.  
  52. /* -------------- FIM DA PARTE DA HASH -------------- */
  53.  
  54.  
  55.  
  56. /* -------------- PARTE DO INDICE -------------- */
  57.  
  58. /* OBS : O indice guarda a inscriçao e qual a posição dela no arquivo candidatos.dat
  59. Depois, tendo uma inscr, calcula-se a hash, e vendo no indice, acha-se a posiçao do registro com aquela inscriçao no arquivo original
  60. Então, vai-se no arquivo original e acessa diretamente aquela posiçao.*/
  61.  
  62. struct elementoIndice{
  63. long long int inscr;
  64. int posicao;
  65. };typedef struct elementoIndice ElemIndice;
  66.  
  67. typedef ElemIndice* Indice[N]; //vetor com N posiçoes com ponteiros para structs ElemIndice
  68.  
  69. void salvarIndice(Hash tab){
  70. FILE *arq = fopen("candidatos.idx", "w");
  71. int i;
  72. for (i = 0; i < N; i++){
  73. fwrite(tab[i], sizeof(long long int),1, arq);
  74. fwrite(&i, sizeof(int),1, arq);
  75. }
  76. fclose(arq);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement