c++编程问题,请高手解决....谢谢!

来源:百度知道 编辑:UC知道 时间:2024/06/15 17:04:26
编写求矩阵[B] n×s和[C] c×m相乘的子函数。其中[B]和[C]中各元素的值须由文件中读入。

1.首先两个矩阵相乘,满足第一个矩阵的列数等于第二个矩阵的行数。所以要先做判断。
2.文件的每一行放一个数据,然后将得到的每一行存放的数据读到两个二维数组中。
3.根据矩阵运算的计算法则计算,将结果放在一个新矩阵中。显示出来。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
int B[2][3];
int row = 2, col = 3;
ifstream in("data.txt");
string str;
int i = 0, j = 0;
while( getline(in,str) )
{
B[i][j++] = atoi(str.c_str());
if(col == j)
{
if( i == row - 1)
break;
i++;
j = 0;
}
}
int C[3][2];
row = 3, col = 2;
i = 0, j = 0;
while( getline(in,str) )
{
C[i][j++] = atoi(str.c_str());
if(col == j)
{
if( i == row - 1)
break;
i++;
j = 0;
}
}
int D[2][2];
int k;
for( i = 0; i < 2; i++ )
for( j = 0; j < 2; j++ )
{