Design a class, Matrix5x5, to represent 5 by 5 matrixes.
This class should have the following member functions:
double get(int, int)
void set(int, int, double)
Matrix5x5 operator + (Matrix5x5 &, Matrix5x5 &)
EXAMPLE INPUT
1
2
3
4
EXAMPLE OUTPUT
4 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 4 0
0 0 0 0 2
主程序 (不能修改)
#include "source.cpp"
#include <iostream>
using namespace std;
void print(Matrix5x5 & matrix) {
for (int row = 0; row < 5; ++ row) {
for (int col = 0; col < 5; ++ col) {
cout << matrix.get(row, col) << ' ';
}
cout << endl;
}
}
int main() {
Matrix5x5 m1, m2, m3;
double value;
cin >> value;
m1.set(0, 0, value);
cin >> value;
m1.set(4, 4, value);
cin >> value;
m2.set(0, 0, value);
cin >> value;
m2.set(3, 3, value);
m3 = m1 + m2;
print(m3);
}
我的答案
class Matrix5x5{
public:
double number[5][5];
Matrix5x5(){
for (int row = 0; row < 5; ++ row) {
for (int col = 0; col < 5; ++ col) {
number[row][col]=0;
}
}
}
double get(int row, int col){
return this->number[row][col];
}
void set(int a, int b, double c){
//a,b为坐标,c为指定坐标赋值
this->number[a][b]=c;
}
Matrix5x5 operator+(const Matrix5x5& n){
Matrix5x5 m;
for (int row = 0; row < 5; ++ row) {
for (int col = 0; col < 5; ++ col) {
m.number[row][col]=this->number[row][col]+n.number[row][col];
}
}
return m;
}
};