C++ 使用 Visual Studio 2010 实现矩阵加法、转置和乘法运算

以下是使用 Visual Studio 2010 中的控制台应用程序实现矩阵的加法运算、转置和乘法运算的示例代码:

#include<iostream>
using namespace std;

const int MAX_SIZE = 100;

class Matrix {
private:
    int rows;
    int cols;
    int data[MAX_SIZE][MAX_SIZE];
public:
    Matrix(int r, int c) {
        rows = r;
        cols = c;
    }

    void inputMatrix() {
        cout << 'Enter matrix elements:' << endl;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                cin >> data[i][j];
            }
        }
    }

    void displayMatrix() {
        cout << 'Matrix:' << endl;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                cout << data[i][j] << ' '; 
            }
            cout << endl;
        }
    }

    Matrix addMatrix(Matrix mat) {
        Matrix result(rows, cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result.data[i][j] = data[i][j] + mat.data[i][j];
            }
        }
        return result;
    }

    Matrix transposeMatrix() {
        Matrix result(cols, rows);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                result.data[j][i] = data[i][j];
            }
        }
        return result;
    }

    Matrix multiplyMatrix(Matrix mat) {
        if (cols != mat.rows) {
            cout << 'Cannot perform multiplication. Invalid dimensions.' << endl;
            return Matrix(0, 0);
        }

        Matrix result(rows, mat.cols);
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < mat.cols; j++) {
                result.data[i][j] = 0;
                for (int k = 0; k < cols; k++) {
                    result.data[i][j] += data[i][k] * mat.data[k][j];
                }
            }
        }
        return result;
    }
};

int main() {
    int r, c;
    cout << 'Enter number of rows: ';
    cin >> r;
    cout << 'Enter number of columns: ';
    cin >> c;

    Matrix m1(r, c);
    m1.inputMatrix();

    cout << 'Enter number of rows for second matrix: ';
    cin >> r;
    cout << 'Enter number of columns for second matrix: ';
    cin >> c;

    Matrix m2(r, c);
    m2.inputMatrix();

    cout << endl;

    Matrix sum = m1.addMatrix(m2);
    cout << 'Addition of matrices:' << endl;
    sum.displayMatrix();
    cout << endl;

    Matrix transpose = m1.transposeMatrix();
    cout << 'Transpose of first matrix:' << endl;
    transpose.displayMatrix();
    cout << endl;

    Matrix product = m1.multiplyMatrix(m2);
    cout << 'Multiplication of matrices:' << endl;
    product.displayMatrix();

    return 0;
}

该程序首先定义了一个 Matrix 类,其中包含了矩阵的行数、列数和数据。类中的成员函数包括输入矩阵、显示矩阵、矩阵加法、矩阵转置和矩阵乘法。

在主函数中,用户需要输入第一个矩阵的行数和列数,然后输入矩阵元素。接着,用户需要输入第二个矩阵的行数和列数,以及矩阵元素。程序会根据用户输入的数据创建两个矩阵对象 m1 和 m2。

接下来,程序会使用 m1 和 m2 对象进行矩阵加法、转置和乘法运算,并将结果存储在对应的矩阵对象中。最后,程序会显示矩阵加法、转置和乘法的结果。

注意:在进行矩阵乘法运算时,要确保第一个矩阵的列数与第二个矩阵的行数相等,否则无法进行乘法运算。如果维度不匹配,程序会输出错误信息。

C++ 使用 Visual Studio 2010 实现矩阵加法、转置和乘法运算

原文地址: https://www.cveoy.top/t/topic/pjuD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录