Esempio n. 1
0
int func_optimize(tree* my_tree, bool* is_optimized)
{
	assert(my_tree);
	if (my_tree -> type != TR_F) return TREE_OK;
	
	assert(is_optimized);
	assert(my_tree -> right);

	

	int argc = 0;
	int ret = 0;
	int number_of_func = 0;

	CALL(count_args(my_tree, &argc));
	CALL(verify_func(my_tree, argc, &number_of_func));

	if (is_possible_func_optimize(my_tree, argc) == false) return TREE_OK;

	double argv[MAXVARS] = {};

	CALL(get_args(my_tree, argv, (const int)argc));

	double value = 0;

	CALL((math_funcs[number_of_func].func)(argv, &value));
	
	CALL(tree_destr(my_tree -> right));
	CALL(tree_clear(my_tree));
	CALL(tree_burn(my_tree, TR_N, value));
	*is_optimized = true;

	return TREE_OK;
}
Esempio n. 2
0
int tree_burn(b_tree** target){
	short int counter;

	if (target == NULL)
		return ERROR_FORMAT;
	if (*target == NULL)
		return ERROR_STREAM;

	b_tree* subtarget;
	subtarget = (*target);

	for (counter = 0; counter < ORDEM - 1; counter++){
		
		tree_burn(&(subtarget->branches[counter]));

		if ((subtarget->index[counter]) != NULL){
			free(subtarget->index[counter]);
			//printf("counter: %d\n", coAunter);
		}
	}


	tree_burn(&(subtarget->branches[ORDEM - 1]));
}
Esempio n. 3
0
int tree_init(tree** dest_save_tree_token, tree_head* head_ptr, tree* tree_to_stick_to, char* func_name, int tree_type, int add_side)
{
	assert(dest_save_tree_token != NULL);
	assert(head_ptr != NULL);
	assert(tree_to_stick_to != NULL);
	assert(func_name != NULL);
	assert(add_side == LEFT || add_side == RIGHT);
	
	int ret = tree_ctr(dest_save_tree_token, head_ptr);
	VERIFY2(ret != TREE_OK,	SNTX_CTR_FAILED, "%s: %s", func_name, sntx_errors[CTR_FAIL]);
	ret = tree_burn(*dest_save_tree_token, tree_type);
	VERIFY2(ret != TREE_OK,	SNTX_BURN_FAILED,"%s: %s", func_name, sntx_errors[BURN_FAIL]);
	
	if (add_side == RIGHT)	ret = tree_add_right(tree_to_stick_to, *dest_save_tree_token);
	else					ret = tree_add_left (tree_to_stick_to, *dest_save_tree_token);
	


	VERIFY2(ret != TREE_OK,	SNTX_ADD_FAILED, "%s: %s", func_name, sntx_errors[ADD_FAIL]);
	
	return TREE_OK;
}
Esempio n. 4
0
int main(int argc, char const *argv[]) {


    int op=0, seek;

    FILE *prim, *list, *saida;

    registro *reg, *in;
    i_primario* test_index= (i_primario*) malloc(sizeof(i_primario));

    in = (registro*)malloc(sizeof(registro));

    int oparq;
    int aux;

    char line[120];
    char search[11];

    list=fopen("data/lista.txt","r+");
    rewind(list);
    prim=fopen("data/indicelista.bt","w+");

    if(list == NULL || prim == NULL ) {
        printf("Erro!\n");
        exit(1);
    }



    b_tree* tree;
    btree_create(&tree);
    build_tree(list,&tree);

    tree_run_space(tree, 0);
    reg_buildall(list, &reg);

    gera_primario(prim,&reg);
    reg_run(reg);

    while(op >= 0) {

        printf("-----MENU-----\n1 - Add Register\n2 - Search Register\n(-1) - Exit\n");
        scanf("%d", &op);


        if((op==1)) {

            //Adiciona registro

            printf("Digite o registro:\n");
            printf("Matricula: ");
            scanf("%s", line);
            getchar();
            strcpy(in->matric,line);
            printf("\nNome: ");

            gets(line);


            if(strlen(line)<NAMEMAX) {
                do {
                    strcat(line," ");
                } while(strlen(line)<NAMEMAX + 1);
            }
            strcpy(in->nome,line);


            printf("\nOption: ");
            scanf("%s", line);
            getchar();
            aux = atoi(line);
            in->op = aux;

            printf("\nCurso: ");
            scanf("%s",line);
            getchar();
            strcpy(in->curso,line);
            printf("\nTurma: (1)Turma A  (2) Turma B: ");
            scanf("%d",&oparq);
            if(oparq==1)
                in->turma='A';
            else
                in->turma='B';

            // Imprimir registro no arquivo
            reg_push(in, list);
            //Inserir na lista


            prim_build(list,in,&test_index);
            printf("nome %s\n",test_index->key);

            tree_run_space(tree, 0);
            insert(&tree, test_index);
            tree_run_space(tree, 0);

            printf("Fim da insercao\n");
        }

        else if((op==2)) {
            //Procurar registro
            printf("Digite o registro a ser procurado (matricula+4 primeiras letras do nome).\n");
            getchar();
            scanf("%s",search);
            seek = key_seek(tree, search);
            switch (seek) {

            case ERROR_DATA:
                printf("Chave não encontrada.\n");
                break;
            case ERROR_STREAM:
                printf("Erro do tipo Bad Input;\n");
                break;
            default:
                printf("Registro Encontrado!Na linha %d do arquivo original.\n", seek );



            }


        } else
            op = -1;

        //Destruimos a arvore para poder então reconstruíla.
        tree_burn(&tree);
        btree_create(&tree);
        rewind(list);

        build_tree(list,&tree);
        reg_buildall(list, &reg);

        rewind(list);
        rewind(prim);
        //gera_primario(prim,&reg);
        tree_run_space(tree, 0);


    }

    gera_primario(prim,&reg);
    free(test_index);
    free(in);
    fclose(prim);
    fclose(list);
    tree_burn(&tree);

    printf("End of Execution.\n");



    return 0;
}