Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <fstream>
- using namespace std;
- class MatrixException: public exception {
- string str;
- public :
- MatrixException (const char* s): str(s){}
- void print () const{
- cerr << str << endl;
- }
- ~MatrixException()throw(){}
- };
- template <class T_type>
- class Matrix
- {
- int m;
- int n;
- T_type** arr;
- public:
- Matrix(){m=0;n=0;arr = NULL;}
- /**
- * конструктор копирования
- */
- Matrix(const Matrix& orig){
- n = orig.n;
- m = orig.m;
- arr = new T_type*[n] ;
- for (int i= 0;i< n;++i){
- arr[i] = new T_type[m];
- for (int j = 0;j< m ;++j)
- arr [i][j] = orig.arr[i][j];
- }
- }
- /**
- * оператор присваивания
- */
- Matrix& operator= (const Matrix& orig){
- if (this == &orig) return *this;
- if (arr != NULL) {
- for (int i = 0 ;i <n ;++i)
- delete [] arr[i];
- delete[] arr;
- }
- n = orig.n;
- m = orig.m;
- arr = new T_type*[n] ;
- for (int i= 0;i< n;++i){
- arr[i] = new T_type[m];
- for (int j = 0;j< m ;++j)
- arr [i][j] = orig.arr[i][j];
- }
- return *this;
- }
- /**
- * конструктор преобразования - из объекта типа T создается матрица 1x1
- */
- Matrix(const T_type& a){
- n = m =1;
- arr = new T_type*[1] ;
- arr[0] = new T_type[1];
- arr [0][0] = a;
- }
- /**
- * конструктор заполняющий матрицу содержимым текстового файла
- */
- Matrix(const char* s){
- ifstream F;
- T_type tmp;
- F.open(s);
- if (!F) throw MatrixException("file not opened");
- if (!(F >> n)) throw MatrixException(" no lines num "); // ошибка чтения ,приведение потока к bool
- if (!(F >> m)) throw MatrixException(" no col num");
- if (!(n > 0 && m > 0) ) throw MatrixException(" incorrect matrix size") ;
- arr = new T_type*[n] ;
- for (int i= 0;i< n;++i){
- arr[i] = new T_type[m];
- for (int j = 0;j< m ;++j)
- if (!(F >> arr [i][j])) throw " not enough elements" ;
- }
- if (F >> tmp ) throw "extra elements";
- F.close();
- }
- /**
- * деструктор
- */
- ~Matrix(){
- if (arr == NULL) return ;
- for (int i = 0 ;i <n ;++i)
- delete [] arr[i];
- delete[] arr;
- }
- /**
- * транспонирование матрицы
- */
- void Transp (){
- T_type t;
- T_type** arr1;
- int d = n;
- //выделение памяти под транспонированную матрицу
- arr1 = new T_type*[m] ;
- for (int i= 0;i< m;++i){
- arr1[i] = new T_type[n];
- for (int j = 0;j< n ;++j)
- arr1 [i][j] = arr [j][i];
- }
- for (int i = 0 ;i <n ;++i)
- delete [] arr[i];
- delete[] arr;
- arr = arr1;
- n = m;
- m = d; //поменяли местами размеры
- }
- friend Matrix operator+ (const Matrix& a,const Matrix& b ){
- if ( a.n != b.n || a.m != b.m) throw MatrixException("incorrect size in + operator");
- Matrix c(a);
- for(int i=0;i < a.n;i++)
- for(int j=0;j < a.m;j++)
- c.arr[i][j] += b.arr[i][j];
- return c;
- }
- friend Matrix operator- (const Matrix& a,const Matrix& b ){
- if ( a.n != b.n || a.m != b.m) throw MatrixException("incorrect size in - operator");
- Matrix c(a);
- for(int i=0;i < a.n;i++)
- for(int j=0;j < a.m;j++)
- c.arr[i][j] -= b.arr[i][j];
- return c;
- }
- friend Matrix operator* (const Matrix& a,const T_type& b ){
- Matrix c(a);
- for(int i=0; i<c.n; i++)
- for(int j=0; j<c.m; j++)
- c.arr[i][j] *= b;
- return c;
- }
- friend Matrix operator* (const T_type& a,const Matrix& b ){
- return b*a;
- }
- friend Matrix operator* (const Matrix& a,const Matrix& b ){
- if (a.m != b.n) throw MatrixException("error in operator *"); // дописать
- Matrix c;
- int k;
- c.n = a.n;
- c.m = b.m;
- c.arr = new T_type*[c.n] ;
- for (int i= 0;i< c.n;++i){
- c.arr[i] = new T_type[c.m];
- for (int j = 0;j< c.m ;++j)
- for( k=0,c.arr [i][j] = 0;k < b.n; k++)
- c.arr[i][j] += a.arr[i][k] * b.arr[k][j];
- }
- return c;
- }
- /**
- * Вывод матрицы на экран:
- */
- friend ostream& operator << (ostream& os,const Matrix& m ){
- for(int i=0; i < m.n; i++){
- for(int j=0; j < m.m; j++)
- os << m.arr[i][j] << " ";
- os << endl;}
- return os;
- }
- };
- ////////////////////////////
- // специализация по std : //
- ////////////////////////////
- template<>
- class Matrix<string>
- {
- int m;
- int n;
- string** arr;
- public:
- Matrix(){m=0;n=0;arr = NULL;}
- /**
- * конструктор копирования
- */
- Matrix(const Matrix& orig){
- n = orig.n;
- m = orig.m;
- arr = new string*[n] ;
- for (int i= 0;i< n;++i){
- arr[i] = new string[m];
- for (int j = 0;j< m ;++j)
- arr [i][j] = orig.arr[i][j];
- }
- }
- Matrix& operator= (const Matrix& orig){
- if (this == &orig) return *this;
- if (arr != NULL) {
- for (int i = 0 ;i <n ;++i)
- delete [] arr[i];
- delete[] arr;
- }
- n = orig.n;
- m = orig.m;
- arr = new string*[n] ;
- for (int i= 0;i< n;++i){
- arr[i] = new string[m];
- for (int j = 0;j< m ;++j)
- arr [i][j] = orig.arr[i][j];
- }
- return *this;
- }
- /**
- * конструктор преобразования - из объекта типа T создается матрица 1x1
- */
- Matrix(const string& a){
- n = m =1;
- arr = new string*[1] ;
- arr[0] = new string[1];
- arr [0][0] = a;
- }
- /**
- * конструктор заполняющий матрицу содержимым текстового файла
- */
- Matrix(const char* s){
- ifstream F;
- F.open(s);
- if (!F) throw "file not opened";
- if (!(F >> n)) throw " no lines num "; // ошибка чтения ,приведение потока к bool
- if (!(F >> m)) throw " no col num";
- if (!(n > 0 && m > 0) ) throw " incorrect matrix size";
- arr = new string*[n] ;
- for (int i= 0;i< n;++i){
- arr[i] = new string[m];
- for (int j = 0;j< m ;++j)
- if (!(F >> arr [i][j])) throw " not enough elem" ;
- }
- F.close();
- }
- /**
- * деструктор
- */
- ~Matrix(){
- if (arr == NULL) return ;
- for (int i = 0 ;i <n ;++i)
- delete [] arr[i];
- delete[] arr;
- }
- /**
- * транспонирование матрицы
- */
- void Transp (){
- string t;
- string** arr1;
- int d = n;
- //выделение памяти под трансп матр
- arr1 = new string*[m] ;
- for (int i= 0;i< m;++i){
- arr1[i] = new string[n];
- for (int j = 0;j< n ;++j)
- arr1 [i][j] = arr [j][i];
- }
- for (int i = 0 ;i <n ;++i)
- delete [] arr[i];
- delete[] arr;
- arr = arr1;
- n = m;
- m = d; //помен мест разм
- }
- friend Matrix operator+ (const Matrix& a,const Matrix& b ){
- if ( a.n != b.n || a.m != b.m) throw "incorrect size in + operator";
- Matrix c(a);
- for(int i=0;i < a.n;i++)
- for(int j=0;j < a.m;j++)
- c.arr[i][j] += b.arr[i][j];
- return c;
- }
- friend Matrix operator- (const Matrix& a,const Matrix& b ){throw MatrixException ("error");}
- friend Matrix operator* (const Matrix& a,const string& b ){
- throw MatrixException ("error");
- }
- friend Matrix operator* (const string& a,const Matrix& b ){
- throw MatrixException ("error");
- }
- friend Matrix operator* (const Matrix& a,const Matrix& b ){
- throw MatrixException ("error");
- }
- /**
- * Вывод матрицы на экран:
- */
- friend ostream& operator << (ostream& os,const Matrix& m ){
- for(int i=0; i < m.n; i++){
- for(int j=0; j < m.m; j++)
- os << m.arr[i][j] << " ";
- os << endl;}
- return os;
- }
- };
- int main()
- { try {
- Matrix<int> m("no.txt");
- } catch(const char * s){cout << ' ' << "exeption"<< s <<endl;}
- }
- int main1()
- {
- try{
- Matrix<int> A("matr.txt"),E("matr1.txt");
- Matrix <string> R("strmatr.txt");
- cout << A + E << "***** \n" << A - E ;
- cout << E*5 << "***** \n" << 7*E;
- cout << A * E;
- A.Transp();
- R.Transp();
- cout << R;
- return 0;
- }catch ( const MatrixException& m){m.print();return 1;}
- catch (const char* s) {cerr << s;return 1;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement