1. 程式人生 > >Matrix Calculation

Matrix Calculation

## Description:

Please complete the class Matrix.

Suppose you have understood the overload operator of C++.

Matrix(string, int, int, int**)

Matrix(const Matrix &)

~Matrix()

bool operator ==(const Matrix& matOther)

bool operator !=(const Matrix& matOther)

void operator +=(const
Matrix& matOther) // addition void operator -=(const Matrix& matOther) // substraction Matrix operator *(const Matrix& matOther) // production, return a new Matrix named "newMat" Note: If the caculation is invalid, print like the following: "invalid addition.\n" "invalid substraction.\n"
"invalid multiplication.\n" and return an empty matrix
.h檔案
#include <iostream>
#include <string.h>
using namespace std;

class Matrix {
    public:
        Matrix() {
            name = "";
            width = height = 0;
            param = NULL;
        }
        Matrix(string, int
, int, int**); Matrix(const Matrix &); ~Matrix(); void setName(string newName) { name = newName; } string getName() { return name; } void print() { cout << name << " = [\n"; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == width-1) cout << param[i][j]; else cout << param[i][j] << ", "; } cout << "\n"; } cout << "]\n"; } // overload operator bool operator ==(const Matrix&); bool operator !=(const Matrix&); void operator +=(const Matrix&); void operator -=(const Matrix&); Matrix operator *(const Matrix&); private: string name; int width, height, **param; };
main()函式

#include <iostream>
#include "Matrix.h"
using namespace std;

int main() {
    int h1, w1, h2, w2, n;
    string name1 = "Mat1";
    string name2 = "Mat2";
    cin >> h1 >> w1;
    int **mat1_arr = new int*[h1];
    for (int i = 0; i < h1; i++) {
        mat1_arr[i] = new int[w1];
        for (int j = 0; j < w1; j++) {
            cin >> mat1_arr[i][j];
        }
    }
    cin >> h2 >> w2;
    int **mat2_arr = new int*[h2];
    for (int i = 0; i < h2; i++) {
        mat2_arr[i] = new int[w2];
        for (int j = 0; j < w2; j++) {
            cin >> mat2_arr[i][j];
        }
    }
    Matrix mat1 = Matrix(name1, h1, w1, mat1_arr);
    mat1.print();
    Matrix mat2 = Matrix(name2, h2, w2, mat2_arr);
    mat2.print();
    cout << "\n(Mat1 += Mat2) addition result:\n";
    mat1 += mat2;
    mat1.print();
    cout << "\n(Mat1 -= Mat2) substraction result:\n";
    mat1 -= mat2;
    mat1.print();
    cout << "\n(Mat1 * Mat2) production result:\n";
    Matrix mat3 = mat1 * mat2;
    if (mat3.getName() != "") mat3.print();
    for (int i = 0; i < h1; i++) {
        delete [] mat1_arr[i];
    }
    delete [] mat1_arr;
    for (int i = 0; i < h2; i++) {
        delete [] mat2_arr[i];
    }
    delete []mat2_arr;
    return 0;
}

解答:
using namespace std;
Matrix::Matrix(string a, int b, int c, int**d)
{
    name=a;
    height=b;
    width=c;
    param =new int*[height];
    for (int i = 0; i < height; i++)
        param[i] = new int[width];
    for(int i=0;i<height;++i)
        for(int j=0;j<width;++j)
            param[i][j]=d[i][j];

}
Matrix::Matrix(const Matrix &a)
{
    this->name=a.name;
    this->height=a.height;
    this->width=a.width;
    param = new int*[height];
    for (int i = 0; i < height; i++) {
        param[i] = new int[width];
    for(int i=0;i<height;++i)
    {
        for(int j=0;j<width;++j)
            param[i][j]=a.param[i][j];
    }
}
}
Matrix::~Matrix()
{
    for (int i = 0; i <height; i++) {
        delete []param[i];
    }
    delete []param;
}

bool Matrix::operator ==(const Matrix&a)
{
    if(width!=a.width||height!=a.height)
        return false;
    else
    {
        for(int i=0;i<height;++i)
            for(int j=0;j<width;++j)
                if(a.param[i][j]!=param[i][j])
                    return false;
    }
    return true;
}
bool Matrix::operator !=(const Matrix&a)
{
    if((*this)==a)
        return false;
    return true;
}
void Matrix::operator +=(const Matrix&a)
{
    if(height!=a.height||width!=a.width)
    {
        cout<<"invalid addition.\n";
        return;
    }
    for(int i=0;i<height;++i)
        for(int j=0;j<width;++j)
            param[i][j]+=a.param[i][j];
}
void Matrix::operator -=(const Matrix&a)
{
    if(height!=a.height||width!=a.width)
    {
        cout<<"invalid substraction.\n";
        return;
    }
    for(int i=0;i<height;++i)
        for(int j=0;j<width;++j)
            param[i][j]-=a.param[i][j];
}
Matrix Matrix::operator *(const Matrix&a)
{

    Matrix temp;
    if(width!=a.height)
    {
        cout<<"invalid multiplication.\n";
        return temp;
    }
    temp.name="newMat";
    temp.height=height;
    temp.width=a.width;
    temp.param =new int*[height];
    for (int i = 0; i < temp.height; i++)
        temp.param[i] = new int[width];
    for(int i=0;i<temp.height;++i)
    {
        for(int j=0;j<temp.width;++j)
        {
            int sum=0;
            for(int k=0;k<width;++k)
                sum+=param[i][k]*a.param[k][j];
            temp.param[i][j]=sum;
        }
    }
    return temp;
}

心得:
(1)new的二維陣列的方法;
(2)