Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- //PARTE I
- //a
- char *MinMaiu(char *string)
- {
- int len = strlen(string);
- char *res = (char*)malloc(sizeof(char)*(len+1));
- bool maiu = true;
- for(int i=0; i<len; i++)
- {
- if(string[i] >= 'a' && string[i] <= 'z')
- {
- if(maiu)
- res[i] = string[i] + 'A' - 'a';
- else res[i] = string[i];
- maiu = !maiu;
- }
- else if(string[i] >= 'A' && string[i] <= 'Z')
- {
- if(!maiu)
- res[i] = string[i] - 'A' + 'a';
- else res[i] = string[i];
- maiu = !maiu;
- }
- else res[i] = string[i];
- }
- res[len] = '\0';
- return res;
- }
- //b
- int TransformaFrase(char *string)
- {
- int count = 0;
- bool ini=true;
- while(*string != '\0')
- {
- if(*string == ' ')
- ini = true;
- else
- {
- if(ini)
- {
- ini = false;
- count++;
- if(*string >= 'a' && *string <= 'z')
- *string += 'A' - 'a';
- }
- else
- {
- if(*string >= 'A' && *string <= 'Z')
- *string -= 'A' - 'a';
- }
- }
- string++;
- }
- return count;
- }
- //PARTE II
- typedef struct
- {
- int N_passageiros;
- int codigo_aviao;
- }Aviao;
- typedef struct
- {
- Aviao *info;
- NOAviao *Prox;
- }NOAviao;
- typedef struct
- {
- char NomeAeroporto[20];
- NOAeroporto *Next;
- NOAviao *i_avioes;
- }NOAeroporto;
- typedef struct
- {
- NOAeroporto *Inicio;
- }EDados;
- //a
- EDados *CriarEDados()
- {
- EDados *res = (EDados *)malloc(sizeof(EDados));
- res->Inicio = NULL;
- return res;
- }
- //b
- void AddAterragemAviao(EDados *Ed, char *aeroporto, int cod_aviao, int npassageiros)
- {
- if(!Ed)
- CriarEDados();
- NOAeroporto *aux = Ed->Inicio;
- NOAviao *auxav = NULL;
- NOAviao *no = (NOAviao *)malloc(sizeof(NOAviao));
- Aviao *av = (Aviao *)malloc(sizeof(Aviao));
- av->codigo_aviao = cod_aviao;
- av->N_passageiros = npassageiros;
- no->info = av;
- no->Prox = NULL;
- if(!aux)
- {
- Ed->Inicio = (NOAeroporto *)malloc(sizeof(NOAeroporto));
- Ed->Inicio->Next = NULL;
- strcpy(Ed->Inicio->NomeAeroporto, aeroporto);
- Ed->Inicio->i_avioes = no;
- return;
- }
- while(aux)
- {
- if(!stricmp(aux->NomeAeroporto, aeroporto)) //se for para inserir neste aeroporto
- {
- if(!aux->i_avioes) //se for o primeiro
- {
- aux->i_avioes = no;
- return;
- }
- auxav = aux->i_avioes;
- while(auxav->Prox) //vai buscar o ultimo
- auxav=auxav->Prox;
- auxav->Prox = no;
- return;
- }
- aux = aux->Next;
- }
- }
- //c
- char *AeroportoMaisFrequentado(EDados *Ed)
- {
- char *res = NULL;
- int max=0, cont;
- NOAeroporto *aero = Ed->Inicio;
- NOAviao *av=NULL;
- while(aero)
- {
- cont = 0;
- av = aero->i_avioes;
- while(av)
- {
- cont+=av->info->N_passageiros;
- av=av->Prox;
- }
- if(cont > max)
- {
- max =cont;
- res = aero->NomeAeroporto;
- }
- aero = aero->Next;
- }
- return res;
- }
- //d
- void Limpeza(EDados *Ed)
- {
- if(!Ed)
- return;
- NOAviao *av,*aux;
- NOAeroporto *aero = Ed->Inicio, *auxa;
- while(aero)
- {
- av=aero->i_avioes;
- while(av)
- {
- free(av->info);
- aux=av->Prox;
- free(av);
- av=aux;
- }
- auxa = aero->Next;
- free(aero);
- aero=auxa;
- }
- free(Ed);
- }
- //PARTE III
- typedef struct no
- {
- NOAviao *Lista;
- char *NomeAeroporto;
- struct no *esq, *dir;
- }NO;
- typedef struct arv_bin
- {
- int NElementos;
- NO *Raiz;
- }Arv_Bin;
- //a
- int MAterragem(NO *n, char *aero)
- {
- if(!n)
- return -1;
- int esq=-1, dir=-1, cont=0;
- char *nesq, *ndir;
- esq=MAterragem(n->esq,nesq); //vai buscar o numro e nome a esquerda
- dir=MAterragem(n->dir,ndir); //vai buscar o numro e nome a direita
- NOAviao *aux = n->Lista;
- while(aux)
- {
- cont++;
- aux=aux->Prox;
- }
- if(esq > dir && esq > cont) //se a esquerda for a maior
- {
- aero = nesq;
- return esq;
- }
- if(dir > cont) //se a direita for a maior
- {
- aero = ndir;
- return dir;
- }
- //a atual é a maior
- aero = n->NomeAeroporto;
- return cont;
- }
- char *MaisAterragens(Arv_Bin *A)
- {
- if(!A)
- return NULL;
- char *aux = NULL;
- MAterragem(A->Raiz,aux);
- return aux;
- }
- //b
- int CAterr(NO *n)
- {
- int c=0;
- if(n->esq)
- c+=CAterr(n->esq);
- if(n->dir)
- c+=CAterr(n->dir);
- NOAviao *aux = n->Lista;
- while(aux)
- {
- c++;
- aux=aux->Prox;
- }
- return c;
- }
- int ContarAterragens(Arv_Bin *A)
- {
- if(!A)
- return 0;
- return CAterr(A->Raiz);
- }
- //c
- /*
- Exemplo do ficheiro final:
- Aeroporto : Lisboa
- Codigo Aviao: 2 Passageiros: 420
- Codigo Aviao: 10 Passageiros: 120
- ...
- Codigo Aviao: 17 Passageiros: 210
- Aeroporto : Porto
- Codigo Aviao: 13 Passageiros: 195
- ...
- ...
- Aeroporto : Madrid
- ...
- */
- void GFich(NO *n, FILE *f)
- {
- if(n == NULL)
- return;
- NOAviao *aux = n->Lista;
- fprintf(f,"Aeroporto : %s\n",n->NomeAeroporto);
- while(aux)
- {
- fprintf(f,"\tCodigo Aviao: %d\tPassageiros: %d\n", aux->info->codigo_aviao,aux->info->N_passageiros);
- aux = aux->Prox;
- }
- GFich(n->esq,f);
- GFich(n->dir,f);
- }
- void GravarFicheiro(Arv_Bin *A, char *nficheiro)
- {
- if(!A)
- return;
- FILE *f = fopen(nficheiro,"w");
- if(!f)
- return;
- GFich(A->Raiz,f);
- fclose(f);
- }
- //d
- void LimpezaNO(NO *n)
- {
- if(!n)
- return;
- NOAviao *aux = n->Lista,*aux2;
- LimpezaNO(n->esq);
- LimpezaNO(n->dir);
- while(aux)
- {
- free(aux->info);
- aux2 = aux;
- free(aux);
- aux=aux2;
- }
- free(n);
- }
- void LimpezaArvore(Arv_Bin *A)
- {
- if(!A)
- return;
- LimpezaNO(A->Raiz);
- free(A);
- }
- //e
- int MemNO(NO *n)
- {
- if(!n)
- return;
- NOAviao *aux = n->Lista;
- int mem=0;
- mem+=MemNO(n->esq);
- mem+=MemNO(n->dir);
- while(aux)
- {
- mem+=MemoriaAviao(aux->info);
- mem+=sizeof(NOAviao);
- aux = aux->Prox;
- }
- mem+=sizeof(NO);
- return mem;
- }
- void MemoriaArvore(Arv_Bin *A)
- {
- int mem = sizeof(Arv_Bin);
- mem += MemNO(A->Raiz);
- printf("A memoria total ocupada pela arvore e: %d Bytes", mem);
- }
- void main ()
- {
- //PARTE I
- char *res = MinMaiu("ola PESSOAL");
- printf("%s\n\n", res);
- int pal = TransformaFrase(res);
- printf("%d pal\n%s\n\n",pal, res);
- free(res);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement