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.
A function Matrix Matrix::reshape(int rows, int columns) const, which returns a matrix that has the same elements as the default parameter of this object function (the invisible object parameter), but has different rows and columns as given by the parameters. Elements are moved from the invisible object parameter into the returned matrix ordered first by column and then by row.
EXAMPLE INPUT
4 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
6 4
EXAMPLE OUTPUT
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
1 14 4 17
7 20 10 23
13 3 16 6
19 9 22 12
2 15 5 18
8 21 11 24
主程序 (不能修改)
#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 matrix = read(); // calls copy constructor
matrix.print();
cout << endl;
int rows;
int columns;
cin >> rows >> columns;
matrix.reshape(rows, columns).print();
}
我的答案
#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 reshape(int rows, int columns) const{
Matrix m(rows, columns, NULL);
int count_columns=0;
int count_rows=0;
for (int i = 0; i <columns; ++ i){
for (int j = 0; j < rows; ++ j) {
m.set(j+1,i+1,this->get(count_rows+1,count_columns+1));
++count_rows;
if(count_rows==this->rows){
count_rows=0;
++count_columns;
}
}
}
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 reshape(int rows,int columns){
Matrix m3(rows,columns,NULL);
int count1=0;
int a[100]={0};
for (int i = 0; i columns ; ++ i){
for(int j=0;jrows;++j){
a[count1]=this->get(j+1,i+1);
count1++;
}
}
int count2=0;
for (int i = 0; i