Advertisement
Kimossab

ED - 2014/06/30

Jun 9th, 2015
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.64 KB | None | 0 0
  1. /*
  2.         APENAS TESTEI A PARTE I POR QUESTOES ÓBVIAS...
  3.  
  4.         O RESTO ESTÁ COMO EU FARIA DURANTE O EXAME.
  5.  
  6.         MESMO QUE HAJA ERROS OU ELES IGNORAM OU DESCONTAM APENAS UMAS DECIMAS
  7.         HÁ ERROS QUE SÓ SE DÃO CONTA DURANTE TESTES.
  8.         NAO VOU ESTAR A CRIAR UM FICHEIRO NOVO PARA TESTAR AS FUNÇOES DAS LISTAS
  9.         NEM A CRIAR UMA ARVORE PARA TESTAR ISTO...
  10.  
  11.         TODAS AS SOLUÇÕES NAO SAO UNICAS. HÁ VARIAS FORMAS DE FAZER A MESMA TRETA
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <malloc.h>
  17. #include <string.h>
  18.  
  19. //PARTE I
  20. //1
  21. void escreveVectorInt(int *vector, int n)
  22. {
  23.     if(vector == NULL)
  24.         return;
  25.  
  26.     for(int i=0; i<n; i++)
  27.         printf("Indice %d = %d\n", i, vector[i]);
  28. }
  29.  
  30. //2 - Recursivo
  31. float SumRec(int n, int x, int i=1, int exp=1)
  32. {
  33.     exp *= x;
  34.     if(i == n)
  35.         return exp/i;
  36.     return exp/i + SumRec(n,x,i+1,exp);
  37. }
  38.  
  39. //2 - Iterativo
  40. float SumIt(int n, int x)
  41. {
  42.     int exp=1;
  43.     float sum = 0;
  44.  
  45.     for(int i=1; i<=n; i++)
  46.     {
  47.         exp *= x;
  48.         sum+=exp/i;
  49.     }
  50.  
  51.     return sum;
  52. }
  53.  
  54. //3
  55. char *substring(char *src, int position, int length)
  56. {
  57.     char *t = (char*)malloc(sizeof(char) * (length+1));
  58.     for(int i=0; i<length; i++)
  59.         t[i] = src[i+position];
  60.     t[length] = 0;
  61.     return t;
  62. }
  63.  
  64. //PARTE II - NAO TESTADA (Basicamente era isto que faria no teste...)
  65. //a
  66. typedef struct
  67. {
  68.     char Nome[100];
  69.     char cod[5];
  70.     char especialidade[150];
  71.     char morada[200];
  72.     int idade;
  73. }Funcionario;
  74. typedef struct nozito
  75. {
  76.     Funcionario *Info;
  77.     struct nozito *Prox;
  78. }NO;
  79. typedef struct lista
  80. {
  81.     char Nome[100];
  82.     NO *Inicio;
  83. }Lista;
  84.  
  85. Funcionario *CriarFunc(char *nome, char *codint, char *espec, char *mor, int idade)
  86. {
  87.     Funcionario *aux = (Funcionario *)malloc(sizeof(Funcionario));
  88.     strcpy(aux->Nome, nome);
  89.     strcpy(aux->cod, codint);
  90.     strcpy(aux->especialidade, espec);
  91.     strcpy(aux->morada, mor);
  92.     aux->idade = idade;
  93.     return aux;
  94. }
  95.  
  96. //b
  97. void AddFunc(Lista *L, Funcionario *F)
  98. {
  99.     if(!F)
  100.         return;
  101.     if(!L)
  102.     {
  103.         L = (Lista *)malloc(sizeof(Lista));
  104.         L->Inicio = NULL;
  105.         strcpy(L->Nome,"Funcionarios");
  106.     }
  107.  
  108.     NO *n = (NO *)malloc(sizeof(NO));
  109.     n->Info = F;
  110.  
  111.     if(!L->Inicio)
  112.     {
  113.         n->Prox = NULL;
  114.         L->Inicio = n;
  115.         return;
  116.     }
  117.     n->Prox = L->Inicio;
  118.     L->Inicio = n;
  119. }
  120.  
  121. //c
  122. int Gravar(Lista *L, char *nf)
  123. {
  124.     if(!L)
  125.         return;
  126.  
  127.     FILE *f = fopen(nf, "w");
  128.     NO *aux = L->Inicio;
  129.     bool prim = true;
  130.     while(aux)
  131.     {
  132.         if(!prim)
  133.             fprintf(f,"\n");
  134.         else prim = false;
  135.         fprintf(f,"%s\t%s\t%s\t%s\t%d", aux->Info->Nome, aux->Info->cod, aux->Info->especialidade, aux->Info->morada, aux->Info->idade);
  136.         aux = aux->Prox;
  137.     }
  138.     fclose(f);
  139. }
  140.  
  141. //d
  142. void Inverter(Lista *L)
  143. {
  144.     NO *aux = L->Inicio;
  145.     NO *aux2 = NULL;
  146.     NO *aux3;
  147.  
  148.     while(aux)
  149.     {
  150.         aux3 = aux->Prox;
  151.         aux->Prox = aux2;
  152.         aux2 = aux;
  153.         aux = aux3;
  154.     }
  155. }
  156.  
  157. //PARTE III
  158. //a acho que e isto -.-
  159. int FHashing(Funcionario *F)
  160. {
  161.     return F->idade % 10;
  162. }
  163.  
  164. typedef struct
  165. {
  166.     NO *hash[10];
  167. }Hashing;
  168.  
  169. //b - Nao estou confiante a cerca disto... é capaz de nao dar assim, mas a logica é a mesma, basta nao usarem estafunçao e porem na de baixo que quase de certeza que é assim
  170. bool EliminaFuncHash(NO *F, char *COD)
  171. {
  172.     NO *ant = NULL;
  173.     while(F)
  174.     {
  175.         if(stricmp(F->Info->cod,COD)) //se nao forem iguais
  176.         {
  177.             ant = F;
  178.             F=F->Prox;
  179.             continue;
  180.         }
  181.  
  182.         if(!ant)
  183.         {
  184.             ant = F;
  185.             F = F->Prox;
  186.             free(ant);
  187.         }
  188.  
  189.         ant->Prox = F->Prox;
  190.         free(F);
  191.         return true;
  192.     }
  193.     return false;
  194. }
  195. void EliminaFuncionarios(Hashing *H, char *COD)
  196. {
  197.     for(int i=0; i<10; i++)
  198.         if(EliminaFuncHash(H->hash[i],COD))
  199.             return;
  200. }
  201.  
  202. //c
  203. struct aux_esp
  204. {
  205.     char esp[150];
  206.     int quant;
  207. };
  208.  
  209. int VerificarVectorEspec(aux_esp *e, int parc, char *esp)
  210. {
  211.     for(int i=0; i<parc; i++)
  212.         if(!stricmp(e->esp,esp))
  213.             return i;
  214.     return -1;
  215. }
  216.  
  217. void EspecialidadeMaisComum(Hashing *H)
  218. {
  219.     aux_esp *esp = NULL;
  220.     int qesp=0, a, maior;
  221.     NO* aux;
  222.     for(int i=0; i<10; i++)
  223.     {
  224.         aux = H->hash[i];
  225.         while(aux)
  226.         {
  227.             a=VerificarVectorEspec(esp,qesp,aux->Info->especialidade);
  228.             if(a == -1)
  229.             {
  230.                 esp = (aux_esp *)realloc(esp,sizeof(aux_esp)*(qesp+1));
  231.                 strcpy(esp[qesp].esp,aux->Info->especialidade);
  232.                 esp[qesp].quant++;
  233.                 qesp++;
  234.             }
  235.             else
  236.                 esp[a].quant++;
  237.             aux = aux->Prox;
  238.         }
  239.     }
  240.  
  241.     if(!esp)
  242.         return;
  243.     a=0;
  244.     maior = esp[0].quant;
  245.     for(int i=1; i<qesp; i++)
  246.         if(esp[i].quant > maior)
  247.         {
  248.             a=i;
  249.             maior = esp[i].quant;
  250.         }
  251.  
  252.     printf("\nA especialidade mais comum é %s com %d funcionários\n", esp[a].esp,maior);
  253. }
  254.  
  255. void main()
  256. {
  257.     int vector[] = {1,2,4,8,16};
  258.     escreveVectorInt(vector,5);
  259.     printf("\n\nSumatorio:\nRecursivo: %f\nIterativo: %f",SumRec(5,10),SumIt(5,10));
  260.     printf("\nNova frase: %s\n", substring("ola PESSOAL",4,6));
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement