Advertisement
Kimossab

ED - Freq 2015

Jun 15th, 2015
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <malloc.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. //PARTE I
  8. //a)
  9. //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)
  10. //vou fazer a ultima
  11. void PrimEUlt(char *s, int *p, int *u)
  12. {
  13.     *p = -1;
  14.     int i=0;
  15.     while(*s != '\0')
  16.     {
  17.         if(*s != ' ' && *s != '\n' && *s != '\t')
  18.         {
  19.             if(*p == -1)
  20.                 *p = i;
  21.             else *u = i;
  22.         }
  23.         i++;
  24.         s++;
  25.     }
  26. }
  27.  
  28. //b)
  29. //neste é para fazer um programa, mas pode-se usar a função feita em a)
  30. char *GetSubStr(char *str, int p, int u)
  31. {
  32.     char res[150];
  33.     for(int i=0; i<=u-p; i++)
  34.     {
  35.         res[i] = str[i+p];
  36.     }
  37.     if(res[u-p] != '\0')
  38.         res[u-p+1] = '\0';
  39.     return res;
  40. }
  41.  
  42. void LerFich()
  43. {
  44.     FILE *f = fopen("texto.txt","r");
  45.  
  46.     int p,u;
  47.     char buffer[150];
  48.     while(fgets(buffer, sizeof(buffer),f))
  49.     {
  50.         PrimEUlt(buffer,&p,&u);
  51.         printf("%s\n",GetSubStr(buffer, p, u));
  52.     }
  53.  
  54.     fclose(f);
  55. }
  56.  
  57. //PARTE II
  58. //a)
  59. typedef struct
  60. {
  61.     char *nome;
  62.     int num_serv;
  63.     int hora;
  64. }UTENTE;
  65. typedef struct no
  66. {
  67.     UTENTE *info;
  68.     no *prox;
  69. }NO;
  70. typedef struct
  71. {
  72.     NO *prim;
  73. }FILA;
  74.  
  75. //b)
  76. void InsertUt(NO *p, char *nome, int num)
  77. {
  78.     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*
  79.         return;
  80.    
  81.     //Thank you teacher... por nao nos fornecer ajuda acerca da hora *claps*
  82.     time_t timer;
  83.     struct tm *t;
  84.     timer = time(NULL);
  85.     t = localtime(&timer);
  86.     UTENTE *novo = (UTENTE *)malloc(sizeof(UTENTE));
  87.     if(novo == NULL) //erro de alocagem de memoria (memoria insuficiente)
  88.         return;
  89.  
  90.     NO *aux = p;
  91.     NO *n;
  92.  
  93.     novo->nome = nome;
  94.     novo->num_serv = num;
  95.     novo->hora = t->tm_hour;
  96.     while(aux->prox)
  97.         aux = aux->prox;
  98.    
  99.     n = (NO *)malloc(sizeof(NO));
  100.     if(!n)
  101.         return;
  102.  
  103.     n->prox = NULL;
  104.     n->info = novo;
  105.  
  106.     n->prox = aux;
  107. }
  108.  
  109. //d)
  110. int NumUtServ(NO *p, int serv)
  111. {
  112.     if(!p)
  113.         return -1;
  114.  
  115.     int num=0;
  116.     NO *aux = p;
  117.  
  118.     while(aux)
  119.     {
  120.         if(aux->info->num_serv == serv)
  121.             num++;
  122.         aux = aux->prox;
  123.     }
  124.     return num;
  125. }
  126.  
  127. //e)
  128. void BuscaPrim(NO *p, int serv)
  129. {
  130.     if(!p)
  131.         return;
  132.  
  133.     NO *aux = p;
  134.     NO *ant = NULL;
  135.  
  136.     //Parabens!!! Podes ter um problema insolucionavel aqui!!!
  137.     while(aux)
  138.     {
  139.         if(aux->info->num_serv != serv)
  140.         {
  141.             ant = aux;
  142.             aux = aux->prox;
  143.             continue;
  144.         }
  145.  
  146.         chama_utente(aux->info->nome,aux->info->num_serv);
  147.         //Hora de remover o cliente YEY
  148.         //que se faz? ult->prox = aux->prox
  149.         //entao porque ainda nao fiz?
  150.         //simples, e se o ult for NULL?
  151.         //ora aí está, quer dizer que vamos retirar o primeiro da fila, nada de novo
  152.         //mas o que se passa? que tem?
  153.         //simples, como raio vamos alterar o primeiro da fila sem termos o ponteiro para a fila?
  154.         //MUITO OBRIGADO STOR PELA PORCARIA DE PARTE DO EXAME...
  155.         //podem reclamar com o stor por causa desta pergunta
  156.         if(ant == NULL)
  157.             return;
  158.         ant->prox = aux->prox;
  159.         free(aux->info->nome);
  160.         free(aux->info);
  161.         free(aux);
  162.         return;
  163.     }
  164. }
  165.  
  166. //f)
  167. struct n_serv
  168. {
  169.     int num;
  170.     int quant;
  171. };
  172. void ChamaSemNum(FILA *f)
  173. {
  174.     if(!f)
  175.         return;
  176.     struct n_serv *serv = NULL;
  177.     NO *aux = f->prim;
  178.     int quant = 0;
  179.     bool found;
  180.  
  181.     while(aux)
  182.     {
  183.         found = false;
  184.         for(int i=0; i<quant; i++)
  185.             if(serv[i].num == aux->info->num_serv)
  186.             {
  187.                 serv[i].quant++;
  188.                 aux = aux->prox;
  189.                 continue;
  190.             }
  191.         serv = (struct n_serv *)realloc(serv,sizeof(struct n_serv) * (quant+1));
  192.         if(!serv)
  193.             return;
  194.         serv[quant].num = aux->info->num_serv;
  195.         serv[quant].quant = 0;
  196.         quant++;
  197.         aux=aux->prox;
  198.     }
  199.  
  200.     int max=serv[0].quant, a=0;
  201.     for(int i=1; i<quant; i++)
  202.         if(serv[i].quant > max)
  203.         {
  204.             max = serv[i].quant;
  205.             a = i;
  206.         }
  207.  
  208.     BuscaPrim(f->prim,serv[a].num);
  209. }
  210.  
  211. //PARTE III
  212. typedef struct folha
  213. {
  214.     ListaUtentes *Info; //nos exercicios anteriores fizemos com lista... Boa Stor! Ta cada vez melhor...
  215.     int numero_servico;
  216.     struct folha *Esq, *Dir;
  217. }FOLHA;
  218. typedef struct arv_bin
  219. {
  220.     int NElementos;
  221.     FOLHA *Raiz;
  222. } Arv_Bin;
  223. //a) Honestamente... esta é demasiado lixada...
  224. struct n_serv ContarUtente(FOLHA *f)
  225. {
  226.     struct n_serv t = {f->numero_servico,f->Info->n_elem};
  227.     struct n_serv aux;
  228.     if(f->Esq)
  229.     {
  230.         aux=ContarUtente(f->Esq);
  231.         if(aux.quant > t.quant)
  232.             t = aux; //nao tenho a certeza se funciona, se nao funcionar: t.num = aux.num;t.quant = aux.quant;
  233.     }
  234.     if(f->Dir)
  235.     {
  236.         aux=ContarUtente(f->Dir);
  237.         if(aux.quant > t.quant)
  238.             t = aux;
  239.     }
  240.     return t;
  241. }
  242. int MaisUtentesEspera(Arv_Bin *A)
  243. {
  244.     return ContarUtente(A->Raiz).num;
  245. }
  246. //b)
  247. FOLHA *BuscarFNum(FOLHA *f, int n)
  248. {
  249.     if(f->Info->n_serv == n)
  250.         return f;
  251.     if(f->Info->n_serv > n && f->Esq)
  252.         return BuscarFNum(f->Esq, n);
  253.     if(f->Info->n_serv < n && f->Dir)
  254.         return BuscarFNum(f->Dir,n);
  255.     return NULL;
  256. }
  257. char *ChamarUtenteDoServico(Arv_Bin *A, int nservico)
  258. {
  259.     FOLHA *aux = BuscarFNum(A->Raiz, nservico);
  260.     if(!aux)
  261.         return NULL;
  262.  
  263.     NO *a = aux->Info->prim;
  264.     chama_utente(a->nome,nservico);
  265.     aux->Info->prim = a->prox;
  266.     free(a->info->nome);
  267.     free(a->info);
  268.     free(a);
  269.     return NULL; //No idea what it's supposed to be returned -.-
  270. }
  271.  
  272. //c)
  273. void DeslocarUtentes(Arv_Bin *A, int ns_old, int ns_new)
  274. {
  275.     FOLHA *old = BuscarFNum(A->Raiz,ns_old);
  276.     FOLHA *n = BuscarFNum(A->Raiz, ns_new);
  277.     if(!old || !n)
  278.         return;
  279.  
  280.     NO *aux = n->Info->prim;
  281.     while(aux && aux->prox)
  282.         aux = aux->prox;
  283.  
  284.     if(!aux)//se a folha estivesse vazia
  285.     {
  286.         n->Info->prim = old->Info->prim;
  287.         n->Info->n_elem = old->Info->n_elem;
  288.         old->Info = NULL;
  289.         return;
  290.     }
  291.  
  292.     aux->prox = old->Info->prim;
  293.     n->Info->n_elem += old->Info->n_elem;
  294.     old->Info = NULL;
  295. }
  296.  
  297. //d)
  298. void DestruirLista(ListaUtentes *l)
  299. {
  300.     NO *aux = l->prim;
  301.     NO *ant = NULL;
  302.     while(aux)
  303.     {
  304.         ant = aux;
  305.         aux = aux->prox;
  306.         free(ant->info->nome);
  307.         free(ant->info);
  308.         free(ant);
  309.     }
  310.     free(l);
  311. }
  312. void DestruirFolhas(FOLHA *f)
  313. {
  314.     if(f->Esq)
  315.         DestruirFolhas(f->Esq);
  316.     if(f->Dir)
  317.         DestruirFolhas(f->Dir);
  318.     if(f->Info)
  319.         DestruirLista(f->Info);
  320.     free(f);
  321. }
  322. void DestruirArvore(Arv_Bin *A)
  323. {
  324.     if(A && A->Raiz)
  325.         DestruirFolhas(A->Raiz);
  326.     free(A);
  327. }
  328. //=======================================================
  329. void main()
  330. {
  331.     int p, u;
  332.     PrimEUlt(" \n OLA A\n\0", &p, &u);
  333.     printf("Primeiro: %d\nUltimo: %d\n",p, u);
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement