#include<iostream>
#include<cstdlib>
#include<vector>
#include<iomanip>
using namespace std;
namespace a{
template <typename T>
class arrays{
private:
int col,row;
vector<vector<T> > mat;
public:
arrays(){};
arrays<T>(int i,int j){
row=i;
col=j;
mat.resize(row);
for( i=0;i<row;i++){
mat[i].resize(col);
}
}
T& operator()(size_t i,size_t j){
if (i<row&&j<col){
return (mat.at(i).at(j));}
else{
std::cout<<"the
cite write function wrong"<<endl; }
}
const T operator()(size_t i,size_t j) const{
if (i<row&&j<col){
return (mat.at(i).at(j));}
else{
std::cout<<"the
cite read function wrong"<<endl; }
}
friend arrays<T> zeros(int m,int n,T t){
arrays<T> temp(m,n);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
temp(i,j)=0;
}
}
temp.row=m;temp.col=n;
return temp;
}
friend arrays<T> ones(int m,int n,T t){
arrays<T> temp(m,n);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
temp(i,j)=1;
}
}
temp.row=m;temp.col=n;
return temp;
}
friend arrays<T> eyes(int m,T t){
arrays<T> temp(m,m);
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if (i==j){
temp(i,j)=1;}
else
temp(i,j)=0;
}
}
temp.row=m;temp.col=m;
return temp;
}
friend arrays<T>rands(int m,int n,T t){
arrays<T> temp(m,n);
srand((unsigned)time(NULL));
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
temp(i,j)=(T)(rand()/RAND_MAX);
}
}
return temp;
}
void disp(){
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<std::setw(7)<<mat.at(i).at(j);
}
std::cout<<endl;
}
std::cout<<endl;
}
int get_row(){
return(row);}
int get_col(){
return(col);}
friend arrays<T> get_i_row(arrays<T>& temp1,int i){
int row1,col1;
row1=temp1.get_row();col1=temp1.get_col();
if (i<row1){
int row_temp=1;
arrays<T> temp(row_temp,col1);
for(int j=0;j<col1;j++){
temp(0,j)=temp1(i,j);
}
return temp;
}
else
std::cout<<"the index beyound the row rage"<<endl;
}
friend arrays<T> get_i_col(arrays<T>& temp1,int i){
int row1,col1;
row1=temp1.get_row();col1=temp1.get_col();
if (i<col1){
int row_temp=1;
arrays<T> temp(row1,row_temp);
for(int j=0;j<row1;j++){
temp(j,0)=temp1(j,i);
}
return temp;
}
else
std::cout<<"the index beyound the col rage"<<endl;
}
friend arrays<T> operator +(arrays<T>& temp1,arrays<T>& temp2){
int row1,row2,col1,col2;
row1=temp1.get_row();col1=temp1.get_col();
row2=temp2.get_row();col2=temp2.get_col();
if (row1==row2&&col1==col2){
arrays<T> temp(row1,col1);
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
temp(i,j)=temp1(i,j)+temp2(i,j);
}
}
temp.row=row1;temp.col=col1;
return(temp);
}
else{
std::cout<<"the add functon is wrong "<<endl;
}
}
friend arrays<T> operator +(arrays<T>& temp1,T temp2){
int row1,col1;
row1=temp1.get_row();col1=temp1.get_col();
arrays<T> temp(row1,col1);
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
temp(i,j)=temp1(i,j)+temp2;
}
}
temp.row=row1;temp.col=col1;
return(temp);
}
friend arrays<T> operator -(arrays<T>& temp1,T temp2){
int row1,col1;
row1=temp1.get_row();col1=temp1.get_col();
arrays<T> temp(row1,col1);
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
temp(i,j)=temp1(i,j)-temp2;
}
}
temp.row=row1;temp.col=col1;
return(temp);
}
friend arrays<T> operator *(arrays<T>& temp1,T temp2){
int row1,col1;
row1=temp1.get_row();col1=temp1.get_col();
arrays<T> temp(row1,col1);
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
temp(i,j)=temp1(i,j)*temp2;
}
}
temp.row=row1;temp.col=col1;
return(temp);
}
friend arrays<T> operator /(arrays<T>& temp1,T temp2){
int row1,col1;
row1=temp1.get_row();col1=temp1.get_col();
arrays<T> temp(row1,col1);
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
temp(i,j)=temp1(i,j)/temp2;
}
}
temp.row=row1;temp.col=col1;
return(temp);
}
friend arrays<T> operator -(arrays<T>& temp1,arrays<T>& temp2){
int row1,row2,col1,col2;
row1=temp1.get_row();col1=temp1.get_col();
row2=temp2.get_row();col2=temp2.get_col();
if (row1==row2&&col1==col2){
arrays<T> temp(row1,col1);
for(int i=0;i<row1;i++){
for(int j=0;j<col1;j++){
temp.mat.at(i).at(j)=temp1.mat.at(i).at(j)-temp2.mat.at(i).at(j);
}
}
temp.row=row1;temp.col=col1;
return(temp);
}
else{
std::cout<<"the sub function is wrong "<<endl;
}
}
friend arrays<T> operator *(arrays<T>& temp1,arrays<T>& temp2){
int row1,row2,col1,col2;
row1=temp1.get_row();col1=temp1.get_col();
row2=temp2.get_row();col2=temp2.get_col();
if (col1==row2){
arrays<T> temp(row1,col2);
for(int i=0;i<row1;i++){
for(int j=0;j<col2;j++){
arrays<T> temp_1=get_i_row(temp1,i);
arrays<T> temp_2=get_i_col(temp2,j);
int sum=0;
for (int sum_i=0;sum_i<col1;sum_i++){
sum=sum+temp_1(0,sum_i)*temp_2(sum_i,0);
}
temp(i,j)=sum;
}
}
temp.row=row1;temp.col=col2;
return(temp);
}
else{
std::cout<<"the mutil function is wrong "<<endl;
}
}
arrays<T> operator=(arrays<T>& temp1){
arrays<T> temp;
temp.row=temp1.row;
temp.col=temp1.col;
for(int i=0;i<temp1.row;i++){
for(int j=0;j<temp1.col;j++){
temp.mat.at(i).at(j)=temp1.mat.at(i).at(j);
}
}
return(temp);
}
friend arrays<T> copy(arrays<T>& temp1
){
int rows,cols;
rows=temp1.get_row();cols=temp1.get_col();
arrays<T> temp2(rows,cols);
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
temp2(i,j)=temp1(i,j);
}
}
temp2.row=rows;temp2.col=cols;
return(temp2);
}
};
}
int main(){
int col=5,row=6;
a::arrays<int>mat1(row,col);
// int rows=5,cols=6;
a::arrays<double>mat2(row,col);
for (int i=0;i<row;i++){
for (int j=0;j<col;j++){
mat1(i,j)=i*j;
mat2(i,j)=i+j;
}
}
mat1.disp();
mat2.disp();
system("pause");
return 1;
}