Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- APENAS TESTEI A PARTE I POR QUESTOES ÓBVIAS...
- O RESTO ESTÁ COMO EU FARIA DURANTE O EXAME.
- MESMO QUE HAJA ERROS OU ELES IGNORAM OU DESCONTAM APENAS UMAS DECIMAS
- HÁ ERROS QUE SÓ SE DÃO CONTA DURANTE TESTES.
- NAO VOU ESTAR A CRIAR UM FICHEIRO NOVO PARA TESTAR AS FUNÇOES DAS LISTAS
- NEM A CRIAR UMA ARVORE PARA TESTAR ISTO...
- TODAS AS SOLUÇÕES NAO SAO UNICAS. HÁ VARIAS FORMAS DE FAZER A MESMA TRETA
- */
- #include <stdio.h>
- #include <ctype.h>
- #include <malloc.h>
- #include <string.h>
- //PARTE I
- //1
- void escreveVectorInt(int *vector, int n)
- {
- if(vector == NULL)
- return;
- for(int i=0; i<n; i++)
- printf("Indice %d = %d\n", i, vector[i]);
- }
- //2 - Recursivo
- float SumRec(int n, int x, int i=1, int exp=1)
- {
- exp *= x;
- if(i == n)
- return exp/i;
- return exp/i + SumRec(n,x,i+1,exp);
- }
- //2 - Iterativo
- float SumIt(int n, int x)
- {
- int exp=1;
- float sum = 0;
- for(int i=1; i<=n; i++)
- {
- exp *= x;
- sum+=exp/i;
- }
- return sum;
- }
- //3
- char *substring(char *src, int position, int length)
- {
- char *t = (char*)malloc(sizeof(char) * (length+1));
- for(int i=0; i<length; i++)
- t[i] = src[i+position];
- t[length] = 0;
- return t;
- }
- //PARTE II - NAO TESTADA (Basicamente era isto que faria no teste...)
- //a
- typedef struct
- {
- char Nome[100];
- char cod[5];
- char especialidade[150];
- char morada[200];
- int idade;
- }Funcionario;
- typedef struct nozito
- {
- Funcionario *Info;
- struct nozito *Prox;
- }NO;
- typedef struct lista
- {
- char Nome[100];
- NO *Inicio;
- }Lista;
- Funcionario *CriarFunc(char *nome, char *codint, char *espec, char *mor, int idade)
- {
- Funcionario *aux = (Funcionario *)malloc(sizeof(Funcionario));
- strcpy(aux->Nome, nome);
- strcpy(aux->cod, codint);
- strcpy(aux->especialidade, espec);
- strcpy(aux->morada, mor);
- aux->idade = idade;
- return aux;
- }
- //b
- void AddFunc(Lista *L, Funcionario *F)
- {
- if(!F)
- return;
- if(!L)
- {
- L = (Lista *)malloc(sizeof(Lista));
- L->Inicio = NULL;
- strcpy(L->Nome,"Funcionarios");
- }
- NO *n = (NO *)malloc(sizeof(NO));
- n->Info = F;
- if(!L->Inicio)
- {
- n->Prox = NULL;
- L->Inicio = n;
- return;
- }
- n->Prox = L->Inicio;
- L->Inicio = n;
- }
- //c
- int Gravar(Lista *L, char *nf)
- {
- if(!L)
- return;
- FILE *f = fopen(nf, "w");
- NO *aux = L->Inicio;
- bool prim = true;
- while(aux)
- {
- if(!prim)
- fprintf(f,"\n");
- else prim = false;
- 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);
- aux = aux->Prox;
- }
- fclose(f);
- }
- //d
- void Inverter(Lista *L)
- {
- NO *aux = L->Inicio;
- NO *aux2 = NULL;
- NO *aux3;
- while(aux)
- {
- aux3 = aux->Prox;
- aux->Prox = aux2;
- aux2 = aux;
- aux = aux3;
- }
- }
- //PARTE III
- //a acho que e isto -.-
- int FHashing(Funcionario *F)
- {
- return F->idade % 10;
- }
- typedef struct
- {
- NO *hash[10];
- }Hashing;
- //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
- bool EliminaFuncHash(NO *F, char *COD)
- {
- NO *ant = NULL;
- while(F)
- {
- if(stricmp(F->Info->cod,COD)) //se nao forem iguais
- {
- ant = F;
- F=F->Prox;
- continue;
- }
- if(!ant)
- {
- ant = F;
- F = F->Prox;
- free(ant);
- }
- ant->Prox = F->Prox;
- free(F);
- return true;
- }
- return false;
- }
- void EliminaFuncionarios(Hashing *H, char *COD)
- {
- for(int i=0; i<10; i++)
- if(EliminaFuncHash(H->hash[i],COD))
- return;
- }
- //c
- struct aux_esp
- {
- char esp[150];
- int quant;
- };
- int VerificarVectorEspec(aux_esp *e, int parc, char *esp)
- {
- for(int i=0; i<parc; i++)
- if(!stricmp(e->esp,esp))
- return i;
- return -1;
- }
- void EspecialidadeMaisComum(Hashing *H)
- {
- aux_esp *esp = NULL;
- int qesp=0, a, maior;
- NO* aux;
- for(int i=0; i<10; i++)
- {
- aux = H->hash[i];
- while(aux)
- {
- a=VerificarVectorEspec(esp,qesp,aux->Info->especialidade);
- if(a == -1)
- {
- esp = (aux_esp *)realloc(esp,sizeof(aux_esp)*(qesp+1));
- strcpy(esp[qesp].esp,aux->Info->especialidade);
- esp[qesp].quant++;
- qesp++;
- }
- else
- esp[a].quant++;
- aux = aux->Prox;
- }
- }
- if(!esp)
- return;
- a=0;
- maior = esp[0].quant;
- for(int i=1; i<qesp; i++)
- if(esp[i].quant > maior)
- {
- a=i;
- maior = esp[i].quant;
- }
- printf("\nA especialidade mais comum é %s com %d funcionários\n", esp[a].esp,maior);
- }
- void main()
- {
- int vector[] = {1,2,4,8,16};
- escreveVectorInt(vector,5);
- printf("\n\nSumatorio:\nRecursivo: %f\nIterativo: %f",SumRec(5,10),SumIt(5,10));
- printf("\nNova frase: %s\n", substring("ola PESSOAL",4,6));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement