对于我的代码,我必须读取 C 中的两个文件,将这两个文件的内容解释为矩阵,然后将矩阵相加或相乘并将结果打印到屏幕上。给定的只有操作和两个输入文件,矩阵的维度是未知的。对于似乎符合这些函数用法的代码行,我遇到了一些奇怪的段错误。你能看看吗?谢谢。
这是代码:
#include <stdio.h>
#include <stdlib.h>
void matrixadd (long **mat1, long **mat2, int rows, int cols) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("[%d][%d]: %ld ", i, j, mat1[i][j] + mat2[i][j]);
}
printf("\n");
}
}
void matrixmult (long **mat1, long **mat2, int * dim1, int * dim2) {
int i, j, k;
long curnum;
for (i = 0; i < dim1[0]; i++) {
for (j = 0; j < dim2[1]; j++) {
curnum = 0;
for (k = 0; i < dim1[1]; k++) {
curnum += mat1[i][k]*mat2[k][j];
}
printf("[%d][%d]: %ld", i, j, curnum);
}
printf("\n");
}
}
int filetomat (FILE *file, long **mat, int * dim) {
int rows = 0, cols = 0;
long fac = 1l;
long curnum = 0;
char c;
int n_spaces = 0;
while ((c = fgetc(file)) != EOF) {
if (c == '-') {
fac = -1l;
}
if (c >= '0' && c <= '9') {
curnum *= 10;
curnum += (int)(c-'0');
}
if (c == ' ') {
mat[0][n_spaces] = fac*curnum;
n_spaces++;
fac = 1;
curnum = 0;
mat[0] = (long *)realloc(mat[0], sizeof(long)*(n_spaces+1));
if (mat[0] == NULL) {
printf("Allocation error.");
return 1;
}
}
if (c == '\n') { // Füge letzte Zahl in Matrix ein
mat[0][n_spaces] = fac*curnum;
fac = 1l;
curnum = 0;
rows++;
cols = n_spaces;
mat = realloc(mat, 2*sizeof(long *));
if (mat == NULL) {
printf("Allocation error.");
return 1;
}
n_spaces = 0;
break;
}
}
mat[1] = calloc(cols+1, sizeof(long));
if (mat[1] == NULL) {
printf("Allocation error");
return 1;
}
while ((c = fgetc(file)) != EOF) {
if (c == '-') {
fac = -1l;
}
if (c >= '0' && c <= '9') {
curnum *= 10;
curnum = (int)(c-'0');
}
if (c == ' ') {
if (n_spaces > cols) {
printf("n_spaces %d > cols %d, row %d\n", n_spaces, cols, rows);
return 1;
}
mat[rows][n_spaces] = fac*curnum;
n_spaces++;
fac = 1;
curnum = 0;
}
if (c == '\n') {
if (n_spaces != cols) {
printf("n_spaces %d != cols %d, row %d\n", n_spaces, cols, rows);
return 1;
}
mat[rows][n_spaces] = fac*curnum;
fac = 1l;
curnum = 0;
rows++;
n_spaces = 0;
mat = realloc(mat, sizeof(long *)*(rows+1));
mat[rows] = (long *)calloc(cols+1, sizeof(long));
if (mat == NULL || mat[rows] == NULL) {
printf("Allocation error");
}
}
}
dim[0] = rows-1;
dim[1] = cols;
return 0;
}
int main (int argc, char **argv) {
if (argc < 4) {
printf("Not enough arguments");
return 1;
}
int stat1, stat2;
FILE *f1, *f2;
long **mat1, **mat2;
int *dim1 = (int *)calloc(2, sizeof(int));
int *dim2 = (int *)calloc(2, sizeof(int));
if (dim1 == NULL || dim2 == NULL) {
printf("Allocation error");
return 1;
}
mat1 = calloc(1, sizeof(long *));
mat1[0] = (long *)malloc(sizeof(long));
mat2 = calloc(1, sizeof(long *));
mat2[0] = (long *)malloc(sizeof(long));
if (mat1 == NULL || mat1[0] == NULL || mat2 == NULL || mat2[0] == NULL) {
printf("Allocation error");
}
char op;
if (argv[1][0] == '+' || argv[1][0] == 'x') {
op = argv[1][0];
}
else {
printf("Unknown operation");
return 1;
}
f1 = fopen(argv[2], "r");
f2 = fopen(argv[3], "r");
stat1 = filetomat(f1, mat1, dim1);
stat2 = filetomat(f2, mat2, dim2);
fclose(f1);
fclose(f2);
if (stat1 > 0 || stat2 > 0) {
printf("Matrices not valid.");
return 1;
}
if (dim1[1] == dim2[0] && op == 'x') {
matrixmult(mat1, mat2, dim1, dim2);
}
else if (dim1[0] == dim2[0] && dim1[1] == dim2[1] && op == '+' ) {
matrixadd(mat1, mat2, dim1[0]+1, dim2[0]+1);
}
else {
printf("Operation not defined.");
return 1;
}
return 0;
}
这是输入文件内容的示例:
710562522 1048607709 -380295227
-585472920 -710044615 -44110201
-1057241338 598692442 291232156