Advertisement
ricardorichsn

exer_iii.c

Sep 2nd, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX 100
  6.  
  7. int consultar(char nome[], char sobrenome[]);
  8. void incluir(char first_name[], char last_name[], char tel[]);
  9. int do_hash(char nome[], char sobrenome[]);
  10. void popular();
  11. void imprimir();
  12.  
  13. char agenda[MAX][2][20];
  14. int tab_hash[MAX];
  15. int itab_hash = 0;
  16.  
  17. int main() {
  18.    
  19.     int tmp, op;
  20.     char nome[20], sobrenome[20];
  21.    
  22.     popular();
  23.    
  24.     do{
  25.         printf("1-Consultar por nome e sobrenome\n2-Imprimir Lista\n");
  26.         scanf("%d", &op);
  27.         switch(op) {
  28.             case 1:
  29.                 system("clear");
  30.                 printf("Digite o nome: ");
  31.                 scanf("%s", nome);
  32.                 printf("Digite o sobrenome: ");
  33.                 scanf("%s", sobrenome);
  34.                 tmp = consultar(nome, sobrenome);
  35.                 printf("ID: %d - ", tmp);
  36.                 printf("Nome: %s", agenda[tmp][0]);
  37.                 printf(" %s", agenda[tmp][1]);
  38.                 printf("\tTelefone: %s\n", agenda[tmp][2]);
  39.                 break;
  40.             case 2:
  41.                 system("clear");
  42.                 imprimir();
  43.                 break;
  44.             default:
  45.                 break;
  46.            
  47.         }
  48.     }while(op != 3);
  49.    
  50.     return 0;
  51. }
  52.  
  53. void incluir(char nome[], char sobrenome[], char tel[]) {
  54.    
  55.     int iagenda=0;
  56.    
  57.     iagenda = do_hash(nome, sobrenome);
  58.    
  59.     strcpy(agenda[iagenda][0], nome);
  60.     strcpy(agenda[iagenda][1], sobrenome);
  61.     strcpy(agenda[iagenda][2], tel);
  62.    
  63.     tab_hash[itab_hash++] = iagenda;
  64.    
  65. }
  66.  
  67. int do_hash(char nome[], char sobrenome[]) {
  68.    
  69.     int i, key=0;
  70.    
  71.     for(i=0; i<strlen(nome); i++) {
  72.         key += (nome[i] - 'a');
  73.     }
  74.    
  75.     for(i=0; i<strlen(sobrenome); i++) {
  76.         key += sobrenome[i] - 'A';
  77.     }
  78.    
  79.     return key%100;
  80. }
  81.  
  82. int consultar(char nome[], char sobrenome[]) {
  83.    
  84.     int iagenda=0;
  85.    
  86.     iagenda = do_hash(nome, sobrenome);
  87.    
  88.     return iagenda;
  89.    
  90. }
  91.  
  92. void imprimir() {
  93.    
  94.     int i, tmp;
  95.    
  96.     for(i=0; i<itab_hash; i++) {
  97.         tmp = tab_hash[i];
  98.         printf("ID: %d - ", tmp);
  99.         printf("Nome: %s", agenda[tmp][0]);
  100.         printf(" %s", agenda[tmp][1]);
  101.         printf("\tTelefone: %s\n", agenda[tmp][2]);
  102.     }
  103.    
  104. }
  105.  
  106. void popular() {
  107.    
  108.     incluir("ricardo", "nunes", "1234-5678");
  109.     incluir("dede", "trapxxx", "1234-5678");
  110.     incluir("artur", "linux", "1234-5678");
  111.     incluir("felipe", "gasp", "1234-5678");
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement