Matrix class template (class template)

【id:342】【14分】F. 矩阵类模板(类模板)
时间限制
1s
内存限制
128MB
题目描述

设计一个矩阵类模板Matrix,支持任意数据类型的数据。

要求至少包含2个成员函数:矩阵转置函数transport、以及打印输出函数print

编写main函数进行测试,调用类的成员函数完成转置和输出。


输入

第一行先输入t,表示有t个测试用例

从第二行开始输入每个测试用例的数据。

首先输入数据类型,I表示int,D表示double,C表示char,接着输入两个参数m和n,分别表示矩阵的行和列

接下来输入矩阵的元素,一共m行,每行n个数据


输出

输出转置后的矩阵


样例查看模式 
正常显示
查看格式
输入样例1 <-复制
2
I 2 3
1 2 3
4 5 6
C 3 3
a b c
d e f
g h i
输出样例1
1 4
2 5
3 6
a d g
b e h
c f i

class template

Add to class

template<class T>

The data members in the class are of type T

The incoming array parameters need to be entered outside first and cannot be entered in the constructor.

Definition in main function

Matrix<int> one(arr, row, column);

one is the object name

#include <iomanip>
#include <algorithm>
#include "iostream"

using namespace std;

template<class T>
class Matrix {
    //要求至少包含2个成员函数:矩阵转置函数transport、以及打印输出函数print
public:
    T **arr;
    int row;
    int column;

    Matrix(T **arr, int row, int column) : arr(arr), row(row), column(column) {}

    void transport() {
        T **res = new T*[column];
        for (int i = 0; i < column; ++i) {
            res[i] = new T[row];
        }
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < column; ++j) {
                res[j][i] = arr[i][j];
            }
        }
        arr = res;
    }

    void print() {
        for (int i = 0; i < column; ++i) {
            for (int j = 0; j < row - 1; ++j) {
                cout << arr[i][j] << " ";
            }
            cout << arr[i][row - 1] << endl;
        }
    }
};

int main() {
    int times;
    cin >> times;
    while (times--) {
        char type;
        int row;
        int column;
        cin >> type >> row >> column;
        //I表示int,D表示double,C表示char
        switch (type) {
            case 'I': {
                int **arr = new int *[row];
                for (int i = 0; i < row; ++i) {
                    arr[i] = new int[column];
                }
                for (int i = 0; i < row; ++i) {
                    for (int j = 0; j < column; ++j) {
                        cin >> arr[i][j];
                    }
                }
                Matrix<int> one(arr, row, column);
                one.transport();
                one.print();
                break;
            }
            case 'D': {
                double **arr = new double *[row];
                for (int i = 0; i < row; ++i) {
                    arr[i] = new double[column];
                }
                for (int i = 0; i < row; ++i) {
                    for (int j = 0; j < column; ++j) {
                        cin >> arr[i][j];
                    }
                }
                Matrix<double>one(arr, row, column);   one.transport();
                one.print();
                break;
            }
            case 'C': {
                char **arr = new char *[row];
                for (int i = 0; i < row; ++i) {
                    arr[i] = new char[column];
                }
                for (int i = 0; i < row; ++i) {
                    for (int j = 0; j < column; ++j) {
                        cin >> arr[i][j];
                    }
                }
                Matrix<char>one(arr, row, column);   one.transport();
                one.print();
                break;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/m0_62288512/article/details/131549871