📜  使用运算符重载的矩阵运算

📅  最后修改于: 2021-09-06 06:28:07             🧑  作者: Mango

先决条件:运算符重载

给定两个 NxN 维度的矩阵mat1[][]mat2[][] ,任务是使用运算符重载执行矩阵运算。

例子:

方法:
要重载+*运算符,我们将创建一个名为 matrix 的类,然后创建一个公共函数来重载运算符。

  • 要重载运算符“+”,请使用原型:
    Return_Type classname :: operator +(Argument list)
    {
        // Function Body
    }
    

    例如:

  • 要重载运算符“-”,请使用原型:
    Return_Type classname :: operator -(Argument list)
    {
        // Function Body
    }
    

    例如:

  • 要重载运算符“*”,请使用原型:
    Return_Type classname :: operator *(Argument list)
    {
        // Function Body
    }
    

下面是上述方法的实现:

C++
// C++ program for the above approach
  
#include "bits/stdc++.h"
#define rows 50
#define cols 50
using namespace std;
  
int N;
  
// Class for Matrix operator overloading
class Matrix {
  
    // For input Matrix
    int arr[rows][cols];
  
public:
    // Function to take input to arr[][]
    void input(vector >& A);
    void display();
  
    // Functions for operator overloading
    void operator+(Matrix x);
    void operator-(Matrix x);
    void operator*(Matrix x);
};
  
// Functions to get input to Matrix
// array arr[][]
void Matrix::input(vector >& A)
{
  
    // Travarse the vector A[][]
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            arr[i][j] = A[i][j];
        }
    }
}
  
// Function to display the element
// of Matrix
void Matrix::display()
{
  
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Print the element
            cout << arr[i][j] << ' ';
        }
        cout << endl;
    }
}
  
// Function for addition of two Matrix
// using operator overloading
void Matrix::operator+(Matrix x)
{
    // To store the sum of Matrices
    int mat[N][N];
  
    // Travarse the Matrix x
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
  
            // Add the corresponding
            // blocks of Matrices
            mat[i][j] = arr[i][j]
                        + x.arr[i][j];
        }
    }
  
    // Display the sum of Matrices
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Print the element
            cout << mat[i][j] << ' ';
        }
        cout << endl;
    }
}
  
// Function for subtraction of two Matrix
// using operator overloading
void Matrix::operator-(Matrix x)
{
    // To store the difference of Matrices
    int mat[N][N];
  
    // Travarse the Matrix x
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Subtract the corresponding
            // blocks of Matrices
            mat[i][j] = arr[i][j]
                        - x.arr[i][j];
        }
    }
  
    // Display the difference of Matrices
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
  
            // Print the element
            cout << mat[i][j] << ' ';
        }
        cout << endl;
    }
}
  
// Function for multiplication of
// two Matrix using operator
// overloading
void Matrix::operator*(Matrix x)
{
    // To store the multiplication
    // of Matrices
    int mat[N][N];
  
    // Travarse the Matrix x
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Initialise current block
            // with value zero
            mat[i][j] = 0;
  
            for (int k = 0; k < N; k++) {
                mat[i][j] += arr[i][k]
                             * (x.arr[k][j]);
            }
        }
    }
  
    // Display the multiplication
    // of Matrices
    for (int i = 0; i < N; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Print the element
            cout << mat[i][j] << ' ';
        }
        cout << endl;
    }
}
  
// Driver Code
int main()
{
  
    // Dimension of Matrix
    N = 3;
  
    vector > arr1
        = { { 1, 2, 3 },
            { 4, 5, 6 },
            { 1, 2, 3 } };
  
    vector > arr2
        = { { 1, 2, 3 },
            { 4, 5, 16 },
            { 1, 2, 3 } };
  
    // Declare Matrices
    Matrix mat1, mat2;
  
    // Take Input to matrix mat1
    mat1.input(arr1);
  
    // Take Input to matrix mat2
    mat2.input(arr2);
  
    // For addition of matrix
    cout << "Addition of two given"
         << " Matrices is : \n";
    mat1 + mat2;
  
    // For substraction of matrix
    cout << "Substraction of two given"
         << " Matrices is : \n";
    mat1 - mat2;
  
    // For multiplication of matrix
    cout << "Multiplication of two"
         << " given Matrices is : \n";
    mat1* mat2;
  
    return 0;
}


输出:
Addition of two given Matrices is : 
2 4 6 
8 10 22 
2 4 6 
Substraction of two given Matrices is : 
0 0 0 
0 0 -10 
0 0 0 
Multiplication of two given Matrices is : 
12 18 44 
30 45 110 
12 18 44

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live