1

我有一项任务是从文件中读取数字以形成矩阵。每行的前两个整数是行和列,然后剩下的整数是矩阵中的数据。

2 2 1 2 3 4

好像

1 2
3 4

我能够使用以下方法成功加载一个矩阵:

void RdMatrix(FILE *file, int (*matrix)[MAXSIZE][MAXSIZE], int *row, int *column)
{
    int data;

    int matRow = RdRowSize(file);
    int matCol = RdColumnSize(file);
    *row = matRow;
    *column = matCol;
    int i, j;

    for (i = 0; i < matRow; i++)
    {
        for (j = 0; j < matCol; j++)
        {
            fscanf(file, "%d", &data);
            *matrix[i][j] = data;
        }
}

然后我可以打印它

void PrMat(int(*matrix)[MAXSIZE][MAXSIZE], int row, int col)
{   
    int i, j;
    printf("\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d ", *matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

在我的主要功能中,我有两个矩阵A[MAXSIZE][MAXSIZE]and B[MAXSIZE][MAXSIZE],一个rowA = 0, colA = 0;and rowB = 0, colB = 0;。我打电话RdMatrix(fpin, &A, &rowA, &columnA); RdMatrix(fpin, &B, &rowB, &columnB); PrMat(&A, rowA, columnA);

输入看起来像:

2 2 1 2 3 4
2 2 9 8 7 6

然后它打印

1 2 
9 8

9 8 
7 6

什么时候应该打印

1 2
3 4

9 8
7 6

我不允许使用任何库,也无济于事,因为我以后必须在汇编中重新编写它。

编辑:包括代码

#include <stdio.h>
#define MAXSIZE 10
FILE *fpin;

int RdRowSize(FILE *file)
{   
    int row;
    fscanf(file, "%d", &row);
    return row;
}

int RdColumnSize(FILE *file)
{
    int col;
    fscanf(file, "%d", &col);
    return col;
}

void RdMatrix(FILE *file, int (*matrix)[MAXSIZE][MAXSIZE], int *row, int *column)
{
    int data;

    int matRow = RdRowSize(file);
    int matCol = RdColumnSize(file);
    *row = matRow;
    *column = matCol;
    int i, j;

    printf("\n=====================\nLoading Matrix\n=====================\n");
    for (i = 0; i < matRow; i++)
    {
        for (j = 0; j < matCol; j++)
        {
            fscanf(file, "%d", &data);
            *matrix[i][j] = data;
        }
    }
}

void PrMat(int(*matrix)[MAXSIZE][MAXSIZE], int row, int col)
{   
    int i, j;
    printf("\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d ", *matrix[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}


int main(void) 
{

    int RsizeM, CsizeM;                                 /*matrix row size and column size*/
    int A[MAXSIZE][MAXSIZE], B[MAXSIZE][MAXSIZE];       /*the two matrices*/
    int rowA=0, columnA=0, rowB=0, columnB=0;           /* the row and column sizes of A and B */




    /*open input file - file name is hardcoded*/
    fpin = fopen("INA1.txt", "r");                      /* open the file for reading */
    if (fpin == NULL) 
    {
        fprintf(stdout, "Cannot open input file  - Bye\n");
        return(-1);                                 /* if problem, exit program*/
    }
    /*ASSUMPTIONS: the file is not empty and contains has at least 1 set of matrices*/


    /* Add while loop after testing a single iteration */ 


    RdMatrix(fpin, &A, &rowA, &columnA);
    RdMatrix(fpin, &B, &rowB, &columnB);

    PrMat(&A, rowA, columnA);
    PrMat(&B, rowA, columnB);



    fclose(fpin);  /* close the file */

    return (0);
}

然后它必须打开的文件称为 INA1.txt

4

1 回答 1

2

引用元素时,应使用(*matrix)[i][j]而不是*matrix[i][j]. 此外,在打印 B 矩阵时,它应该是rowB,而不是rowA

于 2013-09-23T05:44:39.890 回答