从文件中读取数据并在C 中执行矩阵乘法

Reading data from file and performing matrix multiplication in c++

本文关键字:执行 文件 读取 数据      更新时间:2023-10-16

我是C 的新手,并且正在尝试执行从文件中获取数据的矩阵乘法。但是我无法获得乘法部分。有人请帮助我解决这个问题。

在尝试进行2个矩阵的乘法时,但无法获得输出。

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    string line;
    int d[3][3],e[3][3],f[3][3];
    int i=0;
    int x=0;
    int j=0;
    string a[20];
    string b[20];
    string c[20];
    ifstream myfile;
    myfile.open("numeric.txt");
    while(getline (myfile,line))
    {
        if(line!=" ")
        {
            a[x]=line;
            b[x]=line;
            cout<<a[x]<<endl;
            x++;       
        }
    }
    cout<<"first Matrix"<<endl;
    for(i=0;i<3;i++)
    {
        for(j=0;j<=4;j++)
        {
            cout<<a[i][j]<<"";
        }
        cout<<endl;  
    }                              
    cout<<"Second Matrix"<<endl;
    for(i=1;i<4;i++)
    {
        for(j=0;j<6;j++)
        {
            cout<<b[i][j]<<"";
        }
        cout<<endl;
    }
    cout<<"Multiplication"<<endl;
    for(i=0;i<3;i++)
    {
        for(j=0;j<=3;j++)
        {
            c[i][j]=0;
            for(int k=0;k<3;k++)
            {
                c[i][j]+=a[i][k]*b[k][j];
            }
        }
    }
    cout<<"Multiplication Result"<<endl;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            cout<<c[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

这是您写入(固定大小)矩阵的完整示例,以及如何从该文件构建它们:

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main() {
    const size_t matrixSize = 3;
    // 
    ofstream matrixOutput("matrix.txt");
    for (size_t i = 0; i < matrixSize; i++) {
        for (size_t j = 0; j < matrixSize; j++) {
            matrixOutput << j*i << ' ';
        }
        matrixOutput << 'n';
    }
    for (size_t i = 0; i < matrixSize; i++) {
        for (size_t j = 0; j < matrixSize; j++) {
            matrixOutput << j*i * 2 << ' ';
        }
        matrixOutput << 'n';
    }
    matrixOutput.close();
    //
    ifstream matrixData("matrix.txt");
    size_t matrixInput[matrixSize][matrixSize];
    size_t matrixInput2[matrixSize][matrixSize];
    size_t position = 0;
    size_t number = 0;
    while (matrixData >> number) {
        const size_t matrixNumber = size_t(floor(position / (matrixSize*matrixSize)));
        const size_t row = size_t(floor(position / matrixSize)) % matrixSize;
        switch (matrixNumber) {
            case 0:
                matrixInput[row][position % 3] = number; break;
            case 1: 
                matrixInput2[row][position % 3] = number; break;
        }
        position++;
    }
    matrixData.close();
    cout << "Matrices: " << endl;
    for (size_t i = 0; i < matrixSize; i++) {
        for (size_t j = 0; j < matrixSize; j++) {
            cout << matrixInput[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
    for (size_t i = 0; i < matrixSize; i++) {
        for (size_t j = 0; j < matrixSize; j++) {
            cout << matrixInput2[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
    cout << "Matrices multiplication: " << endl;
    for (size_t i = 0; i < matrixSize; i++) {
        for (size_t j = 0; j < matrixSize; j++) {
            cout << matrixInput[i][j] * matrixInput2[i][j] << ' ';
        }
        cout << endl;
    }
    // ...
}

注意:如果愿意,也可以将矩阵大小写入文件,以便以后可以检索并构建自定义大小矩阵。