这是头文件
#include
#ifndef MATRIX_H_
#define MATRIX_H_
class matrix
{
private:
double a[20][20];
int m,n;
public:
matrix();
matrix(const matrix&a);
~matrix();
matrix operator=(const matrix&a);
friend void chengfa(const matrix&a, double b,matrix&c);
friend matrix& operator*(const matrix&a,double b);
friend matrix& operator*(double b,const matrix&a);
friend matrix& operator+(const matrix&a, const matrix&b);
friend matrix& operator-(const matrix&a, const matrix&b);
friend matrix& operator*(const matrix&a, const matrix&b);
friend std::ostream & operator<<(std::ostream&os, matrix&a);
friend std::istream & operator>>(std::istream&is, matrix&a);
};
#endif
这是对应的函数
#include
#include"matrix.h"
using std::cin;
using std::cout;
matrix::matrix()
{
for (int i = 0; i <20; i++)
for (int j = 0; j <20; j++)
a[i][j] = 0;
}
matrix::matrix(const matrix&x)
{
m = x.m;
n = x.n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = x.a[i][i];
}
matrix::~matrix(){}
std::istream & operator>>(std::istream &is, matrix&a)
{
cout << "please input the row of a:\n";
is >> a.m;
cout << "please input the lie of a:\n";
is >> a.n;
cout << "please input the member of a:\n";
for (int i = 0; i < a.m; i++)
for (int j = 0; j < a.n; j++)
is >> a.a[i][j];
return is;
}
std::ostream & operator<<(std::ostream &os, matrix&a)
{
for (int i = 0; i < a.m; i++)
for (int j = 0; j <a.n; j++)
{
os << a.a[i][j] << " ";
if (j == a.n-1)
os << "\n";
}
return os;
}
matrix matrix::operator=(const matrix&x)
{
m = x.m;
n = x.n;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = x.a[i][j];
return*this;
}
matrix& operator*(const matrix&a,double b)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j] * b;
return c;
}
matrix& operator*(double b, const matrix&a)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j] * b;
return c;
}
matrix& operator+(const matrix&a, const matrix&b)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j]+b.a[i][j];
return c;
}
matrix& operator-(const matrix&a, const matrix&b)
{
matrix c;
c = a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
c.a[i][j] = c.a[i][j] - b.a[i][j];
return c;
}
matrix& operator*(const matrix&a, const matrix&b)
{
matrix c;
c = a - a;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
for (int k = 0; k < c.n; k++)
c.a[i][j] += a.a[i][k] * b.a[k][j];
return c;
}
下面是主函数:
#include
#include"matrix.h"
void main()
{
using namespace std;
matrix a,b;
cout << "请输入a;" << endl;
cin >> a;
cout << "a是:" << endl;
cout << a << endl;
b = a*1.5;
cout << "b是:" << endl;
cout << b << endl;
cout << "a*b是:" << endl;
cout << a * b<< endl;
}
输出如下:
请输入a;
please input the row of a:
3
please input the lie of a:
3
please input the member of a:
1 2 3
4 5 6
7 8 9
a是:
1 2 3
4 5 6
7 8 9
b是:
1.5 3 4.5
6 7.5 9
10.5 12 13.5
a*b是:
45 54 63
99 121.5 144
153 189 225
Press any key to continue . . .
可是把重载运算符返回值前面的&去掉,结果就完全错误了
如下:
请输入a;
please input the row of a:
3
please input the lie of a:
3
please input the member of a:
1 2 3
4 5 6
7 8 9
a是:
1 2 3
4 5 6
7 8 9
b是:
1.5 1.5 1.5
7.5 7.5 7.5
13.5 13.5 13.5
a*b是:
57 57 57
124.5 124.5 124.5
192 192 192
Press any key to continue . . .