int mi_atoi(const char *cad, int tam) { int i=tam-1, entero=0, pos=1; while (i>=0) { while (caracter_valido(cad[i])){ entero=entero+(cad[i]-48)*pos; pos=pos*10; i--; } pos=1; i--; } return entero; }
void ler_entrada(char *arquivo, Indice **Ml, Indice **Mh){ FILE *entrada; /* Ponteiro para o arquivo de entrada */ char c, buf[TAMANHO_BUFFER]; int objeto_linha, objeto_coluna; /* v,w respectivamente*/ int parametros = 0; int i = 0, j = 0; int linhas_ml, linhas_mh, valor; /* Leitura da qnt de objetos */ entrada = fopen(arquivo,"r"); if(entrada == NULL) erro(ARQUIVONAOEXISTE); while(!parametros){ c = fgetc(entrada); while(caracter_valido(c)) { buf[i] = c; i++; c = fgetc(entrada); } buf[i] = '\0'; n = atoi(buf); parametros++; i = 0; } tamanho_M = ((n * (1 + n))/2) - (n-1) - 1; /* (Soma de PA de 1 ate n) - enesimo termo */ linhas_ml = 0; linhas_mh = 0; *Ml = malloc(tamanho_M*sizeof(Indice)); *Mh = malloc(tamanho_M*sizeof(Indice)); if(*Ml == NULL) erro(MALLOC_lh); if(*Mh == NULL) erro(MALLOC_lh); /* Leitura de Mh */ objeto_linha = 0; objeto_coluna = objeto_linha + 1; while(linhas_ml < n-1){ reset_buffer(buf); c = fgetc(entrada); while(caracter_valido(c)){ buf[i] = c; i++; c = fgetc(entrada); } valor = atoi(buf); set_M(*Ml,j,valor,objeto_linha,objeto_coluna); j++; objeto_coluna++; if(c == '\n') { objeto_linha = objeto_linha + 1; objeto_coluna = objeto_linha + 1; linhas_ml++; } i = 0; } /* Leitura de Ml */ j = 0; objeto_linha = 0; objeto_coluna = objeto_linha + 1; while(linhas_mh < n-1){ reset_buffer(buf); c = fgetc(entrada); while(caracter_valido(c)){ buf[i] = c; i++; c = fgetc(entrada); } valor = atoi(buf); set_M(*Mh,j,valor,objeto_linha,objeto_coluna); j++; objeto_coluna++; if(c == '\n') { objeto_linha = objeto_linha + 1; objeto_coluna = objeto_linha + 1; linhas_mh++; } i = 0; } fclose(entrada); }