Buongiorno,
sono una studentessa universitaria che deve svolgere un esercizio, sono alle prime armi e avrei bisogno di una mano con il seguente codice. L'obiettivo è quello di sommare, sottrarre o moltiplicare due matrici contenti in file separati. Non so per quale ragione il codice da me create si interrompe quando mi chiede di inserire il nome del file perchè non riesce a leggerlo. Vi ringrazio in anticipo.
P.S. è una delle prime volte che programmo perciò scusatemi in anticipo se non comprendo al volo i passaggi
#include <stdio.h>
#include <stdbool.h>
#define R_MAX 10
#define C_MAX 10
#define LEN_STR 20
typedef struct
{
int r;
int c;
int m[R_MAX][C_MAX];
} matrix_t;
int read_matrix(matrix_t *, char *);
void print_matrix(matrix_t, char *);
void sum_matrix(matrix_t, matrix_t, matrix_t *);
void sub_matrix(matrix_t, matrix_t, matrix_t *);
void product_matrix(matrix_t, matrix_t, matrix_t*);
int main()
{
int tmp;
char op;
matrix_t A, B, D;
char str[LEN_STR];
printf("What kind of operation you want to perform?\n");
printf("e -> Exit\n");
printf("+ -> Sum of matrices\n");
printf("- -> Sub of matrices\n");
printf("* -> Product between matrices\n");
scanf("%c",&op); gets(str);
while (op != '+' && op !='-' && op !='*' && op !='e')
{
printf("Operation not found, repeat please\n");
printf("\n");
printf("What kind of operation you want to perform?\n");
printf("e -> Exit\n");
printf("+ -> Sum of matrices\n");
printf("- -> Sub of matrices\n");
printf("* -> Product between matrices\n");
scanf("%c",&op);
gets(str);
continue;
}
if (op == 'e')
return 0;
while (true)
{
while (true)
{
printf("What is the file name of the first matrix?\n");
gets(str);
tmp = read_matrix(&A,str);
}
}
if (tmp < 0)
return 0;
while (true)
{
printf("What is the file name of the second matrix?\n");
gets(str);
tmp = read_matrix(&B,str);
} if (tmp < 0)
return 0;
if (A.r != B.r)
{
if (A.c != B.c)
printf("Please, the two vectors should have the same number of row>
return 0;
}
switch (op)
{
case 1:
sum_matrix(A, B, &D);
printf("Where you want to put the result?\n");
gets(str);
print_matrix(D,str);
break;
case 2:
sub_matrix(A, B, &D);
printf("Where you want to put the result?\n");
gets(str);
print_matrix(D,str);
break;
case 3:
product_matrix(A, B, &D);
printf("Where you want to put the result?\n");
gets(str);
print_matrix(D,str);
break;
}
}
int read_matrix(matrix_t* m, char* name)
{
int tmp,i,j;
FILE *fin = NULL;
fin = fopen(name,"r"); if (fin == NULL)
{
printf("I was not able to read file %s!",name);
return -1;
}
fscanf(fin,"%d",&tmp);
if (tmp >= R_MAX)
{
if (tmp>= C_MAX )
printf("There are too many rows or columns!");
return -1;
}
m->r = tmp;
m->c = tmp;
for (i = 0; i < m->r; i++)
{
for (j=0; m->c; j++)
fscanf(fin,"%d",&(m->m[i][j]));
fclose(fin);
return 0;
}
}
void print_matrix(matrix_t m, char* name)
{
int i, j;
FILE *fout = NULL;
fout = fopen(name,"w");
fprintf(fout,"%d\n", m.r);
for (i = 0; i < (m.r); i++)
{
for (j=0; j<(m.c); j++)
fprintf(fout,"%.2d\n",m.m[i][j]);
fclose(fout);
}}
void sum_matrix(matrix_t A, matrix_t B, matrix_t *D)
{
int i,j;
D->r = A.r;
D->c = A.c;
for (i = 0; i < A.r; i++)
{
for (j=0;j < A.c; j++)
D->m[i][j] = A.m[i][j] + B.m[i][j];
}
}
void sub_matrix(matrix_t A, matrix_t B, matrix_t *D)
{
int i,j;
D-> r = A.r;
D-> c = A.c;
for (i = 0; i < A.r; i++)
{
for (j=0; j<A.c; j++)
D->m[i][j] = A.m[i][j] - B.m[i][j];
}
}
void product_matrix(matrix_t A, matrix_t B, matrix_t *D)
{
int i,j;
D-> r = A.r;
D-> c = B.c;
if (A.c != B.r)
{
printf("Number of rows of the first matrix must be egual to the number of columns of the second one\n");
}
for (i = 0; i < A.r; i++)
{
for (j=0; j < B.c; j++)
D->m[i][j] = A.m[i][j] * B.m[i][j];
}
}