鱼C论坛

 找回密码
 立即注册
查看: 2247|回复: 4

[已解决]求问这个数组乘法的程序为什么完成不了

[复制链接]
发表于 2019-4-2 22:45:13 | 显示全部楼层 |阅读模式
5鱼币
代码及调用见图
最佳答案
2019-4-2 22:45:14
本帖最后由 Croper 于 2019-4-3 00:33 编辑

写了个矩阵的类,实现了矩阵乘法,你可以参考下
方向可能有点小问题,毕竟这一块都快忘光了。。。

  1. #include <initializer_list>
  2. #include <iostream>

  3. template <int _cy, int _cx> class Matrix {
  4.         int _data[_cy][_cx];
  5. public:
  6.         int* operator[](int rowindex) {
  7.                 return _data[rowindex];
  8.         }

  9.         const int* operator[](int rowindex) const {
  10.                 return const_cast<Matrix*>(this)->operator[](rowindex);
  11.         }

  12.         Matrix() {
  13.                 memset(_data, 0, sizeof(_data));
  14.         }
  15.         Matrix(const std::initializer_list<int>& list) :Matrix() {
  16.                 int *p = (int*)_data;
  17.                 for (auto it = list.begin(); it != list.end(); ++it, ++p) {
  18.                         *p = *it;
  19.                 }
  20.         }
  21.         Matrix(const Matrix& m2) {
  22.                 memcpy(_data, m2._data, sizeof(_data));
  23.         }

  24.         template <int _cz>
  25.         Matrix<_cy, _cz> operator*(const Matrix<_cx, _cz>& m2) {
  26.                 Matrix<_cy, _cz> ret;
  27.                 for (int y = 0; y < _cy; ++y) for (int z = 0; z < _cz; ++z) {
  28.                         for (int x = 0; x < _cx; ++x) {
  29.                                 ret[y][z] += _data[y][x] * m2[x][z];
  30.                         }
  31.                 }
  32.                 return ret;
  33.         }


  34.         void print() {
  35.                 std::cout << std::endl;
  36.                 for (int y = 0; y < _cy; ++y) {
  37.                         for (int x = 0; x < _cx; ++x) {
  38.                                 std::cout << '\t' << _data[y][x];
  39.                         }
  40.                         std::cout << std::endl;
  41.                 }
  42.         }
  43. };

  44. int main() {
  45.         Matrix<2,3> m1 = { 1,2,3,4,5,6 };
  46.         Matrix<3, 4> m2 = { 1,2,3,4,5,6,7,8,9,10,11,12 };
  47.         m1.print();
  48.         m2.print();
  49.         auto m3 = m1 * m2;
  50.         m3.print();
  51.         system("pause");

  52. }
复制代码
QQ截图20190402224306.png
QQ截图20190402224253.png

最佳答案

查看完整内容

写了个矩阵的类,实现了矩阵乘法,你可以参考下 方向可能有点小问题,毕竟这一块都快忘光了。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 22:45:14 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Croper 于 2019-4-3 00:33 编辑

写了个矩阵的类,实现了矩阵乘法,你可以参考下
方向可能有点小问题,毕竟这一块都快忘光了。。。

  1. #include <initializer_list>
  2. #include <iostream>

  3. template <int _cy, int _cx> class Matrix {
  4.         int _data[_cy][_cx];
  5. public:
  6.         int* operator[](int rowindex) {
  7.                 return _data[rowindex];
  8.         }

  9.         const int* operator[](int rowindex) const {
  10.                 return const_cast<Matrix*>(this)->operator[](rowindex);
  11.         }

  12.         Matrix() {
  13.                 memset(_data, 0, sizeof(_data));
  14.         }
  15.         Matrix(const std::initializer_list<int>& list) :Matrix() {
  16.                 int *p = (int*)_data;
  17.                 for (auto it = list.begin(); it != list.end(); ++it, ++p) {
  18.                         *p = *it;
  19.                 }
  20.         }
  21.         Matrix(const Matrix& m2) {
  22.                 memcpy(_data, m2._data, sizeof(_data));
  23.         }

  24.         template <int _cz>
  25.         Matrix<_cy, _cz> operator*(const Matrix<_cx, _cz>& m2) {
  26.                 Matrix<_cy, _cz> ret;
  27.                 for (int y = 0; y < _cy; ++y) for (int z = 0; z < _cz; ++z) {
  28.                         for (int x = 0; x < _cx; ++x) {
  29.                                 ret[y][z] += _data[y][x] * m2[x][z];
  30.                         }
  31.                 }
  32.                 return ret;
  33.         }


  34.         void print() {
  35.                 std::cout << std::endl;
  36.                 for (int y = 0; y < _cy; ++y) {
  37.                         for (int x = 0; x < _cx; ++x) {
  38.                                 std::cout << '\t' << _data[y][x];
  39.                         }
  40.                         std::cout << std::endl;
  41.                 }
  42.         }
  43. };

  44. int main() {
  45.         Matrix<2,3> m1 = { 1,2,3,4,5,6 };
  46.         Matrix<3, 4> m2 = { 1,2,3,4,5,6,7,8,9,10,11,12 };
  47.         m1.print();
  48.         m2.print();
  49.         auto m3 = m1 * m2;
  50.         m3.print();
  51.         system("pause");

  52. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 23:25:25 | 显示全部楼层
本帖最后由 Croper 于 2019-4-2 23:28 编辑

首先,resule没有申请空间

第二,你应该把代码贴上来,发图你是想让其他人帮你人脑跑程序么。。。
脑内编译,脑内运行,脑内报错还行

第三,不要英文拼音混用,不要英文拼音混用,不要英文拼音混用!我看了半天愣是没反应过来lie是什么意思

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-3 00:03:11 | 显示全部楼层
Croper 发表于 2019-4-2 23:25
首先,resule没有申请空间

第二,你应该把代码贴上来,发图你是想让其他人帮你人脑跑程序么。。。

完美赞同
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-3 11:19:53 | 显示全部楼层
Croper 发表于 2019-4-2 23:25
首先,resule没有申请空间

第二,你应该把代码贴上来,发图你是想让其他人帮你人脑跑程序么。。。

我在发帖的时候没有看到代码贴代码那一行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 10:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表