Guest User

Untitled

a guest
Nov 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <valarray>
  4. #include <cassert>
  5.  
  6. using namespace std;
  7. class matrix{
  8. protected:
  9. valarray<int> data; //valarray that will simulate matrix
  10. int row, col;
  11. public:
  12. void createMatrix (int row, int col, int num[], matrix& mat)
  13. {
  14. mat.row = row;
  15. mat.col = col;
  16. mat.data.resize (row * col);
  17. for (int i = 0; i < row * col; i++)
  18. mat.data [i] = num [i];
  19.  
  20. }
  21. friend matrix operator+= (matrix& mat1, matrix mat2);
  22. friend matrix operator-= (matrix& mat1, matrix mat2);
  23. friend matrix operator+= (matrix& mat, int scalar);
  24. friend matrix operator-= (matrix& mat, int scalar);
  25. friend void operator++ (matrix& mat);
  26. friend void operator-- (matrix& mat);
  27. friend istream& operator>> (istream& in, matrix& mat);
  28.  
  29. };
  30. //__________________________________________
  31. matrix operator+= (matrix& mat1, matrix mat2){
  32. if(mat1.col==mat2.col && mat1.row==mat2.row)
  33. {
  34.  
  35. for (int i=0; i<mat1.data.size(); i++)
  36. {
  37. cout<< mat1.data[i]+mat2.data[i];
  38.  
  39. }
  40. }
  41. else
  42. {
  43.  
  44. cout<< "mat1 ,mat2 not the same dimensions .\n";
  45.  
  46. }
  47.  
  48. }
  49. //__________________________________________
  50. matrix operator-= (matrix& mat1, matrix mat2)
  51. {
  52.  
  53. if(mat1.col==mat2.col && mat1.row==mat2.row)
  54. {
  55.  
  56. for (int i=0; i<mat1.data.size(); i++)
  57. {
  58. cout<<mat1.data[i]-mat2.data[i];
  59.  
  60. }
  61. }
  62. else
  63. {
  64.  
  65. cout<< "mat1 ,mat2 not the same dimensions .\n";
  66. }
  67.  
  68. }
  69. //__________________________________________
  70. matrix operator+= (matrix& mat, int scalar)
  71. {
  72. for(int i=0; i<mat.data.size(); i++ )
  73. {
  74.  
  75. cout<< mat.data[i]+scalar;
  76.  
  77. }
  78.  
  79. }
  80. //__________________________________________
  81. matrix operator-= (matrix& mat, int scalar)
  82. {
  83. for(int i=0; i<mat.data.size(); i++ )
  84. {
  85.  
  86. cout<< mat.data[i]-scalar;
  87.  
  88.  
  89. }
  90.  
  91. }
  92. //__________________________________________
  93. void operator++ (matrix& mat)
  94. {
  95. for(int i=0; i<mat.data.size(); i++ )
  96. {
  97.  
  98. cout<<mat.data[i]+1<<" ";
  99. }
  100. }
  101. //__________________________________________
  102.  
  103. void operator-- (matrix& mat)
  104. {
  105.  
  106.  
  107. for(int i=0; i<mat.data.size(); i++ )
  108. {
  109.  
  110. cout<<mat.data[i]-1<<" ";
  111.  
  112.  
  113. }
  114.  
  115.  
  116. }
  117. //__________________________________________
  118. istream& operator>> (istream& in, matrix& mat)
  119. {
  120.  
  121. cout<<"enter the row and the collom \n ";
  122. cout<<"row = ";
  123. cin>>mat.row;
  124. cout<<" \ncollom = ";
  125. cin>> mat.col;
  126. mat.data.resize(mat.col*mat.row);
  127. cout<<" \nenter data of matrix \n";
  128. for (int i=0; i<mat.col*mat.row; i++)
  129. {
  130.  
  131. cin>>mat.data[i];
  132.  
  133. }
  134. return in;
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. int main(){
  148. matrix g;
  149. cout<<"choose(1) if we have matrix.... \nchoose (2) if you will enter matrix.... \n choose (3) to know the matrix properties \n==>";
  150. int x,y,z;
  151. cin>>x;
  152.  
  153. matrix mat1, mat2, mat3,mat4;
  154.  
  155. /* if(x==1 || x ==3 )
  156. {
  157.  
  158.  
  159. int i,s1, s2,s3 ;
  160. 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;
  161. cin >> i ;
  162. int data1[] = {1,2,3,4,50,60};
  163. int data2 [] = {1,2,3,4,5,6};
  164. int data3 [] = {10,100,10,100,10,100,10,100};
  165.  
  166. createMatrix (3, 2, data1, mat1);
  167. createMatrix (2,2, data2, mat2);
  168. createMatrix (4, 2, data3, mat3);
  169.  
  170.  
  171.  
  172. if (i==1)
  173. {
  174. mat1 + mat3;
  175. }
  176. if (i==2)
  177. {
  178. mat1 - mat3;
  179. }
  180. if (i==3)
  181. {
  182. matrix sum;
  183. sum=mat1 * mat2;
  184. print(sum);
  185.  
  186. }
  187. if (i==4)
  188. {
  189. cout << "enter your number : "<<endl;
  190. cin >> s1;
  191. mat1 + s1;
  192. }
  193. if (i==5)
  194. {
  195. mat1 - s2;
  196. cout << "enter your number : "<<endl;
  197. cin >> s2;
  198. }
  199. if (i==6)
  200. {
  201. mat1 * s3;
  202. cout << "enter your number : "<<endl;
  203. cin >> s3;
  204. }
  205.  
  206.  
  207. }*/
  208.  
  209. if(x==2)
  210. {
  211. cout<<"what are you want : \n1- matrix operator+= & matrix operator-= \n2- matrix operator+=scalar & matrix operator-=scalar \n3- mat++ & mat--" <<endl;
  212. int y;
  213. cin>>y;
  214. if (y==1)
  215. {
  216. cout<<"enter mat1 : \n";
  217. cin>>mat1;
  218. cout<<"\nenter mat2 : \n";
  219. cin>>mat2;
  220. int c ;
  221. cout << " 1-plus or 2-mainus " <<endl;
  222. cin >> c ;
  223. if (c ==1)
  224. {
  225. cout<<"\nmatrix operator+= :\n";
  226. (mat1+=mat2);
  227. }
  228. else if (c == 2)
  229. {
  230. cout<<"\nmatrix operator-= :\n";
  231. (mat1-=mat2);
  232. }
  233. }
  234. if(y==2)
  235. {
  236. int scalar,c;
  237.  
  238. cout<<"enter mat : \n";
  239. cin>>mat3;
  240. cout<<"\nenter the scalar : ";
  241. cin>>scalar;
  242. cout << " 1-plus or 2-mainus " <<endl;
  243. cin >> c;
  244. if (c ==1)
  245. {
  246. cout<<"\nmatrix operator+=scalar :\n";
  247. (mat3+=scalar);
  248. }
  249. else if (c == 2)
  250. {
  251. cout<<"\nmatrix operator-=scalar :\n";
  252. (mat3-=scalar);
  253. }
  254.  
  255. }
  256. if(y==3)
  257. {
  258. int c ;
  259. cout<<"enter mat : \n";
  260. cin>>mat3;
  261. cout << " 1-plus or 2-mainus " <<endl;
  262. cin >> c ;
  263. if (c == 1 )
  264. {
  265. cout<<"\nmat++ :\n";
  266. (++mat3);
  267. }
  268. else if (c == 2)
  269. {
  270. cout<<"\nmat-- :\n";
  271. (--mat3);
  272. }
  273.  
  274. }
  275.  
  276. }
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286. return 0;
  287.  
  288. }
Add Comment
Please, Sign In to add comment