next up previous
Seguinte: fbruta.h Acima: Códigos-fonte: Solução por força Anterior: inout.h

inout.c


001   #include "inout.h"
002   #include <stdio.h>
003   #include <string.h>
004   #include <limits.h>
005   #include <stdlib.h>
006   #include <ctype.h>
007   
008   
009   // Lê um arquivo de entrada especificado por szFName,
010   // armazenando os dados lidos na estrutura PcvData
011   int readPcvFile(const char *szFName, TPcvData *PcvData)
012   {
013       static const char *szDataDelim = "EDGE_WEIGHT_SECTION";
014   
015       FILE *fInput;
016       char szLine[512];
017       int i = 0, j, nMaxj, nDist, nPos, nPos1, bAchouDados = 0;
018       long lFilePos;
019   
020       // Abre o arquivo de entrada
021       fInput = fopen(szFName, "r");
022       if (!fInput) return 0;
023   
024       // Inicializa a estrutura PcvData
025       memset(PcvData->anDist, 0, sizeof(PcvData->anDist));
026       PcvData->nDistDelta = 0;
027   
028       // Estabelece o limite de vértices a ler
029       nMaxj = (PcvData->nNumVertices <= 0 ? MAX_VERTICES :
030           PcvData->nNumVertices);
031   
032       // Procura pela linha com a string "EDGE_WEIGHT_SECTION",
033       // ou pela primeira linha iniciando por um caractere numérico
034       while (!feof(fInput) && !bAchouDados) {
035           lFilePos = ftell(fInput);
036           fgets(szLine, sizeof(szLine), fInput);
037           if (strncasecmp(szLine, szDataDelim, strlen(szDataDelim)) == 0) {
038               bAchouDados = 1;
039           } else if (isdigit(szLine[0])) {
040               fseek(fInput, lFilePos, SEEK_SET);
041               bAchouDados = 1;
042           }
043       }
044   
045       // Interrompe se o arquivo chegou ao final antes de apresentar dados
046       if (feof(fInput)) {
047           fclose(fInput);
048           return 0;
049       }
050   
051       // Lê os dados (custos das arestas) do arquivo de entrada
052       while (!feof(fInput)) {
053           nPos = 0;
054           j = i + 1;
055   
056           // Lê uma linha de texto do arquivo de entrada
057           fgets(szLine, sizeof(szLine), fInput);
058   
059           // Obtém e armazena cada valor inteiro encontrado
060           while ((j < nMaxj) && (sscanf(szLine + nPos, "%d%n", &nDist, &nPos1) > 0)) {
061               nPos += nPos1;
062               PcvData->anDist[i][j] = nDist;
063               PcvData->anDist[j][i] = nDist;
064   
065               // Procura pela aresta de maior custo
066               if (nDist > PcvData->nDistDelta) PcvData->nDistDelta = nDist;
067   
068               j++;
069           }
070   
071           // Armazena o número de vértices, se ele não tiver sido especificado
072           if ((i == 0) && (PcvData->nNumVertices <= 0)) {
073               PcvData->nNumVertices = j;
074               nMaxj = PcvData->nNumVertices;
075           }
076           i++;
077       }
078   
079       // Adiciona o maior custo encontrado ao custo de todas as arestas
080       // Isto obriga a desigualdade triangular, necessária em alguns algoritmos
081       for (i = 0; i < PcvData->nNumVertices; i++) {
082           for (j = 0; j < PcvData->nNumVertices; j++) {
083               if (i != j) PcvData->anDist[i][j] += PcvData->nDistDelta;
084           }
085       }
086   
087       // Fecha o arquivo de entrada
088       fclose(fInput);
089       return 1;
090   }
091   
092   
093   // Imprime o menor caminho encontrado
094   void writeOutput(const TPcvData *PcvData)
095   {
096       int i;
097   
098       // Imprime o caminho descoberto
099       for (i = 0; i < PcvData->nNumVertices; i++) {
100           printf("%d\n", PcvData->anMinCaminho[i]);
101       }
102   }


next up previous
Seguinte: fbruta.h Acima: Códigos-fonte: Solução por força Anterior: inout.h
VilarNt 2003-06-20