当前位置:   article > 正文

头歌实训项目【矩阵运算】_python头歌第6关:矩阵运算

python头歌第6关:矩阵运算
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Matrix
  5. {
  6. friend Matrix operator+(Matrix& m1, Matrix& m2);
  7. friend Matrix operator-(Matrix& m1, Matrix& m2);
  8. friend Matrix operator*(Matrix& m1, Matrix& m2);
  9. //矩阵类的声明
  10. private:
  11. int m_r;
  12. int m_c;
  13. int m_Array[10][10];
  14. public:
  15. Matrix(int r, int c)
  16. {
  17. m_r = r;
  18. m_c = c;
  19. Fill(0);
  20. }
  21. void Fill(int value);
  22. void Set(int r, int c, int value);
  23. int Get(int r, int c);
  24. void Print();
  25. };
  26. //Matrix::Matrix(int r,int c):m_r(r),m_c(c){}
  27. //全部设值函数
  28. void Matrix::Fill(int value)
  29. {
  30. for (int i = 0; i < m_r; i++)
  31. {
  32. for (int j = 0; j < m_c; j++)
  33. {
  34. m_Array[i][j] = value;
  35. }
  36. }
  37. return;
  38. }
  39. //指定位置设值函数
  40. void Matrix::Set(int r, int c, int value)
  41. {
  42. m_Array[r][c] = value;
  43. }
  44. //获取元素函数
  45. int Matrix::Get(int r, int c)
  46. {
  47. return m_Array[r][c];
  48. }
  49. //打印函数
  50. void Matrix::Print()
  51. {
  52. for (int i = 0; i < m_r; i++)
  53. {
  54. for (int j = 0; j < m_c; j++)
  55. {
  56. cout << m_Array[i][j]<<" ";
  57. if (j == m_c - 1)
  58. {
  59. cout << endl;
  60. }
  61. }
  62. }
  63. }
  64. //实现矩阵加法
  65. Matrix operator+(Matrix& m1, Matrix& m2)
  66. {
  67. Matrix temp(m1.m_r, m1.m_c);
  68. for (int i = 0; i < m1.m_r; i++)
  69. {
  70. for (int j = 0; j < m1.m_c; j++)
  71. {
  72. temp.m_Array[i][j] = m1.m_Array[i][j] + m2.m_Array[i][j];
  73. }
  74. }
  75. return temp;
  76. }
  77. //实现矩阵减法
  78. Matrix operator-(Matrix& m1, Matrix& m2)
  79. {
  80. Matrix temp(m1.m_r, m1.m_c);
  81. for (int i = 0; i < m1.m_r; i++)
  82. {
  83. for (int j = 0; j < m1.m_c; j++)
  84. {
  85. temp.m_Array[i][j] = m1.m_Array[i][j] - m2.m_Array[i][j];
  86. }
  87. }
  88. return temp;
  89. }
  90. //实现矩阵乘法
  91. Matrix operator*(Matrix& m1, Matrix& m2)
  92. {
  93. Matrix temp(m1.m_r, m2.m_c);
  94. temp.Fill(0);
  95. for (int i = 0; i < m1.m_r; i++)
  96. {
  97. for (int j = 0; j < m2.m_c; j++)
  98. {
  99. for (int k = 0; k < m1.m_c; k++)
  100. {
  101. temp.m_Array[i][j] += m1.m_Array[i][k] * m2.m_Array[k][j];
  102. }
  103. }
  104. }
  105. return temp;
  106. }
  107. int main()
  108. {
  109. int i, j;
  110. cin >> i >> j;
  111. Matrix m1(i, j), m2(i, j), m3(j, i);
  112. m1.Fill(1);
  113. m2.Fill(2);
  114. m3.Fill(0);
  115. for (int s = 0; s < i; s++) {
  116. for (int c = 0; c < j; c++) {
  117. if (s == c)
  118. m3.Set(s, c, s + 1);
  119. }
  120. }
  121. m1.Print();
  122. m2.Print();
  123. m3.Print();
  124. cout << "m1 + m2 :" << endl;
  125. (m1 + m2).Print();
  126. cout << "m1 - m2 :" << endl;
  127. (m1 - m2).Print();
  128. cout << "m1 * m3 :" << endl;
  129. (m1 * m3).Print();
  130. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/614393
推荐阅读
相关标签
  

闽ICP备14008679号