Advertisement
Guest User

Matrix.cpp

a guest
May 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. using namespace std;
  5. class MatrixException: public exception {
  6.  
  7.  
  8. string str;
  9. public :
  10. MatrixException (const char* s): str(s){}
  11. void print () const{
  12. cerr << str << endl;
  13. }
  14. ~MatrixException()throw(){}
  15. };
  16.  
  17. template <class T_type>
  18. class Matrix
  19. {
  20. int m;
  21. int n;
  22. T_type** arr;
  23.  
  24. public:
  25. Matrix(){m=0;n=0;arr = NULL;}
  26.  
  27. /**
  28. * конструктор копирования
  29. */
  30.  
  31. Matrix(const Matrix& orig){
  32. n = orig.n;
  33. m = orig.m;
  34. arr = new T_type*[n] ;
  35. for (int i= 0;i< n;++i){
  36. arr[i] = new T_type[m];
  37. for (int j = 0;j< m ;++j)
  38. arr [i][j] = orig.arr[i][j];
  39. }
  40. }
  41.  
  42. /**
  43. * оператор присваивания
  44. */
  45.  
  46. Matrix& operator= (const Matrix& orig){
  47. if (this == &orig) return *this;
  48. if (arr != NULL) {
  49. for (int i = 0 ;i <n ;++i)
  50. delete [] arr[i];
  51. delete[] arr;
  52. }
  53. n = orig.n;
  54. m = orig.m;
  55.  
  56. arr = new T_type*[n] ;
  57. for (int i= 0;i< n;++i){
  58. arr[i] = new T_type[m];
  59. for (int j = 0;j< m ;++j)
  60. arr [i][j] = orig.arr[i][j];
  61. }
  62. return *this;
  63. }
  64.  
  65. /**
  66. * конструктор преобразования - из объекта типа T создается матрица 1x1
  67. */
  68.  
  69. Matrix(const T_type& a){
  70. n = m =1;
  71. arr = new T_type*[1] ;
  72. arr[0] = new T_type[1];
  73. arr [0][0] = a;
  74.  
  75. }
  76.  
  77. /**
  78. * конструктор заполняющий матрицу содержимым текстового файла
  79. */
  80.  
  81. Matrix(const char* s){
  82. ifstream F;
  83. T_type tmp;
  84. F.open(s);
  85. if (!F) throw MatrixException("file not opened");
  86. if (!(F >> n)) throw MatrixException(" no lines num "); // ошибка чтения ,приведение потока к bool
  87. if (!(F >> m)) throw MatrixException(" no col num");
  88. if (!(n > 0 && m > 0) ) throw MatrixException(" incorrect matrix size") ;
  89. arr = new T_type*[n] ;
  90. for (int i= 0;i< n;++i){
  91. arr[i] = new T_type[m];
  92. for (int j = 0;j< m ;++j)
  93. if (!(F >> arr [i][j])) throw " not enough elements" ;
  94.  
  95. }
  96. if (F >> tmp ) throw "extra elements";
  97.  
  98. F.close();
  99.  
  100. }
  101.  
  102. /**
  103. * деструктор
  104. */
  105.  
  106. ~Matrix(){
  107. if (arr == NULL) return ;
  108. for (int i = 0 ;i <n ;++i)
  109. delete [] arr[i];
  110. delete[] arr;
  111. }
  112.  
  113. /**
  114. * транспонирование матрицы
  115. */
  116.  
  117. void Transp (){
  118. T_type t;
  119. T_type** arr1;
  120. int d = n;
  121. //выделение памяти под транспонированную матрицу
  122. arr1 = new T_type*[m] ;
  123. for (int i= 0;i< m;++i){
  124. arr1[i] = new T_type[n];
  125. for (int j = 0;j< n ;++j)
  126. arr1 [i][j] = arr [j][i];
  127.  
  128. }
  129.  
  130. for (int i = 0 ;i <n ;++i)
  131. delete [] arr[i];
  132. delete[] arr;
  133. arr = arr1;
  134. n = m;
  135. m = d; //поменяли местами размеры
  136.  
  137. }
  138.  
  139. friend Matrix operator+ (const Matrix& a,const Matrix& b ){
  140. if ( a.n != b.n || a.m != b.m) throw MatrixException("incorrect size in + operator");
  141. Matrix c(a);
  142. for(int i=0;i < a.n;i++)
  143.  
  144. for(int j=0;j < a.m;j++)
  145.  
  146. c.arr[i][j] += b.arr[i][j];
  147. return c;
  148. }
  149.  
  150. friend Matrix operator- (const Matrix& a,const Matrix& b ){
  151.  
  152. if ( a.n != b.n || a.m != b.m) throw MatrixException("incorrect size in - operator");
  153. Matrix c(a);
  154. for(int i=0;i < a.n;i++)
  155.  
  156. for(int j=0;j < a.m;j++)
  157.  
  158. c.arr[i][j] -= b.arr[i][j];
  159. return c;
  160. }
  161.  
  162. friend Matrix operator* (const Matrix& a,const T_type& b ){
  163.  
  164. Matrix c(a);
  165. for(int i=0; i<c.n; i++)
  166. for(int j=0; j<c.m; j++)
  167. c.arr[i][j] *= b;
  168. return c;
  169.  
  170. }
  171.  
  172. friend Matrix operator* (const T_type& a,const Matrix& b ){
  173. return b*a;
  174. }
  175.  
  176.  
  177. friend Matrix operator* (const Matrix& a,const Matrix& b ){
  178. if (a.m != b.n) throw MatrixException("error in operator *"); // дописать
  179. Matrix c;
  180. int k;
  181. c.n = a.n;
  182. c.m = b.m;
  183. c.arr = new T_type*[c.n] ;
  184. for (int i= 0;i< c.n;++i){
  185. c.arr[i] = new T_type[c.m];
  186. for (int j = 0;j< c.m ;++j)
  187. for( k=0,c.arr [i][j] = 0;k < b.n; k++)
  188. c.arr[i][j] += a.arr[i][k] * b.arr[k][j];
  189. }
  190. return c;
  191.  
  192.  
  193. }
  194.  
  195. /**
  196. * Вывод матрицы на экран:
  197. */
  198.  
  199. friend ostream& operator << (ostream& os,const Matrix& m ){
  200.  
  201. for(int i=0; i < m.n; i++){
  202. for(int j=0; j < m.m; j++)
  203. os << m.arr[i][j] << " ";
  204. os << endl;}
  205. return os;
  206. }
  207.  
  208. };
  209.  
  210.  
  211. ////////////////////////////
  212. // специализация по std : //
  213. ////////////////////////////
  214. template<>
  215. class Matrix<string>
  216. {
  217. int m;
  218. int n;
  219. string** arr;
  220.  
  221. public:
  222. Matrix(){m=0;n=0;arr = NULL;}
  223.  
  224. /**
  225. * конструктор копирования
  226. */
  227.  
  228. Matrix(const Matrix& orig){
  229. n = orig.n;
  230. m = orig.m;
  231. arr = new string*[n] ;
  232. for (int i= 0;i< n;++i){
  233. arr[i] = new string[m];
  234. for (int j = 0;j< m ;++j)
  235. arr [i][j] = orig.arr[i][j];
  236. }
  237. }
  238.  
  239.  
  240. Matrix& operator= (const Matrix& orig){
  241. if (this == &orig) return *this;
  242. if (arr != NULL) {
  243. for (int i = 0 ;i <n ;++i)
  244. delete [] arr[i];
  245. delete[] arr;
  246. }
  247. n = orig.n;
  248. m = orig.m;
  249.  
  250. arr = new string*[n] ;
  251. for (int i= 0;i< n;++i){
  252. arr[i] = new string[m];
  253. for (int j = 0;j< m ;++j)
  254. arr [i][j] = orig.arr[i][j];
  255. }
  256. return *this;
  257. }
  258.  
  259.  
  260.  
  261. /**
  262. * конструктор преобразования - из объекта типа T создается матрица 1x1
  263. */
  264.  
  265. Matrix(const string& a){
  266. n = m =1;
  267. arr = new string*[1] ;
  268. arr[0] = new string[1];
  269. arr [0][0] = a;
  270.  
  271. }
  272.  
  273. /**
  274. * конструктор заполняющий матрицу содержимым текстового файла
  275. */
  276.  
  277. Matrix(const char* s){
  278. ifstream F;
  279. F.open(s);
  280. if (!F) throw "file not opened";
  281. if (!(F >> n)) throw " no lines num "; // ошибка чтения ,приведение потока к bool
  282. if (!(F >> m)) throw " no col num";
  283. if (!(n > 0 && m > 0) ) throw " incorrect matrix size";
  284. arr = new string*[n] ;
  285. for (int i= 0;i< n;++i){
  286. arr[i] = new string[m];
  287. for (int j = 0;j< m ;++j)
  288. if (!(F >> arr [i][j])) throw " not enough elem" ;
  289.  
  290. }
  291.  
  292. F.close();
  293.  
  294. }
  295.  
  296. /**
  297. * деструктор
  298. */
  299.  
  300. ~Matrix(){
  301. if (arr == NULL) return ;
  302. for (int i = 0 ;i <n ;++i)
  303. delete [] arr[i];
  304. delete[] arr;
  305. }
  306.  
  307. /**
  308. * транспонирование матрицы
  309. */
  310.  
  311. void Transp (){
  312. string t;
  313. string** arr1;
  314. int d = n;
  315. //выделение памяти под трансп матр
  316.  
  317. arr1 = new string*[m] ;
  318. for (int i= 0;i< m;++i){
  319. arr1[i] = new string[n];
  320. for (int j = 0;j< n ;++j)
  321. arr1 [i][j] = arr [j][i];
  322.  
  323. }
  324.  
  325. for (int i = 0 ;i <n ;++i)
  326. delete [] arr[i];
  327. delete[] arr;
  328. arr = arr1;
  329. n = m;
  330. m = d; //помен мест разм
  331.  
  332. }
  333.  
  334. friend Matrix operator+ (const Matrix& a,const Matrix& b ){
  335. if ( a.n != b.n || a.m != b.m) throw "incorrect size in + operator";
  336. Matrix c(a);
  337. for(int i=0;i < a.n;i++)
  338.  
  339. for(int j=0;j < a.m;j++)
  340.  
  341. c.arr[i][j] += b.arr[i][j];
  342. return c;
  343. }
  344.  
  345. friend Matrix operator- (const Matrix& a,const Matrix& b ){throw MatrixException ("error");}
  346.  
  347.  
  348. friend Matrix operator* (const Matrix& a,const string& b ){
  349.  
  350. throw MatrixException ("error");
  351.  
  352. }
  353.  
  354. friend Matrix operator* (const string& a,const Matrix& b ){
  355. throw MatrixException ("error");
  356. }
  357.  
  358.  
  359. friend Matrix operator* (const Matrix& a,const Matrix& b ){
  360. throw MatrixException ("error");
  361.  
  362.  
  363. }
  364.  
  365. /**
  366. * Вывод матрицы на экран:
  367. */
  368.  
  369. friend ostream& operator << (ostream& os,const Matrix& m ){
  370.  
  371. for(int i=0; i < m.n; i++){
  372. for(int j=0; j < m.m; j++)
  373. os << m.arr[i][j] << " ";
  374. os << endl;}
  375. return os;
  376. }
  377.  
  378.  
  379. };
  380.  
  381. int main()
  382. { try {
  383. Matrix<int> m("no.txt");
  384. } catch(const char * s){cout << ' ' << "exeption"<< s <<endl;}
  385. }
  386.  
  387. int main1()
  388. {
  389. try{
  390. Matrix<int> A("matr.txt"),E("matr1.txt");
  391. Matrix <string> R("strmatr.txt");
  392. cout << A + E << "***** \n" << A - E ;
  393.  
  394. cout << E*5 << "***** \n" << 7*E;
  395. cout << A * E;
  396. A.Transp();
  397. R.Transp();
  398. cout << R;
  399.  
  400. return 0;
  401. }catch ( const MatrixException& m){m.print();return 1;}
  402. catch (const char* s) {cerr << s;return 1;}
  403.  
  404.  
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement