#include<iostream>
#include<string> using namespace std; template <class T>
class juzhen {
public:
juzhen();
juzhen(int i);
juzhen(juzhen<T>& another);
~juzhen();
void set();
juzhen<T> operator + (const juzhen<T> &another);
juzhen<T> operator * (const juzhen<T> &another);
juzhen<T> transpose();
juzhen<T>& operator = (const juzhen<T> &another);
void output();
private:
T **array;
int n;
}; template<class T>
juzhen<T>::juzhen()
{
array = new T*[1000];
for (int i=0;i<1000;i++)
array[i]= new T[1000];
n=1000;
}
template<class T>
juzhen<T>::juzhen(int i)
{
array = new T*[i];
for (int j=0;j<i;j++)
array[j]= new T[i];
n=i;
}
template<class T>
juzhen<T>::juzhen(juzhen<T>& another)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
array[i][j]=another.array[i][j];
n=another.n;
} template<class T>
juzhen<T>::~juzhen()
{
for (int i=0;i<n;i++)
delete []array[i];
delete []array;
}
template<class T>
void juzhen<T>::set()
{
cout << "Please enter the integers of your matrix"<<endl;
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
cin>>array[j][k];
}
}
template<class T>
juzhen<T> juzhen<T>::operator +(const juzhen<T> &another)
{
juzhen<T> C(n);
for(int i=0;i<n;i++)
for (int j=0;j<n;j++)
C.array[i][j]=array[i][j]+another.array[i][j];
return C;
}
template<class T>
juzhen<T> juzhen<T>::operator *(const juzhen<T> &another)
{
juzhen<T> C(n);
for(int i=0;i<n;i++)
for (int j=0;j<n;j++)
for (int k=0;k<n;k++)
C.array[i][j]+=array[i][k]*another.array[k][j];
return C;
}
template<class T>
juzhen<T> juzhen<T>::transpose()
{
juzhen<T> C(n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
C.array[i][j]=array[j][i];
return C;
}
template<class T>
juzhen<T>& juzhen<T>::operator =(const juzhen<T>& another)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
array[i][j]=another.array[i][j];
n=another.n;
return *this;
} template<class T>
void juzhen<T>::output()
{
cout<<"The answer is"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<array[i][j]<<" ";
cout<<endl;
}
} int main()
{
int number1,number2;
string judge1,judge2;
char choice;
do
{
cout <<"Enter the n of your n*n square matrix"<<endl;
cin>>number1;
juzhen<int> A(number1);
A.set();
do
{
cout<<"What do you want to do?Enter the following number."<<endl<<"1.+ 2.* 3.transpose"<<endl;
cin>>choice;
if(choice=='1')
{
cout << "Please define another matrix. Enter the n" <<endl;
cin>> number2;
if(number2!=number1)
{
cout<<"Invalid"<<endl;
continue;
}
juzhen<int> B(number2),C(number2);
B.set();
C=A+B;
C.output();
}
else if(choice=='2')
{
cout << "Please define another matrix. Enter the n" <<endl;
cin >> number2;
if(number2!=number1)
{
cout<<"Invalid"<<endl;
continue;
}
juzhen<int> B(number2),C(number2);
B.set();
C=A*B;
C.output();
}
else if(choice=='3')
{
juzhen<int> C(number1);
C=A.transpose();
C.output();
}
else
{
cout <<"Invalid"<<endl;
}
cout<<"Do you want to do another algorithm?(Y to continue)";
cin>>judge1;
}while(judge1=="Y");
cout<<"Do you want to enter another matrix?(Y to rewrite,else to exit)";
cin>>judge2;
}while(judge2=="Y");
return 0;
}
#include<string> using namespace std; template <class T>
class juzhen {
public:
juzhen();
juzhen(int i);
juzhen(juzhen<T>& another);
~juzhen();
void set();
juzhen<T> operator + (const juzhen<T> &another);
juzhen<T> operator * (const juzhen<T> &another);
juzhen<T> transpose();
juzhen<T>& operator = (const juzhen<T> &another);
void output();
private:
T **array;
int n;
}; template<class T>
juzhen<T>::juzhen()
{
array = new T*[1000];
for (int i=0;i<1000;i++)
array[i]= new T[1000];
n=1000;
}
template<class T>
juzhen<T>::juzhen(int i)
{
array = new T*[i];
for (int j=0;j<i;j++)
array[j]= new T[i];
n=i;
}
template<class T>
juzhen<T>::juzhen(juzhen<T>& another)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
array[i][j]=another.array[i][j];
n=another.n;
} template<class T>
juzhen<T>::~juzhen()
{
for (int i=0;i<n;i++)
delete []array[i];
delete []array;
}
template<class T>
void juzhen<T>::set()
{
cout << "Please enter the integers of your matrix"<<endl;
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
cin>>array[j][k];
}
}
template<class T>
juzhen<T> juzhen<T>::operator +(const juzhen<T> &another)
{
juzhen<T> C(n);
for(int i=0;i<n;i++)
for (int j=0;j<n;j++)
C.array[i][j]=array[i][j]+another.array[i][j];
return C;
}
template<class T>
juzhen<T> juzhen<T>::operator *(const juzhen<T> &another)
{
juzhen<T> C(n);
for(int i=0;i<n;i++)
for (int j=0;j<n;j++)
for (int k=0;k<n;k++)
C.array[i][j]+=array[i][k]*another.array[k][j];
return C;
}
template<class T>
juzhen<T> juzhen<T>::transpose()
{
juzhen<T> C(n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
C.array[i][j]=array[j][i];
return C;
}
template<class T>
juzhen<T>& juzhen<T>::operator =(const juzhen<T>& another)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
array[i][j]=another.array[i][j];
n=another.n;
return *this;
} template<class T>
void juzhen<T>::output()
{
cout<<"The answer is"<<endl;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<array[i][j]<<" ";
cout<<endl;
}
} int main()
{
int number1,number2;
string judge1,judge2;
char choice;
do
{
cout <<"Enter the n of your n*n square matrix"<<endl;
cin>>number1;
juzhen<int> A(number1);
A.set();
do
{
cout<<"What do you want to do?Enter the following number."<<endl<<"1.+ 2.* 3.transpose"<<endl;
cin>>choice;
if(choice=='1')
{
cout << "Please define another matrix. Enter the n" <<endl;
cin>> number2;
if(number2!=number1)
{
cout<<"Invalid"<<endl;
continue;
}
juzhen<int> B(number2),C(number2);
B.set();
C=A+B;
C.output();
}
else if(choice=='2')
{
cout << "Please define another matrix. Enter the n" <<endl;
cin >> number2;
if(number2!=number1)
{
cout<<"Invalid"<<endl;
continue;
}
juzhen<int> B(number2),C(number2);
B.set();
C=A*B;
C.output();
}
else if(choice=='3')
{
juzhen<int> C(number1);
C=A.transpose();
C.output();
}
else
{
cout <<"Invalid"<<endl;
}
cout<<"Do you want to do another algorithm?(Y to continue)";
cin>>judge1;
}while(judge1=="Y");
cout<<"Do you want to enter another matrix?(Y to rewrite,else to exit)";
cin>>judge2;
}while(judge2=="Y");
return 0;
}