Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. class Matrix{
  4. private:
  5. int h;
  6. int c;
  7. public:
  8. int data[100][100];
  9. friend istream &operator>>( istream &input, Matrix& K )
  10. {
  11. cout << "Nhap vao hang: ";
  12. input >> K.h;
  13. cout << "Nhap vao cot: ";
  14. input >> K.c;
  15. for(int i = 0; i < K.h; i ++)
  16. {
  17. for(int j = 0; j < K.c; j ++)
  18. { cout << "Nhap vao phan tu " << i << " " << j << ":";
  19. input >> K.data[i][j];
  20. }
  21. }
  22. return input;
  23. }
  24. friend ostream &operator<<( ostream &input, Matrix k )
  25. {
  26. for(int i = 0; i < k.h; i ++)
  27. {
  28. for(int j = 0; j < k.c; j ++)
  29. {
  30. input << k.data[i][j] << "\t";
  31. }
  32. input << "\n";
  33. }
  34. return input;
  35. }
  36. Matrix operator+(Matrix b)
  37. {
  38. Matrix c1;
  39. c1.h = h;
  40. c1.c = c;
  41. for(int i = 0; i < h; i ++)
  42. {
  43. for(int j = 0; j < c; j ++)
  44. {
  45. c1.data[i][j] = data[i][j] + b.data[i][j];
  46. }
  47. }
  48. return c1;
  49. }
  50. Matrix operator-(Matrix b)
  51. {
  52. Matrix c1;
  53. c1.h = h;
  54. c1.c = c;
  55. for(int i = 0; i < h; i ++)
  56. {
  57. for(int j = 0; j < c; j ++)
  58. {
  59. c1.data[i][j] = data[i][j] - b.data[i][j];
  60. }
  61. }
  62. return c1;
  63. }
  64. Matrix operator*(Matrix b)
  65. {
  66. Matrix matrantich;
  67. matrantich.h = h;
  68. matrantich.c = b.c;
  69. for(int i=0;i<matrantich.h;i++)
  70. {
  71. for(int j=0;j<matrantich.c;j++)
  72. {
  73. for(int k=0;k<matrantich.c;k++)
  74. {
  75. matrantich.data[i][j]+=data[i][k]*b.data[k][j];
  76. }
  77. }
  78. }
  79. return matrantich;
  80. }
  81. };
  82.  
  83. int main()
  84. {
  85. Matrix a;
  86. Matrix b;
  87. cin >> a;
  88. cin >> b;
  89. cout << a * b;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement