-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_reload.cpp
136 lines (126 loc) · 2.56 KB
/
matrix_reload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
using namespace std;
class matrix
{
public:
matrix(int, int);
matrix(double**, int, int);
matrix(const matrix&);
~matrix();
friend matrix operator+(const matrix&, const matrix&);
matrix& operator=(const matrix &);
friend ostream& operator<<(ostream& output, const matrix&);
friend istream& operator>>(istream& input, matrix&);
private:
int m, n;
double** a;
};
matrix::matrix(int m, int n)
{
this->m = m; this->n = n;
a = new double*[m];
for (int i = 0; i < m; i++)
{
a[i] = new double[n];
}
}
matrix::matrix(const matrix& M) {
this->m = M.m; this->n = M.n;
this->a = new double*[m];
for (int i = 0; i < m; i++)
{
this->a[i] = new double[n];
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
this->a[i][j] = M.a[i][j];
}
}
matrix::matrix(double** T, int m, int n)
{
this->m = m; this->n = n;
a = T;
}
matrix::~matrix()
{
for (int i = 0; i < m; i++)
{
if (a[i] != NULL)
{
delete[]a[i];
a[i] = NULL;
}
}
delete[]a;
a = NULL;
}
matrix& matrix::operator=(const matrix & A)
{
m = A.m; n = A.n;
for (int i = 0; i < A.m; i++)
{
for (int k = 0; k < A.n; k++)
{
a[i][k] = A.a[i][k];
}
}
return *this;
// TODO: 在此处插入 return 语句
}
matrix operator+(const matrix& A, const matrix& B)
{
matrix C(A.m, A.n);
/*double** T;
T = new double* [A.m];
for (int i = 0; i < A.m; i++)
{
T[i] = new double[A.n];
}*/
for (int i = 0; i < A.m; i++)
{
for (int k = 0; k < A.n; k++)
{
C.a[i][k] = A.a[i][k] + B.a[i][k];
//T[i][k] = A.a[i][k] + B.a[i][k];
}
}
const matrix D = C;
return D; //不能像注释部分这样写,因为一旦出了函数就会调用析构函数,就会导致赋值时出错!!!(找不到指针)
//return matrix(T, A.m, A.n); //此处重载了构造函数
}
ostream& operator<<(ostream& output, const matrix& A)
{
for (int i = 0; i < A.m; i++)
{
for (int k = 0; k < A.n; k++)
{
cout << A.a[i][k] << " ";
}
cout << endl;
}
return output;
// TODO: 在此处插入 return 语句
}
istream& operator>>(istream& input, matrix& A)
{
for (int i = 0; i < A.m; i++)
{
for (int k = 0; k < A.n; k++)
{
cin >> A.a[i][k];
}
}
return input;
// TODO: 在此处插入 return 语句
}
int main()
{
int m, n;
cout << "enter m: "; cin >> m; cout << "enter n: "; cin >> n;
matrix A(m, n); cout << "Please enter this " << m << " x " << n << " matrix" << endl; cin >> A;
matrix B(m, n); cout << "Please enter this " << m << " x " << n << " matrix" << endl; cin >> B;
cout << "C=A+B=" << endl;
matrix C(m, n);
C = (A + B);
cout << (A + B) << endl;
}