2.5 (C++)

Design a class Matrix that has the following private member variables:
int rows
int columns
double * values
Besides, it has the following public functions:
A constructor Matrix(int rows, int column), which initializes all elements in the matrix to 0's.
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-dimension, you need to fill them into the two-dimensional matrix correctly.
A destructor.
A print function which prints each row of elements in a single line, with each element preceded by 4 spaces.
A function getRow(int row), which returns a row matrix for the given row.
A function getColumn(int column), which returns a column matrix for the given column.
EXAMPLE INPUT
4 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2
3
EXAMPLE OUTPUT

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

6    7    8    9    10

3
8
13
18

主程序 (不能修改)

#include "source.cpp"

int main() {
    int rows;
    int columns;
    double values[1000];
    cin >> rows >> columns;
    for (int i = 0; i < rows * columns; ++ i) {
        cin >> values[i];
    }
    Matrix matrix1(rows, columns, values);
    matrix1.print();
    cout << endl;

    int row;
    int column;
    cin >> row >> column;
    matrix1.getRow(row).print();
    cout << endl;
    matrix1.getColumn(column).print();
    
}

我的答案

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

class Matrix{
private:
    int rows;
    int columns;
    double * values[1000];
public:
    Matrix(int r,int col)
    {
        rows=r;
        columns=col;
    }
    Matrix(int r,int col,double val[])
    {
        rows=r;
        columns=col;
        for(int i=0;i<rows*columns;i++)
            values[i]=&val[i];
    }
    void print()
    {
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<columns;j++)
                cout<<"    "<<*values[j+i*columns];
            cout<<endl;
        }
    }
    Matrix getRow(int row){
        Matrix m(1,columns);
        for (int i = 0; i < columns; ++ i) {
            m.values[i]=values[(row-1)*columns+i];
        }
        return m;
    }
    
    Matrix getColumn(int column){
        Matrix m(rows,1);
        for (int i = 0; i < rows; ++ i) {
            m.values[i]=values[i*columns+column-1];
        }
        return m;
    }
};

Mitee的答案
3-9构造函数/5

Cong Liu的答案
正式版本

#include <iostream>
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;
 }
 
 Matrix & operator = (const Matrix & matrix) {
  delete [] this->data;
  this->_init(matrix.rows, matrix.columns, matrix.data);
  return *this;
 }
 
 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;
  }
 }
 
 void set(int row, int column, double value) {
  this->data[this->_index(row, column)] = value;
 }
 
 double get(int row, int column) const {
  return this->data[this->_index(row, column)];
 }
 
};

课堂抄写版(部分符号 变量有错误)

#include <iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int main() {

}
class Matrix{
private:    
    int rows;
    int columns;
    double *addr;
    
    void _init(int rows,int columns,double values[]){
        this->rows=rows;
        this->columns=columns;
        elements=new double[rows*columns];
        for (int i = 0; i < rows*columns ; ++ i) {
            elements[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.values);
    }
    //重新定义=(bushi
    operator = (const Matrix & m){
        double [] this->elements;
        this->_init(m.rows,m.columns,m.values);
        return *this;
    }
    
    ~Matrix(){
        double [] this->elements;
    }
    
    double get(int row, int col) const{
        int index = (row-1)* this->columns + col-1;
        return elements[index];
    }
    
    void set(int row, int col,double val){
        int index = (row-1)* this->columns + col-1;
        elements[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);
            }
        }
    }
    Matrix getRow(int row) const{
        Matrix(1,this->columns);
        for (int j = 0; j < this->columns ; ++ j) {
            m.set(1,j+1,this->get(row,j+1));
        }
        return m;
    }
    Matrix getColumns(int col) const{
        Matrix m(this->rows,1);
        for (int i = 0; i < this->rows; ++ i) {
            m.set(i+1,1,this->get(j+1,col));
        }
        return m;
    }
};
上一篇: 测试1 下一篇: 2.4 (C++)
支持 makedown语法

已有 2 条评论

  1. 欧巴巴o

    发新题!

    欧巴巴o March 17th, 2021 at 06:50 pm回复
    1. yizuodi

      2.6已更新 欢迎在评论区提供代码 评论支持markdown语法

      yizuodi March 18th, 2021 at 11:13 pm回复