Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 100
- int consultar(char nome[], char sobrenome[]);
- void incluir(char first_name[], char last_name[], char tel[]);
- int do_hash(char nome[], char sobrenome[]);
- void popular();
- void imprimir();
- char agenda[MAX][2][20];
- int tab_hash[MAX];
- int itab_hash = 0;
- int main() {
- int tmp, op;
- char nome[20], sobrenome[20];
- popular();
- do{
- printf("1-Consultar por nome e sobrenome\n2-Imprimir Lista\n");
- scanf("%d", &op);
- switch(op) {
- case 1:
- system("clear");
- printf("Digite o nome: ");
- scanf("%s", nome);
- printf("Digite o sobrenome: ");
- scanf("%s", sobrenome);
- tmp = consultar(nome, sobrenome);
- printf("ID: %d - ", tmp);
- printf("Nome: %s", agenda[tmp][0]);
- printf(" %s", agenda[tmp][1]);
- printf("\tTelefone: %s\n", agenda[tmp][2]);
- break;
- case 2:
- system("clear");
- imprimir();
- break;
- default:
- break;
- }
- }while(op != 3);
- return 0;
- }
- void incluir(char nome[], char sobrenome[], char tel[]) {
- int iagenda=0;
- iagenda = do_hash(nome, sobrenome);
- strcpy(agenda[iagenda][0], nome);
- strcpy(agenda[iagenda][1], sobrenome);
- strcpy(agenda[iagenda][2], tel);
- tab_hash[itab_hash++] = iagenda;
- }
- int do_hash(char nome[], char sobrenome[]) {
- int i, key=0;
- for(i=0; i<strlen(nome); i++) {
- key += (nome[i] - 'a');
- }
- for(i=0; i<strlen(sobrenome); i++) {
- key += sobrenome[i] - 'A';
- }
- return key%100;
- }
- int consultar(char nome[], char sobrenome[]) {
- int iagenda=0;
- iagenda = do_hash(nome, sobrenome);
- return iagenda;
- }
- void imprimir() {
- int i, tmp;
- for(i=0; i<itab_hash; i++) {
- tmp = tab_hash[i];
- printf("ID: %d - ", tmp);
- printf("Nome: %s", agenda[tmp][0]);
- printf(" %s", agenda[tmp][1]);
- printf("\tTelefone: %s\n", agenda[tmp][2]);
- }
- }
- void popular() {
- incluir("ricardo", "nunes", "1234-5678");
- incluir("dede", "trapxxx", "1234-5678");
- incluir("artur", "linux", "1234-5678");
- incluir("felipe", "gasp", "1234-5678");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement