struct lista * cpu_1664_dev_opera_parametre_funsiona__6r2r(struct cpu_1664 * cpu, n1 bait) { struct lista *lista_2=lista_nova(0); struct lista *lista_parametre=lista_nova(0); struct lista *lista_informa=lista_nova(0); n1 A=bait&((1<<cpu_1664_bitio_r)-1); n1 B=bait>>cpu_1664_bitio_r; lista_ajunta_asciiexadesimal__n1(lista_parametre, A); lista_ajunta__dato(lista_parametre, ' '); lista_ajunta_asciiexadesimal__n1(lista_parametre, B); lista_ajunta_asciiexadesimal__n8(lista_informa, cpu->sinia[A]); lista_ajunta__dato(lista_informa, ' '); lista_ajunta_asciiexadesimal__n8(lista_informa, cpu->sinia[B]); lista_ajunta__P(lista_2, (P)lista_parametre); lista_ajunta__P(lista_2, (P)lista_informa); return lista_2; }
lista *lerFilmes(FILE *ficheiro) { char buffer[256], *nlptr; lista *filmes; if (ficheiro == NULL) return NULL; filmes = lista_nova(); while (!feof(ficheiro)) { fgets(buffer, 255, ficheiro); nlptr = strchr(buffer, '\n'); if (nlptr) *nlptr = '\0'; lista_insere(filmes, buffer, NULL); } return filmes; }
lista *pesquisa_destinos(grafo *rotas, lista *aeroportos, const char *origem) { /* prob 2.2 - a implementar */ int i=0, j; vetor *v; elemento *e; lista *l= lista_nova(); for(e=aeroportos->inicio;e!=NULL&& strcmp(e->str, origem);e=e->proximo){ i++; } v = v_sucessores(rotas, i); for(i=0;i<v->tamanho;i++){ e = aeroportos->inicio; for(j=0; j<vetor_elemento(v, i);j++){ e=e->proximo; } lista_insere(l, e->str, NULL); } vetor_apaga(v); return l; }
int main() { FILE *f; grafo *g = NULL; lista *aeroportos; char *buff; size_t n; setvbuf(stdout, NULL, _IONBF, 0); f = fopen("aeroportos.txt", "r"); aeroportos = lista_nova(); buff = NULL; n = 0; while (getline(&buff, &n, f) != -1) { buff[strlen(buff) - 1] = '\0'; lista_insere(aeroportos, buff, NULL); } free(buff); fclose(f); g = carrega_rotas(aeroportos, "rotas.txt"); /* teste problema 2.1 */ { FILE *f; char tempo[5], origem[10]; lista *tempos, *origens; printf("* Problema 2.1 *\n"); f = fopen("chegadas.txt", "r"); tempos = lista_nova(); origens = lista_nova(); while (fscanf(f, "%s\t%s", tempo, origem) == 2) { lista_insere(tempos, tempo, NULL); lista_insere(origens, origem, NULL); } if (proximas_n_chegadas(tempos, origens, aeroportos, 5) != 1) printf("Erro ao executar a funcao\n"); lista_apaga(tempos); lista_apaga(origens); fclose(f); } /* teste problema 2.2 */ { printf("\n* Problema 2.2 *\n"); const char *origem = "Lille (FR)"; elemento *elem; lista *l = pesquisa_destinos(g, aeroportos, origem); if (l == NULL) printf("Destinos diretos a partir de %s = 0\n", origem); else { printf("Destinos diretos a partir de %s = %d\n", origem, lista_tamanho(l)); for (elem = l->inicio; elem != NULL; elem = elem->proximo) printf("%s\n", elem->str); lista_apaga(l); } } grafo_apaga(g); lista_apaga(aeroportos); return 0; }
int main() { /* teste problema 1.1 */ { vetor *v; pilha *res; int i; char data[][11] = {"Estocolmo", "Oslo", "Helsinquia", "Copenhaga", "Reykjavik"}; printf("* Problema 1.1 *\n"); v = vetor_novo(); for(i=0; i<5; i++) vetor_insere(v, data[i], -1); printf("Vetor: "); for(i=0; i<v->tamanho; i++) printf("%s ", vetor_elemento(v, i)); printf("\n"); res = vetor_pilha(v); printf("Pilha: "); if(res) { while(!pilha_vazia(res)) { printf("%s ", pilha_top(res)); pilha_pop(res); } } printf("\n"); if(v->tamanho != 5) printf("ERRO: Vetor original nao deve ser alterado!\n"); else { for(i=0; i<5; i++) if(strcmp(data[i], vetor_elemento(v, i)) != 0) { printf("ERRO: Vetor original nao deve ser alterado!\n"); break; } } vetor_apaga(v); pilha_apaga(res); } printf("\n"); /* teste problema 1.2 */ { int res, i, ok; objeto obj; char data[][20] = { "Paul Atreides", "Leto Atreides", "Voran Atreides", "Vladimir Harkonnen", "Dmitri Harkonnen", "Hundro Moritani" }; tabela_dispersao td1, td2; printf("* Problema 1.2 *\n"); td1 = tabela_cria(11, hash_djbm); td2 = tabela_cria(7, hash_djbm); for(i=0; i<6; i++) { strcpy(obj.chave, data[i]); strcpy(obj.valor, data[i]); tabela_adiciona(td1, &obj); tabela_adiciona(td2, &obj); } res = tabela_redimensiona(td1, 7); if(td1->tamanho == 7) { elemento *aux1, *aux2; for(i=0; i<td1->tamanho; i++) { aux2 = td2->elementos[i]; while(aux2 != NULL) { ok = 0; aux1 = td1->elementos[i]; while(aux1 != NULL) { if(strcmp(aux1->obj->chave, aux2->obj->chave) == 0) { ok = 1; break; } aux1 = aux1->proximo; } if(!ok) { printf("ERRO: Elemento %s em posicao errada\n", aux2->obj->chave); break; } aux2 = aux2->proximo; } } if(ok) printf("Tabela de dispersao redimensionada corretamente.\n"); } else printf("ERRO: Tabela de dispersao deveria ter novo tamanho.\n"); tabela_apaga(td1); tabela_apaga(td2); printf("\n"); } /* teste problema 1.3 */ { int i; arvore_avl *avl; lista *res, *indicacoes; char data[][20] = { "prog2", "mt2", "trabalho", "e'", "muito", "zzzz...", "dificil", "facil", "nada", "verdade"}; avl = avl_nova(); printf("* Problema 1.3 *\n"); for(i=0; i<10; i++) avl_insere(avl, data[i]); indicacoes = lista_nova(); lista_insere(indicacoes, "esquerda", -1); lista_insere(indicacoes, "esquerda", -1); lista_insere(indicacoes, "direita", -1); res = descobre_segredo(avl, indicacoes); printf("Segredo: "); if(res) { for(i=0; i<res->tamanho; i++) printf("%s ", lista_elemento(res, i)); } printf("\n\nSegredo (impl. recursiva): "); descobre_segredo_recurs(avl->raiz, indicacoes); avl_apaga(avl); lista_apaga(indicacoes); lista_apaga(res); printf("\n\n"); } return 0; }