Advertisement
Kimossab

ED - Exame Recurso 2015

Aug 21st, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. //PARTE I
  6. //a
  7. char *MinMaiu(char *string)
  8. {
  9.     int len = strlen(string);
  10.     char *res = (char*)malloc(sizeof(char)*(len+1));
  11.     bool maiu = true;
  12.  
  13.     for(int i=0; i<len; i++)
  14.     {
  15.         if(string[i] >= 'a' && string[i] <= 'z')
  16.         {
  17.             if(maiu)
  18.                 res[i] = string[i] + 'A' - 'a';
  19.             else res[i] = string[i];
  20.             maiu = !maiu;
  21.         }
  22.         else if(string[i] >= 'A' && string[i] <= 'Z')
  23.         {
  24.             if(!maiu)
  25.                 res[i] = string[i] - 'A' + 'a';
  26.             else res[i] = string[i];
  27.             maiu = !maiu;
  28.         }
  29.         else res[i] = string[i];
  30.     }
  31.     res[len] = '\0';
  32.  
  33.     return res;
  34. }
  35.  
  36. //b
  37. int TransformaFrase(char *string)
  38. {
  39.     int count = 0;
  40.     bool ini=true;
  41.  
  42.     while(*string != '\0')
  43.     {
  44.         if(*string == ' ')
  45.             ini = true;
  46.         else
  47.         {
  48.             if(ini)
  49.             {
  50.                 ini = false;
  51.                 count++;
  52.  
  53.                 if(*string >= 'a' && *string <= 'z')
  54.                     *string += 'A' - 'a';
  55.             }
  56.             else
  57.             {
  58.                 if(*string >= 'A' && *string <= 'Z')
  59.                     *string -= 'A' - 'a';
  60.             }
  61.         }
  62.         string++;
  63.     }
  64.  
  65.     return count;
  66. }
  67.  
  68. //PARTE II
  69. typedef struct
  70. {
  71.     int N_passageiros;
  72.     int codigo_aviao;
  73. }Aviao;
  74. typedef struct
  75. {
  76.     Aviao *info;
  77.     NOAviao *Prox;
  78. }NOAviao;
  79. typedef struct
  80. {
  81.     char NomeAeroporto[20];
  82.     NOAeroporto *Next;
  83.     NOAviao *i_avioes;
  84. }NOAeroporto;
  85. typedef struct
  86. {
  87.     NOAeroporto *Inicio;
  88. }EDados;
  89.  
  90. //a
  91. EDados *CriarEDados()
  92. {
  93.     EDados *res = (EDados *)malloc(sizeof(EDados));
  94.     res->Inicio = NULL;
  95.     return res;
  96. }
  97.  
  98. //b
  99. void AddAterragemAviao(EDados *Ed, char *aeroporto, int cod_aviao, int npassageiros)
  100. {
  101.     if(!Ed)
  102.         CriarEDados();
  103.    
  104.     NOAeroporto *aux = Ed->Inicio;
  105.     NOAviao *auxav = NULL;
  106.     NOAviao *no = (NOAviao *)malloc(sizeof(NOAviao));
  107.     Aviao *av = (Aviao *)malloc(sizeof(Aviao));
  108.     av->codigo_aviao = cod_aviao;
  109.     av->N_passageiros = npassageiros;
  110.     no->info = av;
  111.     no->Prox = NULL;
  112.  
  113.     if(!aux)
  114.     {
  115.         Ed->Inicio = (NOAeroporto *)malloc(sizeof(NOAeroporto));
  116.         Ed->Inicio->Next = NULL;
  117.         strcpy(Ed->Inicio->NomeAeroporto, aeroporto);
  118.         Ed->Inicio->i_avioes = no;
  119.         return;
  120.     }
  121.  
  122.     while(aux)
  123.     {
  124.         if(!stricmp(aux->NomeAeroporto, aeroporto)) //se for para inserir neste aeroporto
  125.         {
  126.             if(!aux->i_avioes) //se for o primeiro
  127.             {
  128.                 aux->i_avioes = no;
  129.                 return;
  130.             }
  131.  
  132.             auxav = aux->i_avioes;
  133.             while(auxav->Prox) //vai buscar o ultimo
  134.                 auxav=auxav->Prox;
  135.             auxav->Prox = no;
  136.  
  137.             return;
  138.         }
  139.         aux = aux->Next;
  140.     }
  141. }
  142.  
  143. //c
  144. char *AeroportoMaisFrequentado(EDados *Ed)
  145. {
  146.     char *res = NULL;
  147.     int max=0, cont;
  148.  
  149.     NOAeroporto *aero = Ed->Inicio;
  150.     NOAviao *av=NULL;
  151.     while(aero)
  152.     {
  153.         cont = 0;
  154.         av = aero->i_avioes;
  155.         while(av)
  156.         {
  157.             cont+=av->info->N_passageiros;
  158.             av=av->Prox;
  159.         }
  160.  
  161.         if(cont > max)
  162.         {
  163.             max =cont;
  164.             res = aero->NomeAeroporto;
  165.         }
  166.  
  167.         aero = aero->Next;
  168.     }
  169.     return res;
  170. }
  171.  
  172. //d
  173. void Limpeza(EDados *Ed)
  174. {
  175.     if(!Ed)
  176.         return;
  177.  
  178.     NOAviao *av,*aux;
  179.     NOAeroporto *aero = Ed->Inicio, *auxa;
  180.  
  181.     while(aero)
  182.     {
  183.         av=aero->i_avioes;
  184.         while(av)
  185.         {
  186.             free(av->info);
  187.             aux=av->Prox;
  188.             free(av);
  189.             av=aux;
  190.         }
  191.         auxa = aero->Next;
  192.         free(aero);
  193.         aero=auxa;
  194.     }
  195.  
  196.     free(Ed);
  197. }
  198.  
  199. //PARTE III
  200. typedef struct no
  201. {
  202.     NOAviao *Lista;
  203.     char *NomeAeroporto;
  204.     struct no *esq, *dir;
  205. }NO;
  206. typedef struct arv_bin
  207. {
  208.     int NElementos;
  209.     NO *Raiz;
  210. }Arv_Bin;
  211. //a
  212. int MAterragem(NO *n, char *aero)
  213. {
  214.     if(!n)
  215.         return -1;
  216.  
  217.     int esq=-1, dir=-1, cont=0;
  218.     char *nesq, *ndir;
  219.  
  220.     esq=MAterragem(n->esq,nesq); //vai buscar o numro e nome a esquerda
  221.     dir=MAterragem(n->dir,ndir); //vai buscar o numro e nome a direita
  222.  
  223.     NOAviao *aux = n->Lista;
  224.     while(aux)
  225.     {
  226.         cont++;
  227.         aux=aux->Prox;
  228.     }
  229.  
  230.     if(esq > dir && esq > cont) //se a esquerda for a maior
  231.     {
  232.         aero = nesq;
  233.         return esq;
  234.     }
  235.     if(dir > cont) //se a direita for a maior
  236.     {
  237.         aero = ndir;
  238.         return dir;
  239.     }
  240.     //a atual é a maior
  241.     aero = n->NomeAeroporto;
  242.     return cont;
  243. }
  244. char *MaisAterragens(Arv_Bin *A)
  245. {
  246.     if(!A)
  247.         return NULL;
  248.  
  249.     char *aux = NULL;
  250.     MAterragem(A->Raiz,aux);
  251.     return aux;
  252. }
  253.  
  254. //b
  255. int CAterr(NO *n)
  256. {
  257.     int c=0;
  258.     if(n->esq)
  259.         c+=CAterr(n->esq);
  260.     if(n->dir)
  261.         c+=CAterr(n->dir);
  262.  
  263.     NOAviao *aux = n->Lista;
  264.     while(aux)
  265.     {
  266.         c++;
  267.         aux=aux->Prox;
  268.     }
  269.  
  270.     return c;
  271. }
  272. int ContarAterragens(Arv_Bin *A)
  273. {
  274.     if(!A)
  275.         return 0;
  276.     return CAterr(A->Raiz);
  277. }
  278.  
  279. //c
  280. /*
  281. Exemplo do ficheiro final:
  282. Aeroporto : Lisboa
  283.     Codigo Aviao: 2 Passageiros: 420
  284.     Codigo Aviao: 10    Passageiros: 120
  285.     ...
  286.     Codigo Aviao: 17    Passageiros: 210
  287. Aeroporto : Porto
  288.     Codigo Aviao: 13    Passageiros: 195
  289.     ...
  290. ...
  291. Aeroporto : Madrid
  292.     ...
  293. */
  294. void GFich(NO *n, FILE *f)
  295. {
  296.     if(n == NULL)
  297.         return;
  298.  
  299.     NOAviao *aux = n->Lista;
  300.     fprintf(f,"Aeroporto : %s\n",n->NomeAeroporto);
  301.     while(aux)
  302.     {
  303.         fprintf(f,"\tCodigo Aviao: %d\tPassageiros: %d\n", aux->info->codigo_aviao,aux->info->N_passageiros);
  304.         aux = aux->Prox;
  305.     }
  306.  
  307.     GFich(n->esq,f);
  308.     GFich(n->dir,f);
  309. }
  310. void GravarFicheiro(Arv_Bin *A, char *nficheiro)
  311. {
  312.     if(!A)
  313.         return;
  314.     FILE *f = fopen(nficheiro,"w");
  315.     if(!f)
  316.         return;
  317.  
  318.     GFich(A->Raiz,f);
  319.     fclose(f);
  320. }
  321.  
  322. //d
  323. void LimpezaNO(NO *n)
  324. {
  325.     if(!n)
  326.         return;
  327.  
  328.     NOAviao *aux = n->Lista,*aux2;
  329.     LimpezaNO(n->esq);
  330.     LimpezaNO(n->dir);
  331.  
  332.     while(aux)
  333.     {
  334.         free(aux->info);
  335.        
  336.         aux2 = aux;
  337.         free(aux);
  338.         aux=aux2;
  339.     }
  340.     free(n);
  341. }
  342. void LimpezaArvore(Arv_Bin *A)
  343. {
  344.     if(!A)
  345.         return;
  346.     LimpezaNO(A->Raiz);
  347.     free(A);
  348. }
  349.  
  350. //e
  351. int MemNO(NO *n)
  352. {
  353.     if(!n)
  354.         return;
  355.    
  356.     NOAviao *aux = n->Lista;
  357.     int mem=0;
  358.     mem+=MemNO(n->esq);
  359.     mem+=MemNO(n->dir);
  360.    
  361.     while(aux)
  362.     {
  363.         mem+=MemoriaAviao(aux->info);
  364.         mem+=sizeof(NOAviao);
  365.         aux = aux->Prox;
  366.     }
  367.     mem+=sizeof(NO);
  368.     return mem;
  369. }
  370. void MemoriaArvore(Arv_Bin *A)
  371. {
  372.     int mem = sizeof(Arv_Bin);
  373.     mem += MemNO(A->Raiz);
  374.     printf("A memoria total ocupada pela arvore e: %d Bytes", mem);
  375. }
  376.  
  377. void main ()
  378. {
  379.     //PARTE I
  380.     char *res = MinMaiu("ola PESSOAL");
  381.     printf("%s\n\n", res);
  382.     int pal = TransformaFrase(res);
  383.     printf("%d pal\n%s\n\n",pal, res);
  384.     free(res);
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement