Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <malloc.h>
- #include <string.h>
- #include <time.h>
- //PARTE I
- //a)
- //nao faço ideia como os stores queriam que uma função retorne 2 numeros... há 3 maneiras: retornar um array de inteiros, uma string ou usar valores por referencia (tipo scanf quando se usa o &i)
- //vou fazer a ultima
- void PrimEUlt(char *s, int *p, int *u)
- {
- *p = -1;
- int i=0;
- while(*s != '\0')
- {
- if(*s != ' ' && *s != '\n' && *s != '\t')
- {
- if(*p == -1)
- *p = i;
- else *u = i;
- }
- i++;
- s++;
- }
- }
- //b)
- //neste é para fazer um programa, mas pode-se usar a função feita em a)
- char *GetSubStr(char *str, int p, int u)
- {
- char res[150];
- for(int i=0; i<=u-p; i++)
- {
- res[i] = str[i+p];
- }
- if(res[u-p] != '\0')
- res[u-p+1] = '\0';
- return res;
- }
- void LerFich()
- {
- FILE *f = fopen("texto.txt","r");
- int p,u;
- char buffer[150];
- while(fgets(buffer, sizeof(buffer),f))
- {
- PrimEUlt(buffer,&p,&u);
- printf("%s\n",GetSubStr(buffer, p, u));
- }
- fclose(f);
- }
- //PARTE II
- //a)
- typedef struct
- {
- char *nome;
- int num_serv;
- int hora;
- }UTENTE;
- typedef struct no
- {
- UTENTE *info;
- no *prox;
- }NO;
- typedef struct
- {
- NO *prim;
- }FILA;
- //b)
- void InsertUt(NO *p, char *nome, int num)
- {
- if(!p) //se p nao existir nada se pode fazer nesta função boa stor, mando-nos ter em atençao todos os erros mas dá-nos um insolucionavel *claps*
- return;
- //Thank you teacher... por nao nos fornecer ajuda acerca da hora *claps*
- time_t timer;
- struct tm *t;
- timer = time(NULL);
- t = localtime(&timer);
- UTENTE *novo = (UTENTE *)malloc(sizeof(UTENTE));
- if(novo == NULL) //erro de alocagem de memoria (memoria insuficiente)
- return;
- NO *aux = p;
- NO *n;
- novo->nome = nome;
- novo->num_serv = num;
- novo->hora = t->tm_hour;
- while(aux->prox)
- aux = aux->prox;
- n = (NO *)malloc(sizeof(NO));
- if(!n)
- return;
- n->prox = NULL;
- n->info = novo;
- n->prox = aux;
- }
- //d)
- int NumUtServ(NO *p, int serv)
- {
- if(!p)
- return -1;
- int num=0;
- NO *aux = p;
- while(aux)
- {
- if(aux->info->num_serv == serv)
- num++;
- aux = aux->prox;
- }
- return num;
- }
- //e)
- void BuscaPrim(NO *p, int serv)
- {
- if(!p)
- return;
- NO *aux = p;
- NO *ant = NULL;
- //Parabens!!! Podes ter um problema insolucionavel aqui!!!
- while(aux)
- {
- if(aux->info->num_serv != serv)
- {
- ant = aux;
- aux = aux->prox;
- continue;
- }
- chama_utente(aux->info->nome,aux->info->num_serv);
- //Hora de remover o cliente YEY
- //que se faz? ult->prox = aux->prox
- //entao porque ainda nao fiz?
- //simples, e se o ult for NULL?
- //ora aí está, quer dizer que vamos retirar o primeiro da fila, nada de novo
- //mas o que se passa? que tem?
- //simples, como raio vamos alterar o primeiro da fila sem termos o ponteiro para a fila?
- //MUITO OBRIGADO STOR PELA PORCARIA DE PARTE DO EXAME...
- //podem reclamar com o stor por causa desta pergunta
- if(ant == NULL)
- return;
- ant->prox = aux->prox;
- free(aux->info->nome);
- free(aux->info);
- free(aux);
- return;
- }
- }
- //f)
- struct n_serv
- {
- int num;
- int quant;
- };
- void ChamaSemNum(FILA *f)
- {
- if(!f)
- return;
- struct n_serv *serv = NULL;
- NO *aux = f->prim;
- int quant = 0;
- bool found;
- while(aux)
- {
- found = false;
- for(int i=0; i<quant; i++)
- if(serv[i].num == aux->info->num_serv)
- {
- serv[i].quant++;
- aux = aux->prox;
- continue;
- }
- serv = (struct n_serv *)realloc(serv,sizeof(struct n_serv) * (quant+1));
- if(!serv)
- return;
- serv[quant].num = aux->info->num_serv;
- serv[quant].quant = 0;
- quant++;
- aux=aux->prox;
- }
- int max=serv[0].quant, a=0;
- for(int i=1; i<quant; i++)
- if(serv[i].quant > max)
- {
- max = serv[i].quant;
- a = i;
- }
- BuscaPrim(f->prim,serv[a].num);
- }
- //PARTE III
- typedef struct folha
- {
- ListaUtentes *Info; //nos exercicios anteriores fizemos com lista... Boa Stor! Ta cada vez melhor...
- int numero_servico;
- struct folha *Esq, *Dir;
- }FOLHA;
- typedef struct arv_bin
- {
- int NElementos;
- FOLHA *Raiz;
- } Arv_Bin;
- //a) Honestamente... esta é demasiado lixada...
- struct n_serv ContarUtente(FOLHA *f)
- {
- struct n_serv t = {f->numero_servico,f->Info->n_elem};
- struct n_serv aux;
- if(f->Esq)
- {
- aux=ContarUtente(f->Esq);
- if(aux.quant > t.quant)
- t = aux; //nao tenho a certeza se funciona, se nao funcionar: t.num = aux.num;t.quant = aux.quant;
- }
- if(f->Dir)
- {
- aux=ContarUtente(f->Dir);
- if(aux.quant > t.quant)
- t = aux;
- }
- return t;
- }
- int MaisUtentesEspera(Arv_Bin *A)
- {
- return ContarUtente(A->Raiz).num;
- }
- //b)
- FOLHA *BuscarFNum(FOLHA *f, int n)
- {
- if(f->Info->n_serv == n)
- return f;
- if(f->Info->n_serv > n && f->Esq)
- return BuscarFNum(f->Esq, n);
- if(f->Info->n_serv < n && f->Dir)
- return BuscarFNum(f->Dir,n);
- return NULL;
- }
- char *ChamarUtenteDoServico(Arv_Bin *A, int nservico)
- {
- FOLHA *aux = BuscarFNum(A->Raiz, nservico);
- if(!aux)
- return NULL;
- NO *a = aux->Info->prim;
- chama_utente(a->nome,nservico);
- aux->Info->prim = a->prox;
- free(a->info->nome);
- free(a->info);
- free(a);
- return NULL; //No idea what it's supposed to be returned -.-
- }
- //c)
- void DeslocarUtentes(Arv_Bin *A, int ns_old, int ns_new)
- {
- FOLHA *old = BuscarFNum(A->Raiz,ns_old);
- FOLHA *n = BuscarFNum(A->Raiz, ns_new);
- if(!old || !n)
- return;
- NO *aux = n->Info->prim;
- while(aux && aux->prox)
- aux = aux->prox;
- if(!aux)//se a folha estivesse vazia
- {
- n->Info->prim = old->Info->prim;
- n->Info->n_elem = old->Info->n_elem;
- old->Info = NULL;
- return;
- }
- aux->prox = old->Info->prim;
- n->Info->n_elem += old->Info->n_elem;
- old->Info = NULL;
- }
- //d)
- void DestruirLista(ListaUtentes *l)
- {
- NO *aux = l->prim;
- NO *ant = NULL;
- while(aux)
- {
- ant = aux;
- aux = aux->prox;
- free(ant->info->nome);
- free(ant->info);
- free(ant);
- }
- free(l);
- }
- void DestruirFolhas(FOLHA *f)
- {
- if(f->Esq)
- DestruirFolhas(f->Esq);
- if(f->Dir)
- DestruirFolhas(f->Dir);
- if(f->Info)
- DestruirLista(f->Info);
- free(f);
- }
- void DestruirArvore(Arv_Bin *A)
- {
- if(A && A->Raiz)
- DestruirFolhas(A->Raiz);
- free(A);
- }
- //=======================================================
- void main()
- {
- int p, u;
- PrimEUlt(" \n OLA A\n\0", &p, &u);
- printf("Primeiro: %d\nUltimo: %d\n",p, u);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement