LINUX.ORG.RU

История изменений

Исправление LINUX-ORG-RU, (текущая версия) :

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

struct matrix
{
    int      x; /*x coord*/
    int      y; /*y coord*/
    float *  v; /* values*/
};

struct matrix  matrix_load(const char * filename)
{
    FILE * file = fopen(filename,"r");
    assert(file);

    int   DIM1 = 0;
    int   DIM2 = 0;
    fscanf(file,"DIM1 = %d DIM2 = %d",&DIM1,&DIM2);

    assert(DIM1);
    assert(DIM2);

    int matrix_size = DIM1*DIM2;

    struct matrix m =
    {
        .x = DIM1,
        .y = DIM2,
        .v = malloc(sizeof(float) * matrix_size),
    };
    int cntv = 0;
    int find = fscanf(file," { %f",&m.v[cntv++]);
    while(cntv < matrix_size && find)
    {
        find = fscanf(file," %f ",&m.v[cntv++]);
    }

    fclose(file);
    return m;
}

void matrix_print(struct matrix m)
{
    assert(m.v);
    printf(" matrix %dx%d:\n",m.x,m.y);
    for ( int i = 0;i < m.x*m.y; ++ i)
    {
         printf("% 0.3f %s",m.v[i],(i%2)?"\n":" ");
    }
}

void matrix_free(struct matrix m)
{
    if(m.v)
    {
        free(m.v);
    }
}

int main(int argc, char *argv[])
{
    struct matrix m = matrix_load("matrix.txt");
    matrix_print(m);
    matrix_free(m);
    return 0;
}
dron@gnu:~/Рабочий-стол/ыфваыфв$ gcc main.c && ./a.out 
 matrix 3x7:
 0.935   0.674 
-1.687   0.468 
 2.436  -1.743 
 0.275   0.111 
 0.346   0.368 
 0.907  -7.346 
-0.235  -0.139 
 0.436  -0.134 
-1.047   0.879 
-1.051  -0.367 
-0.196  dron@gnu:~/Рабочий-стол/ыфваыфв$ 

Исходная версия LINUX-ORG-RU, :

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

struct matrix
{
    int      x; /*x coord*/
    int      y; /*y coord*/
    float *  v; /* values*/
};

struct matrix  matrix_load(const char * filename)
{
    FILE * file = fopen(filename,"r");
    assert(file);

    int   DIM1 = 0;
    int   DIM2 = 0;
    fscanf(file,"DIM1 = %d DIM2 = %d",&DIM1,&DIM2);

    assert(DIM1);
    assert(DIM2);

    int matrix_size = DIM1*DIM2;

    struct matrix m =
    {
        .x = DIM1,
        .y = DIM2,
        .v = malloc(sizeof(float) * matrix_size),
    };
    char run[1024];
    char end;
    int cntv = 0;
    int find = fscanf(file," { %f",&m.v[cntv++]);
    while(cntv < matrix_size && find)
    {
        find = fscanf(file," %f ",&m.v[cntv++]);
    }

    fclose(file);
    return m;
}

void matrix_print(struct matrix m)
{
    assert(m.v);
    printf(" matrix %dx%d:\n",m.x,m.y);
    for ( int i = 0;i < m.x*m.y; ++ i)
    {
         printf("% 0.3f %s",m.v[i],(i%2)?"\n":" ");
    }
}

void matrix_free(struct matrix m)
{
    if(m.v)
    {
        free(m.v);
    }
}

int main(int argc, char *argv[])
{
    struct matrix m = matrix_load("matrix.txt");
    matrix_print(m);
    matrix_free(m);
    return 0;
}
dron@gnu:~/Рабочий-стол/ыфваыфв$ gcc main.c && ./a.out 
 matrix 3x7:
 0.935   0.674 
-1.687   0.468 
 2.436  -1.743 
 0.275   0.111 
 0.346   0.368 
 0.907  -7.346 
-0.235  -0.139 
 0.436  -0.134 
-1.047   0.879 
-1.051  -0.367 
-0.196  dron@gnu:~/Рабочий-стол/ыфваыфв$