# include iostream include math using namespace std template typename cl

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165``` ```#include #include using namespace std; template class Matrix { T *data; int N; public: Matrix (char n) { double tmp; cout<<"Input size of martix "<>N; tmp=sqrt(N); if ((int)(tmp*10)%10!=0) throw 2; data=new T[N]; if (N>1) N=sqrt(N); } Matrix(int n) { data=new T[n*n]; N=n; } ~Matrix() { delete[] data; N=0; } T Get(int i, int j) { return data[i*N+j]; } void Set(int i,int j, T d) { data[i*N+j]=d; } Matrix& operator=(Matrix& o) { if (&o==this) return *this; N=o.N; if (data!=NULL) delete[] data; data = new T[N*N]; for (int i=0; idata[i]=a.data[i]+b.data[i]; return *c; } friend Matrix& operator-(Matrix& a, Matrix& b) { if (a.Dim()!=b.Dim()) throw 4; int N=a.Dim(); static Matrix *c; if (&c!=NULL) delete c; c = new Matrix(N); for(int i=0; idata[i]=a.data[i]-b.data[i]; return *c; } friend Matrix& operator*(Matrix& a, Matrix& b) { if (a.Dim()!=b.Dim()) throw 4; int N=a.Dim(); static Matrix *c; if (&c!=NULL) delete c; c = new Matrix(N); for(int i=0; idata[i*N+j]=0; for(int k=0; kdata[i*N+j]+=a.data[i*N+k]*b.data[k*N+j]; } return *c; } void Sum(Matrix &a, Matrix &b) { if (a.Dim()!=b.Dim()) throw 4; for(int i=0; i void Input(Matrix &m) { int i,j,n; T1 d; n=m.Dim(); for(i=0; i a('a'); cout<<"a=\n"; Input(a); Matrix b('b'); cout<<"b=\n"; Input(b); Matrix c(a.Dim()); cout<<"\na=\n"; Print(a); cout<<"b=\n"; Print(b); c=a+b; cout<<"a+b\n"; Print(c); c=a-b; cout<<"a-b\n"; Print(c); c=a*b; cout<<"a*b\n"; Print(c); } catch (int Errcode) { if (Errcode==2) cout<<"Matrix can't have that size"; if (Errcode==4) cout<<"Can't operate with matrix with different sizes"; } catch (...) { cout<<"\nUnknown error"; } } ```