2.12 (C++)

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 element-wise function Matrix Matrix::pow(double exponent).
A element-wise function Matrix Matrix::exp().
A element-wise function Matrix Matrix::log().
A element-wise function Matrix Matrix::abs().
EXAMPLE INPUT
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2
EXAMPLE OUTPUT

1    2    3    4
5    6    7    8
9    10    11    12
13    14    15    16

1    4    9    16
25    36    49    64
81    100    121    144
169    196    225    256

2.71828    7.38906    20.0855    54.5982
148.413    403.429    1096.63    2980.96
8103.08    22026.5    59874.1    162755
442413    1.2026e+06    3.26902e+06    8.88611e+06

0    0.693147    1.09861    1.38629
1.60944    1.79176    1.94591    2.07944
2.19722    2.30259    2.3979    2.48491
2.56495    2.63906    2.70805    2.77259

1    2    3    4
5    6    7    8
9    10    11    12
13    14    15    16

主程序 (不能修改)

#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
    
    double exponent;
    cin >> exponent;
    
    matrix.print();
    cout << endl;
    matrix.pow(exponent).print();
    cout << endl;
    matrix.exp().print();
    cout << endl;
    matrix.log().print();
    cout << endl;
    matrix.abs().print();
    cout << endl;
}

我的答案

#include <iostream>
#include<cmath>
double e=exp(1);
double duishu(int number){
    return log(number);
}
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 pow(double exponent){
       Matrix m(this->rows,this->columns, NULL);
        for (int i = 0; i <this->rows; ++ i) {
            for (int j = 0; j <this->columns; ++ j){
                int temp=1;
                for (int k = 0; k < exponent ; ++ k) {
                    temp=temp*this->get(i+1,j+1);
                }
                m.set(i+1,j+1,temp);
            } 
        }
       return m;
    } 
    
    Matrix exp(){
        Matrix m(this->rows,this->columns, NULL);
        for (int i = 0; i <this->rows; ++ i) {
            for (int j = 0; j <this->columns; ++ j){
                double temp=1;
                for (int k = 0; k < this->get(i+1,j+1) ; ++ k) {
                    temp=temp*e;
                }
                m.set(i+1,j+1,temp);
            } 
        }
        return m;
    }
    
    Matrix log(){
        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,duishu(this->get(i+1,j+1)));
            } 
        }
        return m;
    } 
    
    Matrix abs(){
        Matrix m(this->rows,this->columns, NULL);
        for (int i = 0; i <this->rows; ++ i) {
            for (int j = 0; j <this->columns; ++ j){
                int temp=this->get(i+1,j+1);
                if(temp<0){
                    temp=0-temp;
                }
                m.set(i+1,j+1,temp);
            } 
        }
        return m;
    }
};

真正的解决方案

#include <iostream>
#include<cmath>
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 pow(double exponent){
       Matrix m(this->rows,this->columns, NULL);
        for (int i = 0; i <this->rows; ++ i) {
            for (int j = 0; j <this->columns; ++ j){
                int temp=1;
                for (int k = 0; k < exponent ; ++ k) {
                    temp=temp*this->get(i+1,j+1);
                }
                m.set(i+1,j+1,temp);
            } 
        }
       return m;
    } 
    
    Matrix exp(){
        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,::exp(this->get(i+1,j+1)));
            } 
        }
        return m;
    }
    
    Matrix log(){
        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,::log(this->get(i+1,j+1)));
            } 
        }
        return m;
    } 
    
    Matrix abs(){
        Matrix m(this->rows,this->columns, NULL);
        for (int i = 0; i <this->rows; ++ i) {
            for (int j = 0; j <this->columns; ++ j){
                int temp=this->get(i+1,j+1);
                if(temp<0){
                    temp=0-temp;
                }
                m.set(i+1,j+1,temp);
            } 
        }
        return m;
    }
};
上一篇: 6.2 (C++) 下一篇: String
支持 makedown语法