Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <windows.h>
- #include <malloc.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h> //para usar caracteres acentuados
- #define MAX_FAIXAS_ETARIAS 10
- #define DEBUG 1
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 1
- typedef struct Pessoa
- {
- char nome[21]; // a
- int idade; // b
- float peso; // c
- float altura; // d
- } *PESSOA;
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 2
- typedef struct Elemento
- {
- PESSOA inf;
- struct Elemento *seg;
- } *ELEMENTO;
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 3
- typedef struct Lista
- {
- int nelementos; // a
- ELEMENTO inicio; // b
- } *LISTA;
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 4
- typedef struct Grupo
- {
- int faixa_etaria; // a
- LISTA lista; // b
- } GRUPO;
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 5
- typedef struct Hashing
- {
- GRUPO vector[10];
- } *HASHING;
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 6
- LISTA criar_lista()
- {
- LISTA L;
- L = (LISTA)malloc(sizeof(struct Lista));
- L->inicio = NULL;
- L->nelementos = 0;
- return L;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 7
- ELEMENTO criar_elemento()
- {
- ELEMENTO E = (ELEMENTO) malloc(sizeof(struct Elemento));
- E->inf = (PESSOA) malloc(sizeof(struct Pessoa));
- E->seg = NULL;
- return E;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 8
- void ler_elemento (ELEMENTO ele_novo)
- {
- PESSOA pes = (PESSOA) ele_novo->inf;
- fflush(stdin);
- if (!DEBUG)
- {
- printf("Nome: "); gets(pes->nome);
- printf("Idade (anos): "); scanf("%d", &pes->idade);
- printf("Peso (quilos): "); scanf("%f", &pes->peso);
- printf("Altura (metros): "); scanf("%f", &pes->altura);
- }
- else
- {
- printf("Idade (anos): "); scanf("%d", &pes->idade);
- char st[20]; strcpy(pes->nome,"A_");
- // strcat(pes->nome,itoa(pes->idade,st,10)); // itoa não existe no LINUX
- pes->altura = 1 + pes->idade/100.0;
- pes->peso = 50 + pes->idade/10.0;
- }
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 9
- int comparar_elementos(ELEMENTO A, ELEMENTO B)
- {
- PESSOA PA = (PESSOA) A->inf, PB = (PESSOA) B->inf;
- if (PA->idade < PB->idade)
- return -1;
- else if (PA->idade == PB->idade)
- return 0;
- else
- return 1;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 10
- int elementos_iguais(ELEMENTO A, ELEMENTO B)
- {
- return comparar_elementos(A,B)==0;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 11
- void inserir_elemento_ordenado(LISTA L, ELEMENTO ele_novo)
- {
- if (!L)
- {
- printf("\n\n LISTA NAO EXISTENTE\n\n");
- return;
- }
- int av=1;
- ELEMENTO ant,act;
- if (L->inicio==NULL)
- {
- ele_novo->seg=NULL;
- L->inicio=ele_novo;
- }
- else
- {
- ant=act=L->inicio;
- while(av)
- if(act==NULL)//atingido o fim da lista
- av=0;
- else if(comparar_elementos(act, ele_novo)>0)
- av=0;// inserir antes de act
- else//avança para o elemento seguinte
- {
- ant=act;
- act=act->seg;
- }
- if(act==L->inicio) //inserir no início
- {
- ele_novo->seg=L->inicio;
- L->inicio=ele_novo;
- }
- else
- {//inserir entre ant e act
- ant->seg=ele_novo;
- ele_novo->seg=act;
- }
- }
- L->nelementos++;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 12
- ELEMENTO pesquisar_elemento(LISTA L, ELEMENTO ele_pesquisa) {
- if (!ele_pesquisa || !L)
- return NULL;
- ELEMENTO p = L->inicio;
- while (p) {
- if (elementos_iguais(p, ele_pesquisa))
- return p;
- else
- p = p->seg;
- }
- return NULL;
- }
- /* 13. libertar o espaço alocado para um dado elemento e respectivas informações associadas. */
- void libertar_elemento(ELEMENTO ele_libertar) {
- free(ele_libertar->inf); // Destroi informacao
- free(ele_libertar); // Destroi o elemento
- }
- /* 14. remover um determinado elemento de uma lista */
- ELEMENTO remover_elemento(LISTA L, ELEMENTO ele_remover)
- {
- if (!L)
- return NULL;
- int av=1;
- ELEMENTO ret,ant,act;
- if (L->inicio==NULL) //lista vazia
- return NULL;
- ant=act=L->inicio;
- while(av)
- if(act==NULL) //chegou ao fim, sem encontrar
- av=0;
- else if(elementos_iguais(act, ele_remover))
- av=0; //encontrou elemento a remover
- else
- {
- ant=act;
- act=act->seg;
- }
- if(act==NULL)
- return NULL;
- ret=act;
- if(L->inicio==act)
- L->inicio=act->seg;
- else
- ant->seg=act->seg;
- L->nelementos--;
- return ret;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 15
- void mostrar_elemento (ELEMENTO ele_mostrar)
- {
- PESSOA pes = (PESSOA) ele_mostrar->inf;
- if (!DEBUG)
- {
- printf("\nNome: %s", pes->nome);
- printf("\nIdade (anos): %d", pes->idade);
- printf("\nPeso (quilos): %.2f", pes->peso);
- printf("\nAltura (metros): %.2f", pes->altura);
- }
- else
- {
- printf("\n ( %d )", pes->idade);
- printf(" nome:%s", pes->nome);
- printf(" %d anos", pes->idade);
- printf(" %.2f Kg ", pes->peso);
- printf(" %.2f metros", pes->altura);
- }
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 16
- void mostrar_ordenado(LISTA L)
- {
- if (!L)
- return;
- ELEMENTO p = L->inicio;
- while (p)
- {
- mostrar_elemento(p);
- printf(" ");
- p = p->seg;
- }
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 17
- HASHING criar_vector_hashing()
- {
- HASHING H = (HASHING) malloc(sizeof(struct Hashing));
- return H;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 18
- void inicializar_vector_hashing(HASHING H)
- {
- for(int i=0; i<=10; i++)
- {
- H->vector[i].faixa_etaria=i;
- H->vector[i].lista=criar_lista();
- }
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 19
- int posicao_hashing_elemento(ELEMENTO E)
- {
- return (int)E->inf->idade/10;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 20
- int validar_posicao_hashing (int pos)
- {
- return (pos >= 0) && (pos < MAX_FAIXAS_ETARIAS);
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 21
- void inserir_elemento_hashing(HASHING H, ELEMENTO E)
- {
- int n;
- n=posicao_hashing_elemento(E);
- if(validar_posicao_hashing(n) == 0)
- return;
- inserir_elemento_ordenado(H->vector[n].lista, E);
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 22
- ELEMENTO remover_elemento_hashing (HASHING H, ELEMENTO ele_remover)
- {
- ELEMENTO ret = NULL;
- if (H)
- {
- // Determinar a posição onde pode estar o elemento a eliminar!
- int pos = posicao_hashing_elemento(ele_remover);
- if (validar_posicao_hashing(pos)) // Será posição válida!?
- ret = remover_elemento(H->vector[pos].lista, ele_remover);
- else
- printf("\nOcorreu um erro ao calcular a posicao de hashing correcta!");
- }
- return ret;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 23
- ELEMENTO pesquisar_elemento_hashing(HASHING H, ELEMENTO ele_pesquisa)
- {
- int n=posicao_hashing_elemento(ele_pesquisa);
- if(validar_posicao_hashing(n) == 0)
- return NULL;
- return pesquisar_elemento(H->vector[n].lista, ele_pesquisa);
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 24
- void mostrar_elementos_hashing (HASHING H)
- {
- if (!H)
- return;
- for (int i = 0; i < MAX_FAIXAS_ETARIAS; i++)
- {
- int faixa_etaria = H->vector[i].faixa_etaria;
- int min_faixa_etaria = faixa_etaria * MAX_FAIXAS_ETARIAS;
- int max_faixa_etaria = min_faixa_etaria + MAX_FAIXAS_ETARIAS-1;
- if (H->vector[i].lista->nelementos>0)
- {
- printf("\nExistem %d pessoas da faixa etaria %d (%d a %d anos):",H->vector[i].lista->nelementos,faixa_etaria, min_faixa_etaria, max_faixa_etaria);
- mostrar_ordenado(H->vector[i].lista);
- }
- else
- printf("\nNao existem pessoas na faixa etaria %d (%d a %d)!",faixa_etaria, min_faixa_etaria, max_faixa_etaria);
- }
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // AUXILIAR
- ELEMENTO criar_preencher_elemento(char *nm, int ida, float pes, float alt)
- {
- ELEMENTO E = criar_elemento();
- strcpy(E->inf->nome,nm);
- E->inf->idade = ida ;
- E->inf->peso = pes ;
- E->inf->altura = alt ;
- return E;
- }
- /* 25. menu */
- int menu() {
- int x;
- // system("cls");
- printf("\n# MENU PRINCIPAL --------------------------------------------#");
- printf("\n| (1) Inserir um novo elemento no vector de hashing |");
- printf("\n| (2) Retirar um elemento do vector de hashing |");
- printf("\n| (3) Mostrar os elementos do vector de hashing |");
- printf("\n| (4) Pesquisar um elemento no vector de hashing |");
- printf("\n| ----------------------------------------------------------|");
- printf("\n| (0) SAIR |");
- printf("\n#------------------------------------------------------------#\n");
- do {
- printf("\n Qual a sua opcao ? ");
- fflush(stdin);
- scanf("%d",&x);
- } while ( x < 0 || x > 4 );
- return x;
- }
- //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
- // 26
- void main()
- {
- //variaveis
- int op;
- setlocale(LC_ALL,""); /* para usar caracteres acentuados */
- HASHING pessoas;
- ELEMENTO ele_criar, ele_remover, ele_pesquisar;
- pessoas = criar_vector_hashing();
- inicializar_vector_hashing(pessoas);
- if (DEBUG)
- {
- ele_criar = criar_preencher_elemento("A_30",30,30.5,1.30); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_20",20,20.5,1.20); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_50",50,50.5,1.50); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_60",60,60.5,1.60); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_55",55,55.5,1.55); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_40",40,40.5,1.40); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_26",26,26.5,1.26); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_10",10,10.5,1.10); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_29",29,29.5,1.29); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_27",27,27.5,1.27); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_28",28,28.5,1.28); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_22",22,22.5,1.22); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_25",25,25.5,1.25); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_21",21,21.5,1.21); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_23",23,23.5,1.23); inserir_elemento_hashing(pessoas,ele_criar);
- ele_criar = criar_preencher_elemento("A_24",24,24.5,1.24); inserir_elemento_hashing(pessoas,ele_criar);
- }
- do {
- op = menu();
- switch(op) {
- case 1: // (1) Inserir um novo elemento no vector de hashing
- ele_criar = criar_elemento();
- ler_elemento(ele_criar);
- inserir_elemento_hashing(pessoas, ele_criar);
- break;
- case 2: // (2) Retirar um elemento do vector de hashing
- ele_remover = criar_elemento();
- printf("Idade: ");
- scanf("%d", &((PESSOA)ele_remover->inf)->idade);
- ele_remover = remover_elemento_hashing(pessoas, ele_remover);
- if (ele_remover) {
- printf("O elemento foi removido!\n");
- mostrar_elemento(ele_remover);
- libertar_elemento(ele_remover);
- }
- else
- printf("Não foi encontrado nenhum elemento!\n");
- break;
- case 3: // (3) Mostrar os elementos do vector de hashing
- mostrar_elementos_hashing(pessoas);
- break;
- case 4: // (4) Pesquisar um elemento no vector de hashing
- ele_pesquisar = criar_elemento();
- printf("Idade: ");
- scanf("%d", &((PESSOA)ele_pesquisar->inf)->idade);
- ele_pesquisar = pesquisar_elemento_hashing(pessoas, ele_pesquisar);
- if (ele_pesquisar) {
- printf("Foi encontrado um elemento: ");
- mostrar_elemento(ele_pesquisar);
- printf("\n");
- }
- else
- printf("Não foi encontrado nenhum elemento!\n");
- break;
- case 0 : printf("\n\n FIM \n\n\n");
- }
- fflush(stdin);
- } while( op != 0 );
- //system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment