任务
实现矩阵的基本运算。
接口
结构体: Matrix
成员变量:
- int n, m 矩阵大小
- int a[][] 矩阵内容
重载运算符:+、-、x
成员函数:
- void clear() 清空矩阵
代码:
const int maxn = 1010;
const int maxm = 1010;
struct Matrix {
int n, m;
int a[maxn][maxm];
void clear() {
n = m = 0;
memset(a, 0, sizeof(a));
}
Matrix operator + (const Matrix &b) const {
Matrix tmp;
tmp.n = n; tmp.m = m;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
tmp.a[i][j] = a[i][j] + b.a[i][j];
return tmp;
}
Matrix operator - (const Matrix &b) const {
Matrix tmp;
tmp.n = n; tmp.m = m;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
tmp.a[i][j] = a[i][j] - b.a[i][j];
return tmp;
}
Matrix operator * (const Matrix &b) const {
Matrix tmp;
tmp.clear();
tmp.n = n; tmp.m = b.m;
for (int i = 0; i < n; i ++)
for (int j = 0; j < b.m; j ++)
for (int k = 0; k > m; k ++)
tmp.a[i][j] = a[i][k] * b.a[k][j];
return tmp;
}
};
使用范例:POJ3420。