Kimossab

Ficha 8 - ED

May 8th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <malloc.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <locale.h>   //para usar caracteres acentuados  
  8.  
  9. #define MAX_FAIXAS_ETARIAS 10
  10. #define DEBUG 1
  11.  
  12. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  13. // 1
  14. typedef struct Pessoa
  15. {
  16.     char nome[21]; // a
  17.     int idade;     // b
  18.     float peso;    // c
  19.     float altura;  // d
  20. } *PESSOA;
  21.  
  22. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  23. // 2
  24. typedef struct Elemento
  25. {
  26.     PESSOA inf;
  27.     struct Elemento *seg;
  28. } *ELEMENTO;
  29.  
  30. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  31. // 3
  32. typedef struct Lista
  33. {
  34.     int nelementos;    // a
  35.     ELEMENTO inicio;   // b
  36. } *LISTA;
  37.  
  38. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  39. // 4
  40. typedef struct Grupo
  41. {
  42.     int faixa_etaria;   // a
  43.     LISTA lista;        // b
  44. } GRUPO;
  45.  
  46. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  47. // 5
  48. typedef struct Hashing
  49. {
  50.     GRUPO vector[10];
  51. } *HASHING;
  52.  
  53. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  54. // 6
  55. LISTA criar_lista()
  56. {
  57.     LISTA L;
  58.     L = (LISTA)malloc(sizeof(struct Lista));
  59.     L->inicio = NULL;
  60.     L->nelementos = 0;
  61.     return L;
  62. }
  63.  
  64. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  65. // 7
  66. ELEMENTO criar_elemento()
  67. {
  68.     ELEMENTO E = (ELEMENTO) malloc(sizeof(struct Elemento));
  69.     E->inf = (PESSOA) malloc(sizeof(struct Pessoa));
  70.     E->seg = NULL;
  71.     return E;
  72. }
  73.  
  74. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  75. // 8
  76. void ler_elemento (ELEMENTO ele_novo)
  77. {
  78.     PESSOA pes = (PESSOA) ele_novo->inf;
  79.     fflush(stdin);
  80.     if (!DEBUG)
  81.     {
  82.         printf("Nome: ");             gets(pes->nome);
  83.         printf("Idade (anos): ");     scanf("%d", &pes->idade);
  84.         printf("Peso (quilos): ");    scanf("%f", &pes->peso);
  85.         printf("Altura (metros): ");  scanf("%f", &pes->altura);
  86.     }
  87.     else
  88.     {
  89.         printf("Idade (anos): ");  scanf("%d", &pes->idade);
  90.         char st[20];  strcpy(pes->nome,"A_");
  91.         // strcat(pes->nome,itoa(pes->idade,st,10));    // itoa não existe no LINUX
  92.         pes->altura = 1 + pes->idade/100.0;
  93.         pes->peso = 50 + pes->idade/10.0;
  94.     }
  95. }
  96.  
  97. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  98. // 9
  99. int comparar_elementos(ELEMENTO A, ELEMENTO B)
  100. {
  101.     PESSOA PA = (PESSOA) A->inf, PB = (PESSOA) B->inf;
  102.     if (PA->idade < PB->idade)
  103.         return -1;
  104.     else if (PA->idade == PB->idade)
  105.         return 0;
  106.     else
  107.         return 1;
  108. }
  109.  
  110. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  111. // 10
  112. int elementos_iguais(ELEMENTO A, ELEMENTO B)
  113. {
  114.     return comparar_elementos(A,B)==0;
  115. }
  116.  
  117. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  118. // 11
  119. void inserir_elemento_ordenado(LISTA L, ELEMENTO ele_novo)
  120. {
  121.     if (!L)
  122.     {
  123.         printf("\n\n LISTA NAO EXISTENTE\n\n");
  124.         return;
  125.     }
  126.  
  127.     int av=1;
  128.     ELEMENTO ant,act;
  129.  
  130.     if (L->inicio==NULL)
  131.     {
  132.         ele_novo->seg=NULL;
  133.         L->inicio=ele_novo;
  134.     }
  135.     else
  136.     {
  137.         ant=act=L->inicio;
  138.         while(av)
  139.             if(act==NULL)//atingido o fim da lista
  140.                 av=0;
  141.             else if(comparar_elementos(act, ele_novo)>0)
  142.                 av=0;// inserir antes de act
  143.             else//avança para o elemento seguinte
  144.             {
  145.                 ant=act;
  146.                 act=act->seg;
  147.             }
  148.             if(act==L->inicio) //inserir no início
  149.             {
  150.                 ele_novo->seg=L->inicio;
  151.                 L->inicio=ele_novo;
  152.             }
  153.             else
  154.             {//inserir entre ant e act
  155.                 ant->seg=ele_novo;
  156.                 ele_novo->seg=act;
  157.             }
  158.     }
  159.     L->nelementos++;
  160. }
  161.  
  162. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  163. // 12
  164. ELEMENTO pesquisar_elemento(LISTA L, ELEMENTO ele_pesquisa) {
  165.   if (!ele_pesquisa || !L)
  166.     return NULL;
  167.  
  168.   ELEMENTO p = L->inicio;
  169.   while (p) {
  170.     if (elementos_iguais(p, ele_pesquisa))
  171.         return p;
  172.     else
  173.         p = p->seg;
  174.   }
  175.   return NULL;
  176. }
  177. /* 13. libertar o espaço alocado para um dado elemento e respectivas informações associadas. */
  178. void libertar_elemento(ELEMENTO ele_libertar) {
  179.     free(ele_libertar->inf); // Destroi informacao
  180.     free(ele_libertar); // Destroi o elemento
  181. }
  182. /* 14. remover um determinado elemento de uma lista */
  183. ELEMENTO remover_elemento(LISTA L, ELEMENTO ele_remover)
  184. {
  185.   if (!L)
  186.       return NULL;
  187.  
  188.   int av=1;
  189.   ELEMENTO ret,ant,act;
  190.  
  191.   if (L->inicio==NULL)   //lista vazia
  192.     return NULL;
  193.  
  194.   ant=act=L->inicio;
  195.   while(av)
  196.     if(act==NULL)  //chegou ao fim, sem encontrar
  197.         av=0;
  198.     else if(elementos_iguais(act, ele_remover))
  199.         av=0;      //encontrou elemento a remover
  200.     else
  201.     {
  202.         ant=act;
  203.         act=act->seg;
  204.     }
  205.   if(act==NULL)
  206.       return NULL;
  207.   ret=act;
  208.   if(L->inicio==act)
  209.       L->inicio=act->seg;
  210.   else
  211.       ant->seg=act->seg;
  212.   L->nelementos--;
  213.   return ret;
  214. }
  215.  
  216. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  217. // 15
  218. void mostrar_elemento (ELEMENTO ele_mostrar)
  219. {
  220.     PESSOA pes = (PESSOA) ele_mostrar->inf;
  221.     if (!DEBUG)
  222.     {
  223.         printf("\nNome: %s", pes->nome);
  224.         printf("\nIdade (anos): %d", pes->idade);
  225.         printf("\nPeso (quilos): %.2f", pes->peso);
  226.         printf("\nAltura (metros): %.2f", pes->altura);
  227.     }
  228.     else
  229.     {
  230.         printf("\n ( %d )", pes->idade);
  231.         printf("     nome:%s", pes->nome);
  232.         printf("   %d anos", pes->idade);
  233.         printf("   %.2f Kg ", pes->peso);
  234.         printf("   %.2f metros", pes->altura);
  235.     }
  236. }
  237.  
  238. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  239. // 16
  240. void mostrar_ordenado(LISTA L)
  241. {
  242.     if (!L)
  243.         return;
  244.  
  245.     ELEMENTO p = L->inicio;
  246.     while (p)
  247.     {
  248.         mostrar_elemento(p);
  249.         printf(" ");
  250.         p = p->seg;
  251.     }
  252. }
  253.  
  254. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  255. // 17
  256. HASHING criar_vector_hashing()
  257. {
  258.     HASHING H = (HASHING) malloc(sizeof(struct Hashing));
  259.     return H;
  260. }
  261.  
  262. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  263. // 18
  264. void inicializar_vector_hashing(HASHING H)
  265. {
  266.     for(int i=0; i<=10; i++)
  267.     {
  268.         H->vector[i].faixa_etaria=i;
  269.         H->vector[i].lista=criar_lista();
  270.     }
  271. }
  272.  
  273. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  274. // 19
  275. int posicao_hashing_elemento(ELEMENTO E)
  276. {
  277.     return (int)E->inf->idade/10;
  278. }
  279.  
  280. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  281. // 20
  282. int validar_posicao_hashing (int pos)
  283. {
  284.     return (pos >= 0) && (pos < MAX_FAIXAS_ETARIAS);
  285. }
  286.  
  287. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  288. // 21
  289. void inserir_elemento_hashing(HASHING H, ELEMENTO E)
  290. {
  291.     int n;
  292.     n=posicao_hashing_elemento(E);
  293.     if(validar_posicao_hashing(n) == 0)
  294.         return;
  295.  
  296.     inserir_elemento_ordenado(H->vector[n].lista, E);
  297. }
  298.  
  299. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  300. // 22
  301. ELEMENTO remover_elemento_hashing (HASHING H, ELEMENTO ele_remover)
  302. {
  303.     ELEMENTO ret = NULL;
  304.     if (H)
  305.     {
  306.       // Determinar a posição onde pode estar o elemento a eliminar!
  307.   int pos = posicao_hashing_elemento(ele_remover);
  308.   if (validar_posicao_hashing(pos)) // Será posição válida!?
  309.      ret = remover_elemento(H->vector[pos].lista, ele_remover);
  310.   else
  311.      printf("\nOcorreu um erro ao calcular a posicao de hashing correcta!");
  312.   }
  313.   return ret;
  314. }
  315.  
  316. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  317. // 23
  318. ELEMENTO pesquisar_elemento_hashing(HASHING H, ELEMENTO ele_pesquisa)
  319. {
  320.     int n=posicao_hashing_elemento(ele_pesquisa);
  321.     if(validar_posicao_hashing(n) == 0)
  322.         return NULL;
  323.  
  324.     return pesquisar_elemento(H->vector[n].lista, ele_pesquisa);
  325. }
  326.  
  327. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  328. // 24
  329. void mostrar_elementos_hashing (HASHING H)
  330. {
  331.     if (!H)
  332.         return;
  333.  
  334.     for (int i = 0; i < MAX_FAIXAS_ETARIAS; i++)
  335.     {
  336.         int faixa_etaria = H->vector[i].faixa_etaria;
  337.         int min_faixa_etaria = faixa_etaria * MAX_FAIXAS_ETARIAS;
  338.         int max_faixa_etaria = min_faixa_etaria + MAX_FAIXAS_ETARIAS-1;
  339.         if (H->vector[i].lista->nelementos>0)
  340.         {
  341.             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);
  342.             mostrar_ordenado(H->vector[i].lista);
  343.         }
  344.         else
  345.             printf("\nNao existem pessoas na faixa etaria %d (%d a %d)!",faixa_etaria, min_faixa_etaria, max_faixa_etaria);
  346.     }
  347. }
  348. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  349. // AUXILIAR
  350. ELEMENTO criar_preencher_elemento(char *nm, int ida, float pes, float alt)
  351. {
  352.     ELEMENTO E = criar_elemento();
  353.     strcpy(E->inf->nome,nm);
  354.     E->inf->idade = ida ;
  355.     E->inf->peso =  pes ;
  356.     E->inf->altura = alt ;
  357.     return E;
  358. }
  359. /* 25. menu  */
  360. int menu() {
  361.    int x;
  362.  
  363.    // system("cls");
  364.    printf("\n# MENU PRINCIPAL --------------------------------------------#");
  365.    printf("\n|  (1) Inserir um novo elemento no vector de hashing         |");
  366.    printf("\n|  (2) Retirar um elemento do vector de hashing              |");
  367.    printf("\n|  (3) Mostrar os elementos do vector de hashing             |");
  368.    printf("\n|  (4) Pesquisar um elemento no vector de hashing            |");
  369.    printf("\n|  ----------------------------------------------------------|");
  370.    printf("\n|  (0) SAIR                                                  |");
  371.    printf("\n#------------------------------------------------------------#\n");
  372.   do {
  373.     printf("\n  Qual a sua opcao ? ");
  374.     fflush(stdin);
  375.     scanf("%d",&x);
  376.   } while ( x < 0 || x > 4 );
  377.   return x;
  378. }
  379.  
  380. //-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
  381. // 26
  382. void main()
  383. {
  384.     //variaveis
  385.     int op;
  386.     setlocale(LC_ALL,"");   /* para usar caracteres acentuados   */
  387.  
  388.     HASHING pessoas;
  389.     ELEMENTO ele_criar, ele_remover, ele_pesquisar;
  390.  
  391.     pessoas = criar_vector_hashing();
  392.     inicializar_vector_hashing(pessoas);
  393.  
  394.     if (DEBUG)
  395.     {
  396.         ele_criar = criar_preencher_elemento("A_30",30,30.5,1.30); inserir_elemento_hashing(pessoas,ele_criar);
  397.         ele_criar = criar_preencher_elemento("A_20",20,20.5,1.20); inserir_elemento_hashing(pessoas,ele_criar);
  398.         ele_criar = criar_preencher_elemento("A_50",50,50.5,1.50); inserir_elemento_hashing(pessoas,ele_criar);
  399.         ele_criar = criar_preencher_elemento("A_60",60,60.5,1.60); inserir_elemento_hashing(pessoas,ele_criar);
  400.         ele_criar = criar_preencher_elemento("A_55",55,55.5,1.55); inserir_elemento_hashing(pessoas,ele_criar);
  401.         ele_criar = criar_preencher_elemento("A_40",40,40.5,1.40); inserir_elemento_hashing(pessoas,ele_criar);
  402.         ele_criar = criar_preencher_elemento("A_26",26,26.5,1.26); inserir_elemento_hashing(pessoas,ele_criar);
  403.         ele_criar = criar_preencher_elemento("A_10",10,10.5,1.10); inserir_elemento_hashing(pessoas,ele_criar);
  404.         ele_criar = criar_preencher_elemento("A_29",29,29.5,1.29); inserir_elemento_hashing(pessoas,ele_criar);
  405.         ele_criar = criar_preencher_elemento("A_27",27,27.5,1.27); inserir_elemento_hashing(pessoas,ele_criar);
  406.         ele_criar = criar_preencher_elemento("A_28",28,28.5,1.28); inserir_elemento_hashing(pessoas,ele_criar);
  407.         ele_criar = criar_preencher_elemento("A_22",22,22.5,1.22); inserir_elemento_hashing(pessoas,ele_criar);
  408.         ele_criar = criar_preencher_elemento("A_25",25,25.5,1.25); inserir_elemento_hashing(pessoas,ele_criar);
  409.         ele_criar = criar_preencher_elemento("A_21",21,21.5,1.21); inserir_elemento_hashing(pessoas,ele_criar);
  410.         ele_criar = criar_preencher_elemento("A_23",23,23.5,1.23); inserir_elemento_hashing(pessoas,ele_criar);
  411.         ele_criar = criar_preencher_elemento("A_24",24,24.5,1.24); inserir_elemento_hashing(pessoas,ele_criar);
  412.     }  
  413.  
  414.   do {
  415.     op = menu();
  416.     switch(op) {
  417.       case 1:  //  (1) Inserir um novo elemento no vector de hashing
  418.         ele_criar = criar_elemento();
  419.         ler_elemento(ele_criar);
  420.         inserir_elemento_hashing(pessoas, ele_criar);
  421.         break;
  422.       case 2:  //  (2) Retirar um elemento do vector de hashing
  423.         ele_remover = criar_elemento();
  424.         printf("Idade: ");
  425.         scanf("%d", &((PESSOA)ele_remover->inf)->idade);
  426.         ele_remover = remover_elemento_hashing(pessoas, ele_remover);
  427.         if (ele_remover) {
  428.           printf("O elemento foi removido!\n");
  429.           mostrar_elemento(ele_remover);
  430.           libertar_elemento(ele_remover);
  431.         }
  432.         else
  433.           printf("Não foi encontrado nenhum elemento!\n");
  434.         break;
  435.       case 3:  //  (3) Mostrar os elementos do vector de hashing
  436.         mostrar_elementos_hashing(pessoas);
  437.         break;
  438.       case 4:  //  (4) Pesquisar um elemento no vector de hashing
  439.         ele_pesquisar = criar_elemento();
  440.         printf("Idade: ");
  441.         scanf("%d", &((PESSOA)ele_pesquisar->inf)->idade);
  442.         ele_pesquisar = pesquisar_elemento_hashing(pessoas, ele_pesquisar);
  443.         if (ele_pesquisar) {
  444.           printf("Foi encontrado um elemento: ");
  445.           mostrar_elemento(ele_pesquisar);
  446.           printf("\n");
  447.         }
  448.         else
  449.           printf("Não foi encontrado nenhum elemento!\n");
  450.         break;
  451.       case 0 :  printf("\n\n FIM \n\n\n");
  452.     }
  453.     fflush(stdin);
  454.   } while( op != 0 );
  455.   //system("pause");
  456. }
Advertisement
Add Comment
Please, Sign In to add comment