Advertisement
pablosoares

Untitled

Aug 30th, 2020
1,840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "hash.h"
  5.  
  6. struct hash{
  7.     int qtd,TABLE_SIZE;
  8.     city **itens;
  9.     };
  10.    
  11. int vet[854],k=0;
  12.  
  13.  
  14. int criptografia(char c[]){
  15.    
  16.     int x=0,i;
  17.    
  18.         for(i=0;i<strlen(c);i++)x+=31*(int)c[i];
  19.    
  20.     return x;
  21.    
  22.     }
  23.  
  24. void ler(city ci[]){
  25.    
  26.      FILE *arq;
  27.    
  28.            
  29.      arq = fopen("cidades1.txt","r");
  30.      
  31.      char c;
  32.      int i=0,k=0;
  33.      
  34.         while((!feof(arq))){
  35.            
  36.             fscanf(arq,"%c",&c);
  37.            
  38.             if(c=='\n'){
  39.                
  40.            
  41.                
  42.                
  43.                 ci[k].c[i]='\0';
  44.                
  45.                
  46.                 k++;
  47.                 i=0;
  48.                
  49.                 }
  50.            
  51.             else{
  52.                
  53.                 ci[k].c[i]=c;
  54.                 i++;
  55.            
  56.            
  57.             }
  58.                
  59.             }
  60.            
  61.                 ci[k].c[i]=c;
  62.                
  63.                 ci[k].c[i]='\0';
  64.                
  65.                
  66.  
  67.    
  68.     fclose(arq);
  69.  
  70.    
  71.     }
  72.  
  73. int chaveDivisao(int chave, int TABLE_SIZE){
  74.      return (chave&0x7FFFFFFF) % TABLE_SIZE;
  75.  }
  76.  
  77. int chaveDobra(int chave, int TABLE_SIZE){
  78.    
  79.      int num_bits = 10;
  80.      int parte1 = chave >> num_bits;
  81.      int parte2 = chave & (TABLE_SIZE-1);
  82.  
  83.      return (parte1 ^ parte2);
  84.  
  85.  }
  86.  
  87.  
  88.    
  89. int insereHash(Hash *ha,city cidade[]){
  90.    
  91.      if(ha == NULL || ha->qtd == ha->TABLE_SIZE){
  92.          return 0;
  93.      }
  94.  
  95.      int chave = criptografia(cidade[k].c); //MUDAR AQUI CASO NÃO SEJA DESSA FORMA:  int chave = a1.matricula
  96.      
  97.      k++;
  98.      
  99.      int pos = chaveDivisao(chave,ha->TABLE_SIZE);
  100.  
  101.      city *novo;
  102.  
  103.      novo = (city*)malloc(sizeof(city));
  104.  
  105.      if(novo == NULL){
  106.          return 0;
  107.      }
  108.      
  109.      novo = cidade; //No do backes *novo = al
  110.      
  111.      ha->itens[pos] = novo;
  112.      ha->qtd++;
  113.      
  114.      return 1;    
  115.  
  116.      }
  117. void inserir(city ci[]){
  118.        
  119.      Hash *ha=criaHash(10);
  120.  
  121.      insereHash(ha,ci);
  122.        
  123.      }
  124.  
  125. Hash* criaHash(int TABLE_SIZE){
  126.    
  127.     Hash* ha =(Hash*) malloc(sizeof(Hash));
  128.    
  129.     if(ha!=NULL){
  130.        
  131.        int i;
  132.        
  133.        ha->TABLE_SIZE=TABLE_SIZE;
  134.        ha->itens=(city**)malloc(TABLE_SIZE*sizeof(city*));
  135.        
  136.         if(ha->itens==NULL){
  137.            
  138.             free(ha);
  139.             return NULL;
  140.            
  141.            }
  142.         ha->qtd=0;
  143.         for(i=0;i<ha->TABLE_SIZE;i++){
  144.             ha->itens[i]=NULL;
  145.             }
  146.            
  147.        }
  148.        return ha;
  149.     }
  150.    
  151.  
  152.  
  153. void liberaHash(Hash *ha){
  154.      if(ha != NULL){
  155.          int i;
  156.          for(i=0; i < ha->TABLE_SIZE; i++){
  157.              if(ha->itens[i] != NULL){
  158.                  free(ha->itens[i]);
  159.              }
  160.          }
  161.          free(ha->itens);
  162.          free(ha);
  163.      }
  164.  }
  165.  
  166.  
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement