您好,登錄后才能下訂單哦!
C++操作符重載
實現效果圖:
實例代碼:
Matrix.h
#pragma once #include "vector" #include "iostream" #define rep(i,n) for(int i=1;i<=n;i++) //宏定義for循環,精簡代碼 using namespace std; class Matrix { public: //基本構造函數 Matrix(int Row=0, int Column=0); //拷貝構造函數或者復制構造函數 Matrix(const Matrix& matrix); //賦值操作符重載,必須為成員函數,不然會報錯 Matrix& operator=(const Matrix& matrix); //復合賦值操作符重載,建議重載為成員函數 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); //后綴式自減 //算術和關系操作符一般為非成員函數,聲明為友元 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,用于存放矩陣類數據 };
Matrix.cpp
#include "stdafx.h" #include "Matrix.h" #include "iomanip" //構造函數 Matrix::Matrix(int Row/* =0 */, int Column/* =0 */){ this->Row = Row; this->Column = Column; data.resize(Row + 1); //申請行數為row+1,0號位不用 rep(i, Row) data[i].resize(Column + 1); //申請各行的列數 rep(i, Row) rep(j, Column) data[i][j] = 0; //每個元素初始化為0,方便后面計算 } //打印函數 void Matrix::Display(){ rep(i, Row) { rep(j, Column) cout << setw(8) << data[i][j] << ' '; cout <<endl; } } //拷貝構造函數 Matrix::Matrix(const Matrix& matrix){ Row = matrix.Row; Column = matrix.Column; data.resize(Row+1); //申請行數為row,0號位不用 rep(i, Row) data[i].resize(Column+1); //申請各行的列數 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); //申請行數為row+1,0號位不用 rep(i, Row) data[i].resize(Column + 1); //申請各行的列數 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 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(); //清除數據 data.resize(Row+1); //申請行數為row+1,0號位不用 rep(i, Row) data[i].resize(Column+1); //申請各行的列數 //重新給矩陣賦值 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){ //拷貝構造函數 Matrix ret(*this); //對每個元素都加1 rep(i, Row) { rep(j, Column) { data[i][j] += 1; } } return ret; } //后綴式自減 Matrix Matrix::operator--(int){ //拷貝構造函數 Matrix ret(*this); //對每個元素都減1 rep(i, Row) { rep(j, Column) { data[i][j] -= 1; } } return ret; } //析構函數 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; } //等于關系操作符重載 bool operator==(const Matrix& matrix1, const Matrix& matrix2){ //只有維數相等才有可比性 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; } } //不等于關系操作符重載 bool operator!=(const Matrix& matrix1, const Matrix& matrix2){ //只有維數相等才有可比性 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 << "個數:\n"; cin >> m1; cout << "輸出矩陣的值:\n"; cout << m1; cout << "請輸入第二個矩陣的行和列:\n"; cin >> row2 >> col2; Matrix m2(row2, col2); cout << "請輸入" << row2 << '*' << col2 << "個數:\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拷貝構造m3矩陣結果輸出:\n"; Matrix m3(m1); cout << m3; cout << "m1賦值重載m4矩陣結果輸出:\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; }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。