实验流程: 1.实验要求一:将文件分为$\ Matrix.cpp和Matrix.h$ 2.实验要求二:初始化为 //需要构造函数如下: Matrix():a(1),b(0),c(0),d(1) { };
随后以 的顺序赋值等。 class Matrix { friend istream &operator >>(istream &cin, Matrix &ly1); friend ostream &operator <<(ostream &cout,const Matrix &ly1); private : int a; int b; int c; int d; }; ostream &operator <<(ostream &cout,const Matrix &ly1) { cout<<ly1.a<<" " <<ly1.b<<endl<<ly1.c<<" " <<ly1.d<<endl; return cout; }; istream &operator >>(istream &cin, Matrix &ly1) { cin>>ly1.a>>ly1.b>>ly1.c>>ly1.d; return cin; } ostream &operator <<(ostream &cout,const Matrix &ly1) { cout<<ly1.a<<" " <<ly1.b<<endl<<ly1.c<<" " <<ly1.d<<endl; return cout; };
友元函数一,输入流,使用完毕后返回cin的引用,以便实现cin>>a>>n等操作。
istream &operator >>(istream &cin, Matrix &ly1) { cin>>ly1.a>>ly1.b>>ly1.c>>ly1.d; return cin; }
友元函数二,写访问,使用完毕后返回cout的引用,实现cout<<a<<b等操作
3.实验要求三:实现“+”,“-”,“*”的函数重载 perator+(const Matrix &ly1,const Matrix &ly2) { Matrix temp; temp.a=ly2.a+ly1.a; temp.b=ly2.b+ly1.b; temp.c=ly2.c+ly1.c; temp.d=ly2.d+ly1.d; return temp; } Matrix operator -(const Matrix &ly1,const Matrix &ly2) { Matrix temp; temp.a=ly2.a-ly1.a; temp.b=ly2.b-ly1.b; temp.c=ly2.c-ly1.c; temp.d=ly2.d-ly1.d; return temp; } Matrix operator *(const Matrix &ly1,const Matrix &ly2) { Matrix temp; int ly2_empy1=ly2.a; int ly2_empy2=ly2.b; int ly2_empy3=ly2.c; int ly2_empy4=ly2.d; int ly1_empy1=ly1.a; int ly1_empy2=ly1.b; int ly1_empy3=ly1.c; int ly1_empy4=ly1.d; temp.a=ly1.a*ly2.a+ly1.b*ly2.c; temp.b=ly1.a*ly2.b+ly1.b*ly2.d; temp.c=ly1.c*ly2.a+ly1.d*ly2.c; temp.d=ly1.c*ly2.b+ly1.d*ly2.d; return temp; } Matrix operator *(const Matrix &ly1,const int ly2) { Matrix temp; temp.a=ly1.a*ly2; temp.b=ly1.a*ly2; temp.c=ly1.c*ly2; temp.d=ly1.c*ly2; return temp; } perator+(const Matrix &ly1,const Matrix &ly2) { Matrix temp; temp.a=ly2.a+ly1.a; temp.b=ly2.b+ly1.b; temp.c=ly2.c+ly1.c; temp.d=ly2.d+ly1.d; return temp; }
作为需要传入两个变量的重载,开一个临时变量temp,返回它并以此赋值给左值。
Matrix operator *(const Matrix &ly1,const Matrix &ly2) { Matrix temp; int ly2_empy1=ly2.a; int ly2_empy2=ly2.b; int ly2_empy3=ly2.c; int ly2_empy4=ly2.d; int ly1_empy1=ly1.a; int ly1_empy2=ly1.b; int ly1_empy3=ly1.c; int ly1_empy4=ly1.d; temp.a=ly1.a*ly2.a+ly1.b*ly2.c; temp.b=ly1.a*ly2.b+ly1.b*ly2.d; temp.c=ly1.c*ly2.a+ly1.d*ly2.c; temp.d=ly1.c*ly2.b+ly1.d*ly2.d; return temp; }
矩阵的乘法的特殊规则,同样以临时变量为基础实现。
4.实验要求四:实现“+=”,“-=”,“*=”的函数重载 Matrix &operator +=(Matrix &ly1) { this ->a+=ly1.a; this ->b+=ly1.b; this ->c+=ly1.c; this ->d+=ly1.d; return *this ; } Matrix &operator -=(Matrix &ly1) { this ->a-=ly1.a; this ->b-=ly1.b; this ->c-=ly1.c; this ->d-=ly1.d; return *this ; } Matrix &operator *=(Matrix &ly1) { int this_empy1=this ->a; int this_empy2=this ->b; int this_empy3=this ->c; int this_empy4=this ->d; int ly1_empy1=ly1.a; int ly1_empy2=ly1.b; int ly1_empy3=ly1.c; int ly1_empy4=ly1.d; this ->a=this_empy1*ly1_empy1+this_empy2*ly1_empy3; this ->b=this_empy1*ly1_empy2+this_empy2*ly1_empy4; this ->c=this_empy3*ly1_empy1+this_empy4*ly1_empy3; this ->d=this_empy3*ly1_empy2+this_empy4*ly1_empy4; return *this ; } Matrix &operator +=(Matrix &ly1) { this ->a+=ly1.a; this ->b+=ly1.b; this ->c+=ly1.c; this ->d+=ly1.d; return *this ; }
只需要传入一个变量的引用,开在类的内部,无需中间变量,直接赋值给左值
Matrix &operator *=(Matrix &ly1) { int this_empy1=this ->a; int this_empy2=this ->b; int this_empy3=this ->c; int this_empy4=this ->d; int ly1_empy1=ly1.a; int ly1_empy2=ly1.b; int ly1_empy3=ly1.c; int ly1_empy4=ly1.d; this ->a=this_empy1*ly1_empy1+this_empy2*ly1_empy3; this ->b=this_empy1*ly1_empy2+this_empy2*ly1_empy4; this ->c=this_empy3*ly1_empy1+this_empy4*ly1_empy3; this ->d=this_empy3*ly1_empy2+this_empy4*ly1_empy4; return *this ; }
但乘法计算特殊,变量要使用两次以上,故还是需要中间变量。
5.实验要求五:实现关系运算符“==”,“!=”等 bool operator ==(Matrix ly1){ if (this ->a==ly1.a&&this ->b==ly1.b &&this ->c==ly1.c&&this ->d==ly1.d) return true ; else return false ; } bool operator !=(Matrix ly1) { if (this ->a!=ly1.a||this ->b!=ly1.b ||this ->c!=ly1.c||this ->d!=ly1.d) return true ; else return false ; }
只需要传入一个变量的引用,不需要写访问,使用const保证安全性。
实验代码: $\ Matrix.h$ 内容展示: #include <iostream> using namespace std;#ifndef MATRIX_H #define MATRIX_H class Matrix { friend istream &operator >>(istream &cin, Matrix &ly1); friend ostream &operator <<(ostream &cout, const Matrix &ly1); friend Matrix operator +(const Matrix &ly1, const Matrix &ly2); friend Matrix operator -(const Matrix &ly1, const Matrix &ly2); friend Matrix operator *(const Matrix &ly1, const Matrix &ly2); friend Matrix operator *(const Matrix &ly1, int ); public : bool operator ==(Matrix ly1); bool operator !=(Matrix ly1); Matrix (); Matrix &operator +=(Matrix &ly1); Matrix &operator -=(Matrix &ly1); Matrix &operator *=(Matrix &ly1); ~Matrix (); private : int a; int b; int c; int d; }; ostream &operator <<(ostream &cout, const Matrix &ly1); istream &operator >>(istream &cin, Matrix &ly1); Matrix operator +(const Matrix &ly1, const Matrix &ly2); Matrix operator -(const Matrix &ly1, const Matrix &ly2); Matrix operator *(const Matrix &ly1, const Matrix &ly2); Matrix operator *(const Matrix &ly1, const int ly2); #endif
$\ Matrix.cpp$ 内容展示: #include <iostream> #include "Matrix.h" using namespace std;bool Matrix::operator ==(Matrix ly1){ if (this ->a == ly1.a && this ->b == ly1.b && this ->c == ly1.c && this ->d == ly1.d) return true ; else return false ; } bool Matrix::operator !=(Matrix ly1){ if (this ->a != ly1.a || this ->b != ly1.b || this ->c != ly1.c || this ->d != ly1.d) return true ; else return false ; } Matrix::Matrix () : a (1 ), b (0 ), c (0 ), d (1 ){}; Matrix::~Matrix (){}; Matrix& Matrix::operator +=(Matrix &ly1) { this ->a += ly1.a; this ->b += ly1.b; this ->c += ly1.c; this ->d += ly1.d; return *this ; } Matrix& Matrix::operator -=(Matrix &ly1) { this ->a -= ly1.a; this ->b -= ly1.b; this ->c -= ly1.c; this ->d -= ly1.d; return *this ; } Matrix& Matrix::operator *=(Matrix &ly1) { int this_empy1 = this ->a; int this_empy2 = this ->b; int this_empy3 = this ->c; int this_empy4 = this ->d; int ly1_empy1 = ly1.a; int ly1_empy2 = ly1.b; int ly1_empy3 = ly1.c; int ly1_empy4 = ly1.d; this ->a = this_empy1 * ly1_empy1 + this_empy2 * ly1_empy3; this ->b = this_empy1 * ly1_empy2 + this_empy2 * ly1_empy4; this ->c = this_empy3 * ly1_empy1 + this_empy4 * ly1_empy3; this ->d = this_empy3 * ly1_empy2 + this_empy4 * ly1_empy4; return *this ; } ostream &operator <<(ostream &cout, const Matrix &ly1) { cout << ly1.a << " " << ly1.b << endl << ly1.c << " " << ly1.d << endl; return cout; }; istream &operator >>(istream &cin, Matrix &ly1) { cin >> ly1.a >> ly1.b >> ly1.c >> ly1.d; return cin; } Matrix operator +(const Matrix &ly1, const Matrix &ly2) { Matrix temp; temp.a = ly2.a + ly1.a; temp.b = ly2.b + ly1.b; temp.c = ly2.c + ly1.c; temp.d = ly2.d + ly1.d; return temp; } Matrix operator -(const Matrix &ly1, const Matrix &ly2) { Matrix temp; temp.a = ly2.a - ly1.a; temp.b = ly2.b - ly1.b; temp.c = ly2.c - ly1.c; temp.d = ly2.d - ly1.d; return temp; } Matrix operator *(const Matrix &ly1, const Matrix &ly2) { Matrix temp; int ly2_empy1 = ly2.a; int ly2_empy2 = ly2.b; int ly2_empy3 = ly2.c; int ly2_empy4 = ly2.d; int ly1_empy1 = ly1.a; int ly1_empy2 = ly1.b; int ly1_empy3 = ly1.c; int ly1_empy4 = ly1.d; temp.a = ly1.a * ly2.a + ly1.b * ly2.c; temp.b = ly1.a * ly2.b + ly1.b * ly2.d; temp.c = ly1.c * ly2.a + ly1.d * ly2.c; temp.d = ly1.c * ly2.b + ly1.d * ly2.d; return temp; } Matrix operator *(const Matrix &ly1, const int ly2) { Matrix temp; temp.a = ly1.a * ly2; temp.b = ly1.a * ly2; temp.c = ly1.c * ly2; temp.d = ly1.c * ly2; return temp; }
$main.cpp$内容展示: #include <iostream> #include "Maze.h" #include "Maze.cpp" using namespace std;int main () { char fname[64 ]; cout << "Maze File: " ; cin >> fname; if (LoadMaze (fname)) SolveMaze (); return 0 ; }
实验结果 输入你想创建的矩阵数目 1 我们希望数目应至少大于3 3 接下来请用数字序号表示你的矩阵 请输入矩阵元素,依次为a,b,c,d 1 2 3 4 1 2 3 4 请输入矩阵元素,依次为a,b,c,d 2 3 4 5 2 3 4 5 请输入矩阵元素,依次为a,b,c,d 3 4 5 6 3 4 5 6 接下来计算矩阵1+2+3的值 6 9 12 15 接下来计算矩阵1-2-3的值 2 3 4 5 接下来计算矩阵1*2*3的值 95 118 211 262 接下来计算1*(2) 3 3 9 9 接下来计算1+=2 3 5 7 9 接下来计算1-=2 1 2 3 4 接下来计算1*=2 10 13 22 29 接下来判断1是否等于2 0
(开始灌水)
改进: 1.通过在cout<<”|”实现对矩阵更进一步的展现
2.可采用二维矩阵代替a,b,c,d
3.友元太多会破环安全性,可以减少友元的使用量
实验心得: 1.const是尤其重要的,如果缺少,会导致编译器推导失误;
2.
Matrix operator +(Matrix &ly1,Matrix &ly2) { Matrix *temp1=new Matrix; temp1->a=ly2.a+ly1.a; temp1->b=ly2.b+ly1.b; temp1->c=ly2.c+ly1.c; temp1->d=ly2.d+ly1.d; return *temp1; }
也可以使用该方法,但最后需要释放。
3.friend可能有安全隐患,可以减少friend友元的使用量。