C++实训--矩阵乘法

作者: 简言之_ | 来源:发表于2019-05-13 19:27 被阅读0次

    重要知识点:C++中路径要用“\\”,不然会报错
    例如: ifstream in ("C:\\Users\\admin\\Desktop\\123.txt")
    矩阵乘法A(m×k) * B(k×n)=C(m×n)

    方法一:使用文件流fstream读入矩阵数据

    #include<iostream>
    #include<fstream>
    using namespace std;
    void multiply(int **A, int **B, int **C, int m, int k, int n)//C[m][n]=A[m][k]*B[k][n]
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int s = 0;
                for (int x = 0; x < k; x++)
                {
                    s += A[i][x] * B[x][j];
                }
                C[i][j] = s;
            }
        }
    }
    void DynamicCreate2Array(int **&p, int m, int n)//动态创建二维数组
    {
        //动态开辟空间
        p = new int*[m]; //开辟行
        for (int i = 0; i < m; i++)
            p[i] = new int[n]; //开辟列
    
    }
    void delete2Array(int **p,int m)//释放开辟的资源
    {
        for (int i = 0; i < m; i++)
            delete[] p[i];
        delete[] p;
    }
    void read(int **M, int row, int col, ifstream &infile) //从文件中读入矩阵初始化数据
    {
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                infile >> M[i][j];
            }
        }
    }
    void show(int **M, int row, int col)//显示矩阵
    {//raw行 col列
    
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                cout << M[i][j]<<" ";
                
            }
            cout << endl;
        }
    }
    
    int main() {
    
        ifstream in("C:\\Users\\admin\\Desktop\\123.txt");//路径必须用“\\”
        int m, k, n;
        cout << "请输入m,k,n:" << endl;
            cin >> m >> k >> n;
        int **A, **B, **C;
        cout << "矩阵A:" << endl;
            DynamicCreate2Array(A, m, k);//开辟二维数组A 
            read(A, m, k, in);//从文件中读入矩阵A初始化数据
            show(A, m, k);//显示矩阵
        cout << "矩阵B:" << endl;
            DynamicCreate2Array(B, k, n);
            read(B, k, n, in);
            show(B, k, n);
        cout << "矩阵乘法C=A*B" << endl;
        cout << "矩阵C:" << endl;
            DynamicCreate2Array(C, m, n);
            multiply(A, B, C, m, k, n);
            show(C, m, n);
        delete2Array(A, m);
        delete2Array(B, k);
        delete2Array(C, m);
        system("pause");
        return 0;
    }
    
    123.txt
    测试结果

    方法二:手动输入矩阵数据

    #include<iostream>
    #include<fstream>
    using namespace std;
    void multiply(int **A, int **B, int **C, int m, int k, int n)//C[m][n]=A[m][k]*B[k][n]
    {
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                int s = 0;
                for (int x = 0; x < k; x++)
                {
                    s += A[i][x] * B[x][j];
                }
                C[i][j] = s;
            }
        }
    }
    void DynamicCreate2Array(int **&p, int m, int n)//动态创建二维数组
    {
        //动态开辟空间
        p = new int*[m]; //开辟行
        for (int i = 0; i < m; i++)
            p[i] = new int[n]; //开辟列
    
    }
    void delete2Array(int **p,int m)//释放开辟的资源
    {
        for (int i = 0; i < m; i++)
            delete[] p[i];
        delete[] p;
    }
    void init(int **M, int row, int col) //初始化数组
    {    //raw行 col列
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                cin >> M[i][j];
            }
        }
    }
    void show(int **M, int row, int col)//显示矩阵
    {//raw行 col列
    
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                cout << M[i][j]<<" ";
                
            }
            cout << endl;
        }
    }
    int main() {
    
        int m, k, n;
        cout << "请输入m,k,n:" << endl;
            cin >> m >> k >> n;
        int **A, **B, **C;
        cout << "矩阵A:" << endl;
            DynamicCreate2Array(A, m, k);
            init(A, m, k);
        cout << "矩阵B:" << endl;
            DynamicCreate2Array(B, k, n);
            init(B, k, n);
        cout << "矩阵乘法C=A*B" << endl;
        cout << "矩阵C:" << endl;
            DynamicCreate2Array(C, m, n);
            multiply(A, B, C, m, k, n);
            show(C, m, n);
        delete2Array(A, m);
        delete2Array(B, k);
        delete2Array(C, m);
        system("pause");
        return 0;
    }
    
    测试结果

    相关文章

      网友评论

        本文标题:C++实训--矩阵乘法

        本文链接:https://www.haomeiwen.com/subject/fenraqtx.html