Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <valarray>
- #include <cassert>
- using namespace std;
- class matrix{
- protected:
- valarray<int> data; //valarray that will simulate matrix
- int row, col;
- public:
- void createMatrix (int row, int col, int num[], matrix& mat)
- {
- mat.row = row;
- mat.col = col;
- mat.data.resize (row * col);
- for (int i = 0; i < row * col; i++)
- mat.data [i] = num [i];
- }
- friend matrix operator+= (matrix& mat1, matrix mat2);
- friend matrix operator-= (matrix& mat1, matrix mat2);
- friend matrix operator+= (matrix& mat, int scalar);
- friend matrix operator-= (matrix& mat, int scalar);
- friend void operator++ (matrix& mat);
- friend void operator-- (matrix& mat);
- friend istream& operator>> (istream& in, matrix& mat);
- };
- //__________________________________________
- matrix operator+= (matrix& mat1, matrix mat2){
- if(mat1.col==mat2.col && mat1.row==mat2.row)
- {
- for (int i=0; i<mat1.data.size(); i++)
- {
- cout<< mat1.data[i]+mat2.data[i];
- }
- }
- else
- {
- cout<< "mat1 ,mat2 not the same dimensions .\n";
- }
- }
- //__________________________________________
- matrix operator-= (matrix& mat1, matrix mat2)
- {
- if(mat1.col==mat2.col && mat1.row==mat2.row)
- {
- for (int i=0; i<mat1.data.size(); i++)
- {
- cout<<mat1.data[i]-mat2.data[i];
- }
- }
- else
- {
- cout<< "mat1 ,mat2 not the same dimensions .\n";
- }
- }
- //__________________________________________
- matrix operator+= (matrix& mat, int scalar)
- {
- for(int i=0; i<mat.data.size(); i++ )
- {
- cout<< mat.data[i]+scalar;
- }
- }
- //__________________________________________
- matrix operator-= (matrix& mat, int scalar)
- {
- for(int i=0; i<mat.data.size(); i++ )
- {
- cout<< mat.data[i]-scalar;
- }
- }
- //__________________________________________
- void operator++ (matrix& mat)
- {
- for(int i=0; i<mat.data.size(); i++ )
- {
- cout<<mat.data[i]+1<<" ";
- }
- }
- //__________________________________________
- void operator-- (matrix& mat)
- {
- for(int i=0; i<mat.data.size(); i++ )
- {
- cout<<mat.data[i]-1<<" ";
- }
- }
- //__________________________________________
- istream& operator>> (istream& in, matrix& mat)
- {
- cout<<"enter the row and the collom \n ";
- cout<<"row = ";
- cin>>mat.row;
- cout<<" \ncollom = ";
- cin>> mat.col;
- mat.data.resize(mat.col*mat.row);
- cout<<" \nenter data of matrix \n";
- for (int i=0; i<mat.col*mat.row; i++)
- {
- cin>>mat.data[i];
- }
- return in;
- }
- int main(){
- matrix g;
- cout<<"choose(1) if we have matrix.... \nchoose (2) if you will enter matrix.... \n choose (3) to know the matrix properties \n==>";
- int x,y,z;
- cin>>x;
- matrix mat1, mat2, mat3,mat4;
- /* if(x==1 || x ==3 )
- {
- int i,s1, s2,s3 ;
- cout <<"selct your choose:\n1-addition for two matrix\n2-subtraction of two matrix\n3-multiplication for two matrix\n4-add number for matrix\n5-subtract number form matrix\n6-multiply number to matrix " <<endl;
- cin >> i ;
- int data1[] = {1,2,3,4,50,60};
- int data2 [] = {1,2,3,4,5,6};
- int data3 [] = {10,100,10,100,10,100,10,100};
- createMatrix (3, 2, data1, mat1);
- createMatrix (2,2, data2, mat2);
- createMatrix (4, 2, data3, mat3);
- if (i==1)
- {
- mat1 + mat3;
- }
- if (i==2)
- {
- mat1 - mat3;
- }
- if (i==3)
- {
- matrix sum;
- sum=mat1 * mat2;
- print(sum);
- }
- if (i==4)
- {
- cout << "enter your number : "<<endl;
- cin >> s1;
- mat1 + s1;
- }
- if (i==5)
- {
- mat1 - s2;
- cout << "enter your number : "<<endl;
- cin >> s2;
- }
- if (i==6)
- {
- mat1 * s3;
- cout << "enter your number : "<<endl;
- cin >> s3;
- }
- }*/
- if(x==2)
- {
- cout<<"what are you want : \n1- matrix operator+= & matrix operator-= \n2- matrix operator+=scalar & matrix operator-=scalar \n3- mat++ & mat--" <<endl;
- int y;
- cin>>y;
- if (y==1)
- {
- cout<<"enter mat1 : \n";
- cin>>mat1;
- cout<<"\nenter mat2 : \n";
- cin>>mat2;
- int c ;
- cout << " 1-plus or 2-mainus " <<endl;
- cin >> c ;
- if (c ==1)
- {
- cout<<"\nmatrix operator+= :\n";
- (mat1+=mat2);
- }
- else if (c == 2)
- {
- cout<<"\nmatrix operator-= :\n";
- (mat1-=mat2);
- }
- }
- if(y==2)
- {
- int scalar,c;
- cout<<"enter mat : \n";
- cin>>mat3;
- cout<<"\nenter the scalar : ";
- cin>>scalar;
- cout << " 1-plus or 2-mainus " <<endl;
- cin >> c;
- if (c ==1)
- {
- cout<<"\nmatrix operator+=scalar :\n";
- (mat3+=scalar);
- }
- else if (c == 2)
- {
- cout<<"\nmatrix operator-=scalar :\n";
- (mat3-=scalar);
- }
- }
- if(y==3)
- {
- int c ;
- cout<<"enter mat : \n";
- cin>>mat3;
- cout << " 1-plus or 2-mainus " <<endl;
- cin >> c ;
- if (c == 1 )
- {
- cout<<"\nmat++ :\n";
- (++mat3);
- }
- else if (c == 2)
- {
- cout<<"\nmat-- :\n";
- (--mat3);
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment