阅读 205

PTA 矩阵的乘法运算(pta简单阶乘计算)

线性代数中的矩阵可以表示为一个row*column的二维数组,当row和column均为1时,退化为一个数,当row为1时,为一个行向量,当column为1时,为一个列向量。 建立一个整数矩阵类matrix,其私有数据成员如下:

int row; int column; int **mat; 复制代码

建立该整数矩阵类matrix构造函数; 建立一个 *(乘号)的运算符重载,以便于对两个输入矩阵进行乘法运算; 建立输出函数void display(),对整数矩阵按行进行列对齐输出,格式化输出语句如下:

cout<<setw(10)<<mat[i][j]; //需要#include <iomanip> 复制代码

主函数里定义三个整数矩阵类对象m1、m2、m3. ###输入格式: 分别输入两个矩阵,分别为整数矩阵类对象m1和m2。 每个矩阵输入如下: 第一行两个整数 r c,分别给出矩阵的行数和列数 接下来输入r行,对应整数矩阵的每一行 每行输入c个整数,对应当前行的c个列元素 ###输出格式: 整数矩阵按行进行列对齐(宽度为10)后输出 判断m1和m2是否可以执行矩阵相乘运算。 若可以,执行m3=m1*m2运算之后,调用display函数,对m3进行输出。 若不可以,输出"Invalid Matrix multiplication!" 提示:输入或输出的整数矩阵,保证满足row>=1和column>=1。

输入样例:

4  5 1 0 0 0 5 0 2 0 0 0 0 0 3 0 0 0 0 0 4 0 5  5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 8 9 5 6 7 8 9 结尾无空行 复制代码

输出样例:

        26        32        38        44        50          4         6         8        10        12          9        12        15        18        21         16        20        24        32        36 结尾无空行 复制代码

代码:

#include<iostream> #include <iomanip> using namespace std; class Matrix { public:     Matrix(int, int);//1.构造函数     ~Matrix();//2.析构函数     friend istream& operator>>(istream& is, Matrix& a);//3.重载>>     int getRow() { return row; }//4     int getColum() { return column; }//5     friend Matrix operator*(Matrix&, Matrix&);//6.重载*     Matrix(const Matrix&);//7.复制构造函数     void display();//8.输出 private:     int row;     int column;     int** mat; }; Matrix::Matrix(int r, int c)//1.构造函数 {     row = r;     column = c;     mat = new int* [r + 2];//不开大一点有一个测试点过不了     for (int i = 0; i < row + 2; i++)//亲测     {         mat[i] = new int[c + 2];     } } Matrix::~Matrix()//2.析构函数 {     for (int i = 0; i < row + 2; i++)         delete[]mat[i];     delete[]mat; } istream& operator>>(istream& is, Matrix& a) {//3.重载>>     for (int i = 0; i < a.row; i++)     {         for (int j = 0; j < a.column; j++)         {             is >> a.mat[i][j];         }     } } Matrix operator*(Matrix& a, Matrix& b) {//6.重载*     int i, j, k, x;     if (a.row == 1 && a.column == 1)//数乘矩阵     {         Matrix p(b.row, b.column);         for (i = 0; i < b.row; i++) {             for (j = 0; j < b.column; j++) {                 p.mat[i][j] = a.mat[0][0] * b.mat[i][j];             }         }         return p;     }     else//哈哈哈矩阵乘以数没有测试点,我就给删了     {//普通的矩阵*矩阵         Matrix p(a.row, b.column);         for (i = 0; i < a.row; i++)         {             for (j = 0; j < b.column; j++)             {                 x = 0;                 for (k = 0; k < a.column; k++)                 {                     x += a.mat[i][k] * b.mat[k][j];                 }                 p.mat[i][j] = x;             }         }         return p;     } } Matrix::Matrix(const Matrix& p) {//7.复制构造函数     this->row = p.row;     this->column = p.column;     this->mat = new int* [p.row + 2];     int i, j;     for (i = 0; i < p.row + 2; i++)     {         this->mat[i] = new int[p.column + 2];         for (j = 0; j < p.column; j++)         {             this->mat[i][j] = p.mat[i][j];         }     } } void Matrix::display() {//8.输出     for (int i = 0; i < row; i++)     {         for (int j = 0; j < column; j++)         {             cout << setw(10) << mat[i][j];         }         cout << endl;     } } int main() {     int a, b, i, j;     cin >> a >> b;     Matrix x(a, b);     cin >> x;//还是重载一下好看     cin >> a >> b;     Matrix y(a, b);     cin >> y;     if (x.getColum() == y.getRow() || x.getColum() == 1 && x.getRow() == 1 || y.getRow() == 1 && y.getRow() == 1)      {         Matrix z = x * y;         z.display();     }     else      {         cout << "Invalid Matrix multiplication!" << endl;     }     return 0; } 复制代码

提交结果:

1.png


作者:devon
链接:https://juejin.cn/post/7032870049459208229

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐