Advertisement
ricardorichsn

exer_iv.c

Sep 16th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX 100
  6.  
  7. void incluir(char cpf[], char nome[], char end[], char tel[], char site[]);
  8. int do_hash(char cpf[]);
  9. void popular();
  10. void imprimirTudo();
  11. void imprimir(int tmp);
  12. int consultar(char cpf[]);
  13. void imprimirColuna(int tmp, int col);
  14.  
  15. char lista[MAX][4][30];
  16. int tab_hash[MAX];
  17. int itab_hash = 0;
  18.  
  19. int main() {
  20.    
  21.     int op, col, tmp;
  22.     char cpf[20];
  23.    
  24.     popular();
  25.    
  26.     do{
  27.         printf("1-Consultar por CPF\n2-Imprimir Lista\n3-Escolha os campos a serem visualizados\n");
  28.         scanf("%d", &op);
  29.         switch(op) {
  30.             case 1:
  31.                 system("clear");
  32.                 printf("Digite o CPF: ");
  33.                 scanf("%s", cpf);
  34.                 tmp = consultar(cpf);
  35.                 imprimir(tmp);
  36.                 break;
  37.             case 2:
  38.                 system("clear");
  39.                 imprimirTudo();
  40.                 break;
  41.             case 3:
  42.                 system("clear");
  43.                 do{
  44.                     printf("Escolha\n1-Nome\n2-Endereço\n3-Telefone\n4-Site\n");
  45.                     scanf("%d", &col);
  46.                 }while(col < 1 || col > 4);
  47.                 printf("Digite o CPF: ");
  48.                 scanf("%s", cpf);
  49.                 tmp = consultar(cpf);
  50.                 imprimirColuna(tmp, col);
  51.                 break;
  52.             default:
  53.                 break;
  54.            
  55.         }
  56.     }while(op != 4);
  57.    
  58.     imprimirTudo();
  59.     printf("%d", consultar("189.128.982-11"));
  60.    
  61.     return 0;
  62. }
  63.  
  64. void incluir(char cpf[], char nome[], char end[], char tel[], char site[]) {
  65.    
  66.     int iagenda=0;
  67.    
  68.     iagenda = do_hash(cpf);
  69.    
  70.     strcpy(lista[iagenda][0], cpf);
  71.     strcpy(lista[iagenda][1], nome);
  72.     strcpy(lista[iagenda][2], end);
  73.     strcpy(lista[iagenda][3], tel);
  74.     strcpy(lista[iagenda][4], site);
  75.    
  76.     tab_hash[itab_hash++] = iagenda;
  77.    
  78. }
  79.  
  80. int do_hash(char cpf[]) {
  81.    
  82.     int i, key=0;
  83.    
  84.     for(i=0; i<strlen(cpf); i++) {
  85.         key += (cpf[i] - '1');
  86.     }
  87.    
  88.     return key%100;
  89. }
  90.  
  91. int consultar(char cpf[]) {
  92.    
  93.     int iagenda=0;
  94.    
  95.     iagenda = do_hash(cpf);
  96.    
  97.     return iagenda;
  98.    
  99. }
  100.  
  101. void imprimirTudo() {
  102.    
  103.     int i, tmp;
  104.    
  105.     for(i=0; i<itab_hash; i++) {
  106.         tmp = tab_hash[i];
  107.         imprimir(tmp);
  108.     }
  109.    
  110. }
  111.  
  112. void imprimir(int tmp) {
  113.     printf("ID: %d\n", tmp);
  114.     printf("\tCPF: %s\n", lista[tmp][0]);
  115.     printf("\tNome: %s\n", lista[tmp][1]);
  116.     printf("\tEndereço: %s\n", lista[tmp][2]);
  117.     printf("\tTelefone: %s\n", lista[tmp][3]);
  118.     printf("\tSite: %s\n", lista[tmp][4]);
  119. }
  120.  
  121. void imprimirColuna(int tmp, int col) {
  122.     printf("ID: %d\n", tmp);
  123.     printf("%s\n", lista[tmp][col]);
  124. }
  125.  
  126. void popular() {
  127.    
  128.     incluir("189.128.982-11", "ricardo nunes", "rua esquilinho", "1234-1234", "www.esquilinho.com");
  129.     incluir("123.346.858-33", "esquilinho nunes", "rua esquilinho", "1234-1234", "www.esquilinho.com");
  130.     incluir("999.222.111-88", "artur linux", "rua linux", "1234-1234", "www.4linux.com");
  131.     incluir("242.675.975-24", "esquilinho nunes", "rua esquilinho", "1234-1234", "www.esquilinho.com");
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement