Design a class Matrix that has the following private member variables:
int rows
int columns
double * values
Besides, it has the following public object functions:
A constructor Matrix(int rows, int column, double values[]), which initializes all elements in the matrix to the given values. Note that the given values are in one-dimensional, you need to fill then into the two-dimensional matrix correctly.
A copy constructor Matrix(const Matrix & matrix2).
A destructor.
A print function which prints each row of elements in a single line, with each element preceded with 4 spaces.
An operator Matrix Matrix::operator * (const Matrix & matrix2) const.
An operator Matrix Matrix::operator * (double value) const.
EXAMPLE INPUT
4 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
5 4
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
8
EXAMPLE OUTPUT
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
20 19 18 17
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
140 125 110 95
440 400 360 320
740 675 610 545
1040 950 860 770
8 16 24 32 40
48 56 64 72 80
88 96 104 112 120
128 136 144 152 160
主程序 (不能修改)
#include "source.cpp"
Matrix read() {
int rows;
int columns;
double values[1000];
cin >> rows >> columns;
for (int i = 0; i < rows * columns; ++ i) {
cin >> values[i];
}
Matrix matrix(rows, columns, values);
return matrix;
}
int main() {
Matrix matrix1 = read(); // calls copy constructor
Matrix matrix2 = read();
double value;
cin >> value;
matrix1.print();
cout << endl;
matrix2.print();
cout << endl;
Matrix matrix3 = matrix1 * matrix2;
matrix3.print();
cout << endl;
Matrix matrix4 = matrix1 * value;
matrix4.print();
cout << endl;
}
我的答案
#include <iostream>
using namespace std;
class Matrix{
private:
int rows;
int columns;
double *addr;
void _init(int rows,int columns,double values[]){
this->rows=rows;
this->columns=columns;
this->addr=new double[rows*columns];
for (int i = 0; i < rows*columns ; ++ i) {
this->addr[i]=(values==NULL ? 0: values[i]);
}
}
public:
//构造函数1
Matrix(int rows,int columns){
this->_init(rows,columns,NULL);
}
//构造函数2
Matrix(int rows,int columns,double values[]){
this->_init(rows,columns,values);
}
//释构函数
Matrix(const Matrix & m){
this->_init(m.rows,m.columns,m.addr);
}
//=
Matrix & operator = (const Matrix & m){
delete [] this->addr;
this->_init(m.rows,m.columns,m.addr);
return *this;
}
//析构函数
~Matrix(){
delete [] this->addr;
}
double get(int row, int col) const{
int index = (row-1)* this->columns + (col-1);
return this->addr[index];
}
void set(int row, int col,double val){
int index = (row-1)* this->columns + col-1;
this->addr[index] = val;
}
void print() const {
for (int i = 0; i < this->rows; ++ i) {
for (int j = 0; j < this->columns; ++ j) {
cout << " " << this->get(i+1,j+1);
}
cout<<endl;
}
}
Matrix operator * (const Matrix & matrix2) const{
Matrix m(this->rows,this->rows, NULL);
for (int i = 0; i < this->rows ; ++ i) {
for (int j = 0; j < this->rows ; ++ j) {
int temp=0;
for (int k = 0; k < this-> columns ; ++ k){
temp=temp + this->get(i+1,k+1)*matrix2.get(k+1,j+1);
}
m.set(i+1,j+1,temp);
}
}
return m;
}
Matrix operator * (double val) const{
Matrix m(this->rows,this->columns, NULL);
for (int i = 0; i < this->rows ; ++ i) {
for (int j = 0; j < this->columns ; ++ j) {
m.set(i+1,j+1,this->get(i+1,j+1)*val);
}
}
return m;
}
};
#include
using namespace std;
class Matrix
{
private:
int rows;
int columns;
double * data;
void _init(int rows, int columns, double values[]) {
this->rows = rows;
this->columns = columns;
this->data = new double[rows * columns];
for (int i = 0; i < rows * columns; ++ i)
this->data[i] = (values == NULL ? 0 : values[i]);
}
int _index(int row, int column) const {
return (row - 1) * this->columns + (column - 1);
}
public:
Matrix(int rows, int columns) {
this->_init(rows, columns, NULL);
}
Matrix(int rows, int columns, double values[]) {
this->_init(rows, columns, values);
}
Matrix(const Matrix & matrix) {
this->_init(matrix.rows, matrix.columns, matrix.data);
}
~Matrix() {
delete [] this->data;
}
void print() const {
for (int i = 0; i < this->rows; ++ i) {
for (int j = 0; j < this->columns; ++ j) {
cout _index(row, column)] = value;
}
double get(int row, int column) const {
return this->data[this->_index(row, column)];
}
Matrix operator * (const Matrix & matrix2) const{
Matrix m3(rows,columns,NULL);
for (int i = 0; i < rows; ++ i){
for(int j=0;jdata[i]*value;
}
return m3;
}
};
#include
using namespace std;
class Matrix
{
private:
int rows;
int columns;
double * data;
void _init(int rows, int columns, double values[]) {
this->rows = rows;
this->columns = columns;
this->data = new double[rows * columns];
for (int i = 0; i < rows * columns; ++ i)
this->data[i] = (values == NULL ? 0 : values[i]);
}
int _index(int row, int column) const {
return (row - 1) * this->columns + (column - 1);
}
public:
Matrix(int rows, int columns) {
this->_init(rows, columns, NULL);
}
Matrix(int rows, int columns, double values[]) {
this->_init(rows, columns, values);
}
Matrix(const Matrix & matrix) {
this->_init(matrix.rows, matrix.columns, matrix.data);
}
~Matrix() {
delete [] this->data;
}
void print() const {
for (int i = 0; i < this->rows; ++ i) {
for (int j = 0; j < this->columns; ++ j) {
cout _index(row, column)] = value;
}
double get(int row, int column) const {
return this->data[this->_index(row, column)];
}
Matrix operator * (const Matrix & matrix2) const{
Matrix m3(rows,columns,NULL);
for (int i = 0; i < rows; ++ i){
for(int j=0;jdata[i]*value;
}
return m3;
}
};