Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- class Matrix{
- private:
- int h;
- int c;
- public:
- int data[100][100];
- friend istream &operator>>( istream &input, Matrix& K )
- {
- cout << "Nhap vao hang: ";
- input >> K.h;
- cout << "Nhap vao cot: ";
- input >> K.c;
- for(int i = 0; i < K.h; i ++)
- {
- for(int j = 0; j < K.c; j ++)
- { cout << "Nhap vao phan tu " << i << " " << j << ":";
- input >> K.data[i][j];
- }
- }
- return input;
- }
- friend ostream &operator<<( ostream &input, Matrix k )
- {
- for(int i = 0; i < k.h; i ++)
- {
- for(int j = 0; j < k.c; j ++)
- {
- input << k.data[i][j] << "\t";
- }
- input << "\n";
- }
- return input;
- }
- Matrix operator+(Matrix b)
- {
- Matrix c1;
- c1.h = h;
- c1.c = c;
- for(int i = 0; i < h; i ++)
- {
- for(int j = 0; j < c; j ++)
- {
- c1.data[i][j] = data[i][j] + b.data[i][j];
- }
- }
- return c1;
- }
- Matrix operator-(Matrix b)
- {
- Matrix c1;
- c1.h = h;
- c1.c = c;
- for(int i = 0; i < h; i ++)
- {
- for(int j = 0; j < c; j ++)
- {
- c1.data[i][j] = data[i][j] - b.data[i][j];
- }
- }
- return c1;
- }
- Matrix operator*(Matrix b)
- {
- Matrix matrantich;
- matrantich.h = h;
- matrantich.c = b.c;
- for(int i=0;i<matrantich.h;i++)
- {
- for(int j=0;j<matrantich.c;j++)
- {
- for(int k=0;k<matrantich.c;k++)
- {
- matrantich.data[i][j]+=data[i][k]*b.data[k][j];
- }
- }
- }
- return matrantich;
- }
- };
- int main()
- {
- Matrix a;
- Matrix b;
- cin >> a;
- cin >> b;
- cout << a * b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement