A matrix in linear algebra can be represented as a two-dimensional array of row * column, reduced to a number when both row and column are 1, a row vector when row is 1, and a column vector when column is 1. Create an integer matrix class matrix with private data members as follows:

int row;
int column;
int **mat;
Copy the code

The integer matrix class matrix constructor is established. Create an operator overload of * (multiplier) to multiply the two input matrices; Create output function void display(), align the output of integer matrix by row and row, format the output statement as follows:

cout<<setw(10)<<mat[i][j]; # include <iomanip>Copy the code

The main function defines three integer matrix class objects m1, m2, m3. ### input format: input two matrices, respectively, m1 and m2. Enter two integers r c in the first row, giving the number of rows and columns of the matrix respectively. Then enter r row, c integers in each row of the integer matrix, corresponding to c column elements in the current row. After the integer matrix is aligned by row and row (width 10), the output determines whether M1 and M2 can perform matrix multiplication. If possible, after m3=m1*m2 operation, call the display function to output M3. If it does not run properly, run “Invalid Matrix x!”. Tip: Input or output integer matrix, ensure that row>=1 and column>=1.

Example Input:

4 5 1 0 0 0 5 0 2 0 0 0 0 0 3 0 0 0 0 4 0 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 8 9 5 6 7 8 9 5 6 7 8 9 5 6 7 8 9 there is no blank line at the endCopy the code

Example output:

26 32 38 44 50 4 6 8 10 12 9 12 15 18 21 16 20 24 32 36 There is no blank line at the endCopy the code

Code:

#include<iostream>
#include <iomanip>
using namespace std;
class Matrix
{
public:
    Matrix(int.int);//1. Constructor
    ~Matrix(a);//2. Destructor
    friend istream& operator>>(istream& is, Matrix& a);/ / 3. Overloading > >
    int getRow(a) { return row; }/ / 4
    int getColum(a) { return column; }/ / 5
    friend Matrix operator*(Matrix&, Matrix&);/ / 6. Overloaded *
    Matrix(const Matrix&);//7. Copy constructor
    void display(a);/ / 8. The output
private:
    int row;
    int column;
    int** mat;
};
Matrix::Matrix(int r, int c)//1. Constructor
{
    row = r;
    column = c;
    mat = new int* [r + 2];// There is a test point that can't pass without opening it up
    for (int i = 0; i < row + 2; i++)/ / close test
    {
        mat[i] = new int[c + 2];
    }
}
Matrix::~Matrix(a)//2. Destructor
{
    for (int i = 0; i < row + 2; i++)
        delete[]mat[i];
    delete[]mat;
}
istream& operator>>(istream& is, Matrix& a) {/ / 3. Overloading > >
    for (int i = 0; i < a.row; i++)
    {
        for (int j = 0; j < a.column; j++)
        {
            is >> a.mat[i][j];
        }

    }
}
Matrix operator*(Matrix& a, Matrix& b) {/ / 6. Overloaded *
    int i, j, k, x;
    if (a.row == 1 && a.column == 1)// number times matrix
    {
        Matrix p(b.row, b.column);
        for (i = 0; i < b.row; i++) {
            for (j = 0; j < b.column; j++) {
                p.mat[i][j] = a.mat[0] [0] * b.mat[i][j]; }}return p;
    }
    else// There are no test points for the matrix times the number
    {// Common matrix * matrix
        Matrix p(a.row, b.column);
        for (i = 0; i < a.row; i++)
        {
            for (j = 0; j < b.column; j++)
            {
                x = 0;
                for (k = 0; k < a.column; k++) { x += a.mat[i][k] * b.mat[k][j]; } p.mat[i][j] = x; }}return p;
    }
}
Matrix::Matrix(const Matrix& p) {//7. Copy constructor
    this->row = p.row;
    this->column = p.column;
    this->mat = new int* [p.row + 2];
    int i, j;
    for (i = 0; i < p.row + 2; i++)
    {
        this->mat[i] = new int[p.column + 2];
        for (j = 0; j < p.column; j++)
        {
            this->mat[i][j] = p.mat[i][j]; }}}void Matrix::display(a) {/ / 8. The output
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
        {
            cout << setw(10) << mat[i][j]; } cout << endl; }}int main(a)
{
    int a, b, i, j;
    cin >> a >> b;
    Matrix x(a, b);
    cin >> x;// Let's reload
    cin >> a >> b;
    Matrix y(a, b);
    cin >> y;
    if (x.getColum() == y.getRow() || x.getColum() = =1 && x.getRow() = =1 || y.getRow() = =1 && y.getRow() = =1) 
    {
        Matrix z = x * y;
        z.display(a); }else 
    {
        cout << "Invalid Matrix multiplication!" << endl;
    }
    return 0;
}
Copy the code

Submission Results: