【发布时间】:2019-05-26 09:49:29
【问题描述】:
我有一个包含两个矩阵的文本文件:
1 2 3 4 5 6 1 2 3 * 4 5 6 1 2 3]我希望能够读取两个矩阵的维度和运算类型 * + / -。我想同时检索维度和读取数据。
在我的代码中,get_dim() 函数通过文件中的数据来获取两个矩阵的维度。我不知道是否有办法通过动态内存分配来存储矩阵的值。使用 read_matrix() 函数,我知道我正在再次读取相同数据的矩阵的维度。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define IN 1
#define OUT 0
struct matrix{
int rows;
int cols;
double *data;
};
void f(double x); /*truncate a double */
int get_dim(char *file, int *r, int *col);
void read_matrix(char *file, struct matrix *A, struct matrix *B);
void print_matrix(struct matrix *A);
void multiply(struct matrix *A, struct matrix *B, struct matrix *C);
int main (int argc, char *argv[])
{
int rows[2]= {0,0};
int cols[2]= {0,0};
int operation; /*type of operation 1 for * and 2 for + */
operation = get_dim(argv[1], rows, cols);
struct matrix A;
struct matrix B;
struct matrix C;
A.rows = rows[0];
A.cols = cols[0];
B.rows = rows[1];
B.cols = cols[1];
C.rows = rows[0];
C.cols = cols[1];
A.data = malloc(sizeof(double) * A.rows * A.cols);
B.data = malloc(sizeof(double) * B.rows * B.cols);
C.data = malloc(sizeof(double) * A.rows * B.cols);
read_matrix(argv[1],&A,&B);
print_matrix(&A);
printf("\n*\n");
print_matrix(&B);
printf("\n=\n");
multiply(&A,&B,&C);
print_matrix(&C);
free(A.data);
free(B.data);
free(C.data);
return 0;
}
void read_matrix(char *file, struct matrix *A, struct matrix *B){
int i,j;
FILE *fp;
int c=1;
if((fp = fopen(file, "r")) != NULL ){
for(i=0; i < A->rows; i++)
for(j=0; j < A->cols; j++)
fscanf(fp, "%lf", (A->data + (i * A->cols + j)));
/*skip the character operator line */
while(!isdigit(c))
c=fgetc(fp);
ungetc(c,fp);
for(i=0; i < B->rows; i++)
for(j=0; j < B->cols; j++)
fscanf(fp, "%lf", (B->data + (i * B->cols + j)));
}
fclose(fp);
}
int get_dim(char *file, int *rows, int *cols){
FILE *fp;
double a;
int c =1;
int n = OUT;
int op=0;
if((fp = fopen(file, "r")) == NULL ){
fprintf(stderr, "matrix: I cannot open %s\n",file);
exit(1);
}
while(fscanf(fp,"%lf",&a)){
if(n==OUT)
cols[0]++;
c=fgetc(fp);
if(isdigit(c))
ungetc(c,fp);
else if(c =='\n'){
rows[0]++;
n=IN;
}
else if(c=='*'){
op=1;
break;
}
}
n=OUT;
printf("\n");
while(!isdigit(c))
c=fgetc(fp);
ungetc(c,fp);
while(fscanf(fp,"%lf",&a)){
if(n==OUT)
cols[1]++;
c=fgetc(fp);
if(isdigit(c))
ungetc(c,fp);
else if(c =='\n'){
rows[1]++;
n=IN;
}
else if(c == ']'){
rows[1]++;
break;
}
}
fclose(fp);
return op;
}
void print_matrix(struct matrix *A){
int i,j;
/*printing the matrices*/
double *tmp = A->data;
for(i=0; i < A->rows; i++){
for(j=0; j < A->cols; j++){
f(*(tmp++));
}
printf("\n");
}
}
void multiply(struct matrix *A, struct matrix *B, struct matrix *C)
{
int i, j, k;
/*initialize C to 0*/
for (i=0; i< C->rows; i++){
for (j=0; j < C->cols; j++)
C->data[i * C->cols + j]=0;
}
// Multiplying matrix A and B and storing in C.
for(i = 0; i < A->rows; ++i)
for(j = 0; j < B->cols; ++j)
for(k=0; k < A->cols; ++k)
C->data[i * C->cols + j] += A->data[i * A->cols + k] * B->data[k * B->cols + j];
}
void f(double x)
{
double i,f= modf(x,&i);
if(f<.00001)
printf("%.f ",i);
else printf("%f ",x);
}
【问题讨论】:
-
char c;==>int c; -
我编辑了我的答案以添加一份代码提案
-
请不要在阅读我们的评论时更改/更正您的问题,这会使所有内容无法阅读/无法管理。只需考虑我们的答案
-
我投票决定将此问题作为题外话结束,因为 OP 不断编辑问题,这不是因为不清楚,而是要更改它以消除查看您的评论和还要放一个完全不同版本的代码。我们不是面对一个问题,而是面对几个不同的问题,具体取决于我们阅读的时间。即使我要求停止,OP 也会继续进行更改。这是无法管理的