10.1 (C++)

In this exercise, you need to know how and when to throw an exception.

Design a class Matrix that throws the following exceptions:1. A out_of_range exception, which is thrown whenever a user access an element in a matrix via an out-of-range row/column index. This exception is thrown by object functions get and set . This exception is defined by C++ in a library file named <stdexcept>

  1. A MatrixSizesDoNotMatchException is thrown whenever adding two matrixes, whose rows or columns do not equal. You need to define this exception class. This exception is thrown in the object function operator + .

EXAMPLE INPUT

2 3
1 2 3
4 5 6

1 2
2 4

2 3
2 3 4
3 4 5

2 4
1 2 3 4
4 3 2 1

EXAMPLE OUTPUT

value = 2
caught: out_of_range
(2,3)
 3 5 7
 7 9 11
caught: MatrixSizesDoNotMatchException

主程序 (不能修改)

#include <vector>
using namespace std;

class Matrix
{
private:
    int rows;
    int columns;
    vector<double> elements;
    
public:
    Matrix(int rows, int columns) {
        this->rows = rows;
        this->columns = columns;
        for (int i = 0; i < rows * columns;  ++ i) {
            elements.push_back(0.0);
        }
    }
    
    int size(int dimension) const {
        switch (dimension) {
        case 1: return rows;
        case 2: return columns;
        }
        return 0;
    }
    
    double get(int row, int column) const;
    
    void set(int row, int column, double value);
    
    Matrix operator + (const Matrix & matrix2) const;
};

#include "source.cpp"

#include <iostream>
using namespace std;

Matrix read() {
    int rows;
    int columns;
    cin >> rows >> columns;
    Matrix matrix(rows, columns);
    for (int i = 0; i < rows; ++ i) {
        for (int j = 0; j < columns; ++ j) {
            double value;
            cin >> value;
            matrix.set(i + 1, j + 1, value);
        }
    }
    return matrix;
}

void print(const Matrix & matrix) {
    int rows = matrix.size(1);
    int columns = matrix.size(2);
    cout << "(" << rows << "," << columns << ")" << endl;
    for (int i = 0; i < rows; ++ i) {
        for (int j = 0; j < columns; ++ j) {
            cout << " " << matrix.get(i + 1, j + 1);
        }
        cout << endl;
    }
}

void test1(const Matrix & matrix) {
    for (int i = 0; i < 2; ++ i) {
        int row;
        int column;
        cin >> row >> column;
        try {
            double value = matrix.get(row, column);
            cout << "value = " << value << endl;
        }
        catch (out_of_range & ex) {
            cout << "caught: out_of_range" << endl;
        }
    }
}

void test2(const Matrix & matrix) {
    for (int i = 0; i < 2; ++ i) {
        Matrix matrix2 = read();
        try {
            matrix2 = matrix + matrix2;
            print(matrix2);
        }
        catch (MatrixSizesDoNotMatchException & ex) {
            cout << "caught: MatrixSizesDoNotMatchException" << endl;
        }
    }
}

int main() {
    Matrix matrix = read();
    test1(matrix);
    test2(matrix);
}

Aholic的答案

#include<stdexcept>
#include <iostream>
#include<vector>
using namespace std;
//class out_of_range{};
class MatrixSizesDoNotMatchException{};
   double Matrix::get(int row, int column) const{
/*       if(row>this->size(1)||column>this->size(2)){
           throw out_of_range();
       }*/
       int index = (row-1)* this->size(2) + (column-1);
        return this->elements[index];
   }
    
    void Matrix::set(int row, int column, double value){
/*        if(row>this->size(1)||column>this->size(2)){
           throw out_of_range();
       }*/
        int index = (row-1)* this->size(2) + column-1;
        elements[index]=value;
    }
    
    Matrix operator + (Matrix matrix1,Matrix & matrix2) {
        if(matrix2.size(1)!=matrix1.size(1)||matrix2.size(2)!=matrix1.size(2)){
            throw MatrixSizesDoNotMatchException();
        }
        for (int i = 0; i < matrix1.size(1); ++ i){
            for(int j=0;j<matrix1.size(2);++j){
                
                double a=matrix1.get(i+1,j+1)+matrix2.get(i+1,j+1);
                matrix1.set(i+1,j+1,a);
            }            
        } 
        return matrix1;
    }

另一版本

#include<stdexcept>
#include<iostream>
#include<vector>
using namespace std;
class MatrixSizesDoNotMatchException{};
   double Matrix::get(int row, int column) const{
       if(row>this->size(1)||column>this->size(2)){
           throw out_of_range("this->elements[row*column]");
       }
       int index = (row-1)* this->size(2) + (column-1);
        return this->elements[index];
   }
    
    void Matrix::set(int row, int column, double value){
        if(row>this->size(1)||column>this->size(2)){
           throw out_of_range("this->elements[row*column]");
       }
        int index = (row-1)* this->size(2) + column-1;
        elements[index]=value;
    }
    
    Matrix operator + (Matrix matrix1,Matrix & matrix2) {
        if(matrix2.size(1)!=matrix1.size(1)||matrix2.size(2)!=matrix1.size(2)){
            throw MatrixSizesDoNotMatchException();
        }
        for (int i = 0; i < matrix1.size(1); ++ i){
            for(int j=0;j<matrix1.size(2);++j){
                double a=matrix1.get(i+1,j+1)+matrix2.get(i+1,j+1);
                matrix1.set(i+1,j+1,a);
            }            
        } 
        return matrix1;
    }
支持 makedown语法