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 a one-dimensional array, you need to fill them 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::max() const.
A function Matrix Matrix::min() const.
A function Matrix Matrix::sum() const.
理解题意需要看下面两行:
The last three functions return a row matrix if the 'this' matrix contains multiple rows, otherwise the result matrix is 1x1.
For the max function, the returned row matrix consists of the largest values in each column of the 'this' matrix.
They return a 1-by-1 matrix otherwise.
EXAMPLE INPUT
4 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
EXAMPLE OUTPUT
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
16 17 18 19 20
20
1 2 3 4 5
1
34 38 42 46 50
210
主程序 (不能修改)
#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
matrix1.print();
cout << endl;
matrix1.max().print();
cout << endl;
matrix1.max().max().print();
cout << endl;
matrix1.min().print();
cout << endl;
matrix1.min().min().print();
cout << endl;
matrix1.sum().print();
cout << endl;
matrix1.sum().sum().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 max() const{
if(this->rows>1){
Matrix m(1,this->columns, NULL);
for (int i = 0; i < this->columns; ++ i) {
int temp=0;
for (int j = 0; j < this->rows; ++ j) {
if(temp<this->get(j+1,i+1)){
temp=this->get(j+1,i+1);
}
}
m.set(1,i+1,temp);
}
return m;
}
else{
Matrix m(1,1, NULL);
int temp=0;
for (int i = 0; i < this->columns; ++ i){
if(temp<this->get(1,i+1)){
temp=this->get(1,i+1);
}
m.set(1,1,temp);
}
return m;
}
}
Matrix min() const{
if(this->rows>1){
Matrix m(1,this->columns, NULL);
for (int i = 0; i < this->columns; ++ i) {
int temp=10000;
for (int j = 0; j < this->rows; ++ j) {
if(temp>this->get(j+1,i+1)){
temp=this->get(j+1,i+1);
}
}
m.set(1,i+1,temp);
}
return m;
}
else{
Matrix m(1,1, NULL);
int temp=10000;
for (int i = 0; i < this->columns; ++ i){
if(temp>this->get(1,i+1)){
temp=this->get(1,i+1);
}
m.set(1,1,temp);
}
return m;
}
}
Matrix sum() const{
if(this->rows>1){
Matrix m(1,this->columns, NULL);
for (int i = 0; i < this->columns; ++ i) {
int temp=0;
for (int j = 0; j < this->rows; ++ j) {
temp=temp+get(j+1,i+1);
}
m.set(1,i+1,temp);
}
return m;
}
else{
Matrix m(1,1, NULL);
int temp=0;
for (int i = 0; i < this->columns; ++ i){
temp=temp+this->get(1,i+1);
}
m.set(1,1,temp);
return m;
}
}
};
YZB的答案
#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;
}
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)];
}
Matrix max(){
if(this->rows==1){
int maxnumber=0;
for (int i = 0; i < this->columns; ++ i){
if(this->data[i]>maxnumber){
maxnumber=this->data[i];
}
}
Matrix m3(1,1,NULL);
m3.set(1,1,maxnumber);
return m3;
}
else{
Matrix m3(1,columns,NULL);
for (int i = 0; i < this->columns; ++ i){
int maxnumber=this->get(1,i+1);
for(int j=0;j<this->rows;++j){
if(maxnumber<this->get(j+1,i+1)){
maxnumber=this->get(j+1,i+1);
}
}
m3.set(1,i+1,maxnumber);
}
return m3;
}
}
Matrix min(){
if(this->rows==1){
int minnumber=1000;
for (int i = 0; i < this->columns; ++ i){
if(this->data[i]<minnumber){
minnumber=this->data[i];
}
}
Matrix m3(1,1,NULL);
m3.set(1,1,minnumber);
return m3;
}
else{
Matrix m3(1,columns,NULL);
for (int i = 0; i < this->columns; ++ i){
int minnumber=this->get(1,i+1);
for(int j=0;j<this->rows;++j){
if(minnumber>this->get(j+1,i+1)){
minnumber=this->get(j+1,i+1);
}
}
m3.set(1,i+1,minnumber);
}
return m3;
}
}
Matrix sum(){
if(this->rows==1){
int sumnumber=0;
for (int i = 0; i < this->columns; ++ i){
sumnumber=sumnumber+this->data[i];
}
Matrix m3(1,1,NULL);
m3.set(1,1,sumnumber);
return m3;
}
else{
Matrix m3(1,columns,NULL);
for (int i = 0; i < this->columns; ++ i){
int sumnumber=0;
for(int j=0;j<this->rows;++j){
sumnumber=sumnumber+this->get(j+1,i+1);
}
m3.set(1,i+1,sumnumber);
}
return m3;
}
}
};
```c
#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 max(){
if(this->rows==1){
int maxnumber=0;
for (int i = 0; i < this->columns; ++ i){
if(this->data[i]>maxnumber){
maxnumber=this->data[i];
}
}
Matrix m3(1,1,NULL);
m3.set(1,1,maxnumber);
return m3;
}
else{
Matrix m3(1,columns,NULL);
for (int i = 0; i < this->columns; ++ i){
int maxnumber=this->get(1,i+1);
for(int j=0;jrows;++j){
if(maxnumberget(j+1,i+1)){
maxnumber=this->get(j+1,i+1);
}
}
m3.set(1,i+1,maxnumber);
}
return m3;
}
}
Matrix min(){
if(this->rows==1){
int minnumber=1000;
for (int i = 0; i < this->columns; ++ i){
if(this->data[i]data[i];
}
}
Matrix m3(1,1,NULL);
m3.set(1,1,minnumber);
return m3;
}
else{
Matrix m3(1,columns,NULL);
for (int i = 0; i < this->columns; ++ i){
int minnumber=this->get(1,i+1);
for(int j=0;jrows;++j){
if(minnumber>this->get(j+1,i+1)){
minnumber=this->get(j+1,i+1);
}
}
m3.set(1,i+1,minnumber);
}
return m3;
}
}
Matrix sum(){
if(this->rows==1){
int sumnumber=0;
for (int i = 0; i < this->columns; ++ i){
sumnumber=sumnumber+this->data[i];
}
Matrix m3(1,1,NULL);
m3.set(1,1,sumnumber);
return m3;
}
else{
Matrix m3(1,columns,NULL);
for (int i = 0; i < this->columns; ++ i){
int sumnumber=0;
for(int j=0;jrows;++j){
sumnumber=sumnumber+this->get(j+1,i+1);
}
m3.set(1,i+1,sumnumber);
}
return m3;
}
}
};
```