#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void LeerDatos(char stNombre[30], long *iDeep, long *iConst, long *iGen, int *iNivel, float ***dAb);
int main (void)
{
char stNombre[30], stLine[300];
long iDeep,iConst,iGen,i,j;
int iNivel;
float **dAb;
printf("Introduzca el nombre del archivo: \n");
scanf("%s",stNombre);
LeerDatos(stNombre, &iDeep, &iConst, &iGen, &iNivel, &dAb);
printf("iDeep: %ld, iConst: %ld, iGen: %ld, iNivel: %d",iDeep,iConst,iGen,iNivel);
for (i=0;i<iConst;i++)
{
for (j=0;j<(iGen)+1;j++)
{
printf("%1f ",dAb[i][j]);
}
printf("\n");
}
return 0;
}
void LeerDatos(char stNombre[30], long *iDeep, long *iConst, long *iGen, int *iNivel, float ***dAb)
{
FILE *fp;
long i,j;
float dTmp;
char stTmp[40];
fp = fopen (stNombre, "r");
if (fp==NULL)
{
fputs ("File error",stderr);
exit (1);
}
fscanf(fp,"%ld",iDeep);
fgets(stTmp,40,fp); // Lectura temporal
fscanf(fp,"%ld",iConst);
fgets(stTmp,40,fp); // Lectura temporal
fscanf(fp,"%ld",iGen);
fgets(stTmp,40,fp); // Lectura temporal
fscanf(fp,"%d",iNivel);
fgets(stTmp,40,fp); // Lectura temporal
*dAb=(float **) malloc(*iConst*sizeof(float *));
for (i=1;i<*iConst;i++) *dAb[i]=malloc(((*iGen)+1)*sizeof(float));
/* -> Llenado de la matriz */
for (i=0;i<*iConst;i++)
{
for (j=0;j<(*iGen)+1;j++)
{
fscanf(fp,"%1f",&dTmp);
*dAb[i][j]=dTmp;
}
printf("\n");
}
fclose (fp);
}
600000 Densidad Grid Integración
17 Número de restricciones
7 Número de generadores
2 Niveles
1 2 0 0 0 0 0 1000
-1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 150
0 -1 0 0 0 0 0 0
0 0 1 0 0 0 0 324
0 0 -1 0 0 0 0 0
0 0 0 1 0 0 0 1250
0 0 0 -1 0 0 0 0
0 0 0 0 1 0 0 396
0 0 0 0 -1 0 0 0
0 0 0 0 0 1 0 276
0 0 0 0 0 -1 0 0
0 0 0 0 0 0 1 224
0 0 0 0 0 0 -1 0
-0.012903 -0.078602 -0.031037 -0.022796 -0.005934 -0.03496 0.017291 -5.55323109732788
-0.024644 -0.039737 -0.030382 -0.026945 -0.014107 -0.030527 0.012554 -11.995211073984
-0.064922 -0.081875 -0.052525 -0.069701 -0.01974 -0.055846 0.085246 -98.410734867
/* -> Definición de librerías */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* -> Declaración de funciones */
void LeerDatos(char stNombre[30], long *iDeep, long *iConst, long *iGen, int *iNivel, float ***dAb);
/* Función principal */
int main (void)
{
/* -> Declaración de variables */
char stNombre[30], stLine[300];
long iDeep,iConst,iGen,i,j;
int iNivel;
float **dAb;
/* -> Solicitud del nombre del archivo al usuario */
printf("Introduzca el nombre del archivo: \n");
scanf("%s",stNombre); /*Cada espacio en la frase es para una variable nueva*/
LeerDatos(stNombre, &iDeep, &iConst, &iGen, &iNivel, &dAb);
printf("iDeep: %ld, iConst: %ld, iGen: %ld, iNivel: %d\n",iDeep,iConst,iGen,iNivel);
for (i=0;i<iConst;i++)
{
for (j=0;j<(iGen)+1;j++)
{
printf("%f ",dAb[i][j]);
}
printf("\n");
}
/* Se libera la memoria de las matrices */
free(dAb[0]);
free(dAb);
dAb=NULL;
return 0;
}
void LeerDatos(char stNombre[30], long *iDeep, long *iConst, long *iGen, int *iNivel, float ***dAb)
{
/* -> Declaración de variables */
FILE *fp;
long i,j;
float dTmp;
char stTmp[40];
/* -> Apertura del archivo y lectura de variables */
fp = fopen (stNombre, "r");
if (fp==NULL)
{
fputs ("File error",stderr);
exit (1);
}
fscanf(fp,"%ld",iDeep); // Lectura de la profundidad
fgets(stTmp,40,fp); // Lectura temporal
fscanf(fp,"%ld",iConst); // Lectura del número de restricciones
fgets(stTmp,40,fp); // Lectura temporal
fscanf(fp,"%ld",iGen); // Lectura del número de generadores
fgets(stTmp,40,fp); // Lectura temporal
fscanf(fp,"%d",iNivel); // Lectura del número de niveles
fgets(stTmp,40,fp); // Lectura temporal
/* -> Dimensionamiento de la matriz */
(*dAb)=malloc(*iConst*sizeof(float *));
(*dAb)[0]=(float *) malloc(*iConst*((*iGen)+1)*sizeof(float));
for (i=1;i<*iConst;i++) (*dAb)[i]=(*dAb)[i-1]+((*iGen)+1);
/* -> Llenado de la matriz */
for (i=0;i<*iConst;i++)
{
for (j=0;j<(*iGen)+1;j++)
{
fscanf(fp,"%f",&dTmp);
(*dAb)[i][j]=dTmp;
}
}
/* -> Cierre del archivo */
fclose (fp);
}