您好,登錄后才能下訂單哦!
C++操作符重載
實現(xiàn)效果圖:
實例代碼:
Matrix.h
#pragma once #include "vector" #include "iostream" #define rep(i,n) for(int i=1;i<=n;i++) //宏定義for循環(huán),精簡代碼 using namespace std; class Matrix { public: //基本構(gòu)造函數(shù) Matrix(int Row=0, int Column=0); //拷貝構(gòu)造函數(shù)或者復(fù)制構(gòu)造函數(shù) Matrix(const Matrix& matrix); //賦值操作符重載,必須為成員函數(shù),不然會報錯 Matrix& operator=(const Matrix& matrix); //復(fù)合賦值操作符重載,建議重載為成員函數(shù) Matrix& operator+=(const Matrix& matrix); Matrix& operator*=(const Matrix& matrix); Matrix& operator*=(const float& number); Matrix& operator*=(const int& number); Matrix& operator-=(const Matrix& matrix); Matrix& operator/=(const float& number); float& operator[](const size_t& index); Matrix& operator++();//前綴式自增 Matrix& operator--();//前綴式自減 Matrix operator++(int); //后綴式自增 Matrix operator--(int); //后綴式自減 //算術(shù)和關(guān)系操作符一般為非成員函數(shù),聲明為友元 friend Matrix operator+(const Matrix& matrix1, const Matrix& matrix2); friend Matrix operator-(const Matrix& matrix1, const Matrix& matrix2); friend Matrix operator*(const Matrix& matrix1, const Matrix& matrix2); friend Matrix operator*(const Matrix& matrix1, const float& number); friend Matrix operator*(const Matrix& matrix1, const int& number); friend bool operator==(const Matrix& matrix1, const Matrix& matrix2); friend bool operator!=(const Matrix& matrix1, const Matrix& matrix2); //輸出操作符<<的重載,必須聲明為友元 friend ostream& operator<<(ostream& os, const Matrix&object); //輸入操作符>>重載,必須聲明為友元 friend istream& operator >>(istream& in,Matrix&object); void Display(); ~Matrix(); public: int Row; int Column; vector<vector<float> > data; //二維vector,用于存放矩陣類數(shù)據(jù) };
Matrix.cpp
#include "stdafx.h" #include "Matrix.h" #include "iomanip" //構(gòu)造函數(shù) Matrix::Matrix(int Row/* =0 */, int Column/* =0 */){ this->Row = Row; this->Column = Column; data.resize(Row + 1); //申請行數(shù)為row+1,0號位不用 rep(i, Row) data[i].resize(Column + 1); //申請各行的列數(shù) rep(i, Row) rep(j, Column) data[i][j] = 0; //每個元素初始化為0,方便后面計算 } //打印函數(shù) void Matrix::Display(){ rep(i, Row) { rep(j, Column) cout << setw(8) << data[i][j] << ' '; cout <<endl; } } //拷貝構(gòu)造函數(shù) Matrix::Matrix(const Matrix& matrix){ Row = matrix.Row; Column = matrix.Column; data.resize(Row+1); //申請行數(shù)為row,0號位不用 rep(i, Row) data[i].resize(Column+1); //申請各行的列數(shù) rep(i, Row) rep(j, Column) data[i][j] = matrix.data[i][j]; } //賦值操作符重載 Matrix& Matrix::operator=(const Matrix& matrix){ if (this==&matrix) { return *this; } Row = matrix.Row; Column = matrix.Column; //分配資源 data.resize(Row + 1); //申請行數(shù)為row+1,0號位不用 rep(i, Row) data[i].resize(Column + 1); //申請各行的列數(shù) rep(i, Row) rep(j, Column) data[i][j] = matrix.data[i][j]; //返回本對象的引用 return *this; } //復(fù)合賦值操作符重載 Matrix& Matrix::operator+=(const Matrix& matrix){ if (Row == matrix.Row&&Column == matrix.Column) { rep(i, Row) { rep(j,Column) { data[i][j] += matrix.data[i][j]; } } } return *this; } Matrix& Matrix::operator-=(const Matrix& matrix){ if (Row == matrix.Row&&Column == matrix.Column) { rep(i, Row) { rep(j, Column) { data[i][j] -= matrix.data[i][j]; } } } return *this; } Matrix& Matrix::operator*=(const float& number){ rep(i, Row) { rep(j, Column) { data[i][j] = data[i][j] * number; } } return *this; } Matrix& Matrix::operator*=(const int& number){ rep(i, Row) { rep(j, Column) { data[i][j] = data[i][j] * number; } } return *this; } Matrix& Matrix::operator*=(const Matrix& matrix){ //先保存矩陣的值 Matrix temp(Row, Column); rep(i, temp.Row) { rep(j, temp.Column) { temp.data[i][j] = data[i][j]; } } //改變矩陣的大小和值 Column = matrix.Column; data.clear(); //清除數(shù)據(jù) data.resize(Row+1); //申請行數(shù)為row+1,0號位不用 rep(i, Row) data[i].resize(Column+1); //申請各行的列數(shù) //重新給矩陣賦值 rep(i, temp.Row) { rep(j, matrix.Column) { double sum = 0; rep(k, temp.Column) sum += temp.data[i][k] * matrix.data[k][j]; data[i][j] = sum; //改變矩陣的值 } } return *this; } Matrix& Matrix::operator/=(const float& number){ rep(i, Row) { rep(j, Column) { data[i][j] = data[i][j] / number; } } return *this; } //前綴式自增 Matrix& Matrix::operator++(){ //對每個元素都加1 rep(i, Row) { rep(j, Column) { data[i][j] +=1; } } return *this; } //前綴式自減 Matrix& Matrix::operator--(){ //對每個元素都減1 rep(i, Row) { rep(j, Column) { data[i][j] -= 1; } } return *this; } //后綴式自增 Matrix Matrix::operator++(int){ //拷貝構(gòu)造函數(shù) Matrix ret(*this); //對每個元素都加1 rep(i, Row) { rep(j, Column) { data[i][j] += 1; } } return ret; } //后綴式自減 Matrix Matrix::operator--(int){ //拷貝構(gòu)造函數(shù) Matrix ret(*this); //對每個元素都減1 rep(i, Row) { rep(j, Column) { data[i][j] -= 1; } } return ret; } //析構(gòu)函數(shù) Matrix::~Matrix() { data.clear(); } //加法操作符重載 Matrix operator+(const Matrix& matrix1, const Matrix& matrix2){ Matrix temp(matrix1.Row, matrix1.Column); if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column) { rep(i, matrix1.Row) { rep(j, matrix2.Column) { temp.data[i][j] = matrix1.data[i][j]+matrix2.data[i][j]; } } } return temp; } //減法操作符重載 Matrix operator-(const Matrix& matrix1, const Matrix& matrix2){ Matrix temp(matrix1.Row, matrix1.Column); if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column) { rep(i, matrix1.Row) { rep(j, matrix2.Column) { temp.data[i][j] = matrix1.data[i][j] - matrix2.data[i][j]; } } } return temp; } //乘法操作符重載 Matrix operator*(const Matrix& matrix1, const Matrix& matrix2){ Matrix temp(matrix1.Row, matrix2.Column); rep(i, temp.Row) { rep(j, temp.Column) { double sum = 0; rep(k, matrix1.Column) sum += matrix1.data[i][k] * matrix2.data[k][j]; temp.data[i][j] = sum; } } return temp; } //乘法操作符重載 Matrix operator*(const Matrix& matrix1, const float& number){ Matrix temp(matrix1.Row, matrix1.Column); rep(i, temp.Row) { rep(j, temp.Column) { temp.data[i][j] = matrix1.data[i][j]* number; } } return temp; } //乘法操作符重載 Matrix operator*(const Matrix& matrix1, const int& number){ Matrix temp(matrix1.Row, matrix1.Column); rep(i, temp.Row) { rep(j, temp.Column) { temp.data[i][j] = matrix1.data[i][j] * number; } } return temp; } //等于關(guān)系操作符重載 bool operator==(const Matrix& matrix1, const Matrix& matrix2){ //只有維數(shù)相等才有可比性 if (matrix1.Row==matrix2.Row&&matrix1.Column==matrix2.Column) { rep(i, matrix1.Row) { rep(j, matrix1.Column) { if (matrix1.data[i][j]!=matrix2.data[i][j]) { return false; } } } return true; } else { return false; } } //不等于關(guān)系操作符重載 bool operator!=(const Matrix& matrix1, const Matrix& matrix2){ //只有維數(shù)相等才有可比性 if (matrix1.Row == matrix2.Row&&matrix1.Column == matrix2.Column) { rep(i, matrix1.Row) { rep(j, matrix1.Column) { if (matrix1.data[i][j] != matrix2.data[i][j]) { return true; } } } return false; } else { return false; } } //輸出操作符重載 ostream& operator<<(ostream& os, const Matrix&object){ rep(i, object.Row) { rep(j, object.Column) os << setw(8) << object.data[i][j] << ' '; os <<endl; } return os; } //輸入操作符重載 istream& operator >>(istream& in, Matrix&object){ rep(i, object.Row) rep(j, object.Column) in >> object.data[i][j]; return in; }
main.c
#include "iostream" #include "Matrix.h" using namespace std; int main(){ int row1, row2, col1, col2; cout << "請輸入第一個矩陣的行和列:\n"; cin >> row1 >> col1; Matrix m1(row1, col1); cout << "請輸入" << row1 << '*' << col1 << "個數(shù):\n"; cin >> m1; cout << "輸出矩陣的值:\n"; cout << m1; cout << "請輸入第二個矩陣的行和列:\n"; cin >> row2 >> col2; Matrix m2(row2, col2); cout << "請輸入" << row2 << '*' << col2 << "個數(shù):\n "; cin >> m2; cout << "輸出矩陣的值:\n"; cout << m2; if (col1 != row2) cout << "這兩個矩陣無法相乘\n"; else { cout << "判斷矩陣m1與m2是否相等:\n"; if (m1==m2) { cout << "m1和m2相等:\n"; } else { cout << "m1和m2不相等:\n"; } cout << "m1拷貝構(gòu)造m3矩陣結(jié)果輸出:\n"; Matrix m3(m1); cout << m3; cout << "m1賦值重載m4矩陣結(jié)果輸出:\n"; Matrix m4(m1.Row,m1.Column); m4 = m1; cout << m4; cout << "m1*m2矩陣相乘輸出m5:\n"; Matrix m5(m1.Row, m2.Column); m5 = m1*m2; cout << m5; cout << "矩陣m1*2輸出m6:\n"; Matrix m6(m1.Row, m1.Column); m6 = m1*2; cout << m6; cout << "矩陣m1*0.5輸出m7:\n"; Matrix m7(m1.Row, m1.Column); m7 = m1 * 0.5; cout << m7; cout << "m1*m2矩陣相乘輸出m1:\n"; m1 *= m2; cout << m1; cout << "m1矩陣前自增輸出\n"; cout << ++m1; cout << "m1矩陣后自增輸出\n"; cout << m1++; cout << "m1矩陣輸出\n"; cout << m1; cout << "m1矩陣前自減輸出\n"; cout << --m1; cout << "m1矩陣后自減輸出\n"; cout << m1--; cout << "m1矩陣輸出\n"; cout << m1; } return 0; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。