课程首页地址:http://blog.csdn.net/sxhelijian/article/details/7910565,本周题目链接:http://blog.csdn.net/sxhelijian/article/details/8841620
【项目5(选做)-二维数组类Douary】建立一个二维数组类Douary,使该类中有以下数据成员、成员函数及友员函数,完成矩阵的输入、输出、加、减、相等判断等操作。
class Douary { public: Douary(int m, int n);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为0 Douary::Douary(const Douary &d); //因为有指针数据成员,复制构造函数需要定义 ~Douary(); //析构函数:用于释放动态数组所占用的存储空间。 friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组 friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列形式输出 friend Douary operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加 friend Douary operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减 bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等 private: int *Array; //Array 为动态数组指针,构造函数中分配空间用Array = new int[row*col]; int row; //row 为二维数组的行数。 int col; //col 为二维数组的列数。 } int main() { Douary d1(2,3),d2(2,3) cout<<"输入d1:"<<endl; cin>>d1; cout<<"输入d2:"<<endl; cin>>d2; coutt<<"d1="<<endl; cout<<d1; coutt<<"d2="<<endl; cout<<d2; coutt<<"d1+d2="<<endl; cout<<(d1+d2); coutt<<"d1-d2="<<endl; cout<<(d1-d2); cout<<"d1"<<((d1==d2)?"==":"!=")<<"d2"<<endl; return 0; }
参考解答:
#include <iostream> using namespace std; class Douary { public: Douary(int m, int n);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为 Douary(const Douary &d);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为 ~Douary(); //析构函数:用于释放动态数组所占用的存储空间 friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组,其中d为Dousry类对象; friend ostream &operator<<(ostream &output, Douary &d);//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。 friend Douary operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加 friend Douary operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减 bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等 private: int * Array; //Array 为动态数组指针。 int row; //row 为二维数组的行数。 int col; //col 为二维数组的列数。 }; Douary::Douary(int m, int n) //构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为 { row=m; col=n; Array = new int[m*n]; for(int i=0; i<row; ++i) for(int j=0; j<col; ++j) Array[i*col+j]=0; } Douary::Douary(const Douary &d) { row=d.row; col=d.col; Array = new int[row*col]; for(int i=0; i<row; ++i) for(int j=0; j<col; ++j) Array[i*col+j]=d.Array[i*col+j]; } Douary::~Douary() //析构函数:用于释放动态数组所占用的存储空间 { delete [] Array; } istream &operator>>(istream &input, Douary &d)//重载运算符“>>”输入二维数组,其中d为Dousry类对象 { for(int i=0; i<d.row; ++i) for(int j=0; j<d.col; ++j) cin>>d.Array[i*d.col+j]; return input; } ostream &operator<<(ostream &output, Douary &d)//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象 { for(int i=0; i<d.row; ++i) { for(int j=0; j<d.col; ++j) cout<<d.Array[i*d.col+j]<<"\t"; cout<<endl; } cout<<endl; return output; } Douary operator+(const Douary &d1,const Douary &d2)//两个矩阵相加,规则:对应位置上的元素相加 { //在此可以先判断d1和d2的行列是否相同,如果不相同可以报错退出,不做运算。本参考解答忽略了这一前提 Douary d(d1.row,d1.col); for(int i=0; i<d1.row; ++i) { for(int j=0; j<d1.col; ++j) d.Array[i*d1.col+j]=d1.Array[i*d1.col+j]+d2.Array[i*d1.col+j]; } return d; } Douary operator-(const Douary &d1,const Douary &d2)//两个矩阵相减,规则:对应位置上的元素相减 { //在此可以先判断d1和d2的行列是否相同,如果不相同可以报错退出,不做运算。本参考解答忽略了这一前提 Douary d(d1.row,d1.col); for(int i=0; i<d1.row; ++i) { for(int j=0; j<d1.col; ++j) d.Array[i*d1.col+j]=d1.Array[i*d1.col+j]-d2.Array[i*d1.col+j]; } return d; } bool Douary::operator ==(const Douary &d)//判断两个矩阵是否相等,即对应位置上的所有元素是否相等 { if(row!=d.row||col!=d.col) return false; bool eq = true; for(int i=0; i<row; ++i) { for(int j=0; j<col; ++j) if (Array[i*col+j]!=d.Array[i*col+j]) { eq=false; break; } if (!eq) break; } return eq; } int main() { Douary d1(2,3),d2(2,3); cout<<"输入d1(2,3):"<<endl; cin>>d1; cout<<"输入d2(2,3):"<<endl; cin>>d2; cout<<"d1="<<endl; cout<<d1; cout<<"d2="<<endl; cout<<d2; cout<<"d1+d2="<<endl; Douary d3=(d1+d2); cout<<d3; cout<<"d1-d2="<<endl; d3=(d1-d2); cout<<d3; cout<<"d1"<<((d1==d2)?"==":"!=")<<"d2"<<endl; return 0; }
调试记录:
当main()中改为如下时能对。
cout<<"d1+d2="<<endl; Douary d3=(d1+d2); cout<<d3; cout<<"d1-d2="<<endl; Douary d4=(d1-d2); cout<<d4;综合判断问题还是出在复制构造函数中,当对象已经存在,要给其重新赋值时。
2014.4.20修改:
#include <iostream> using namespace std; class Douary { public: Douary(int m, int n);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为 Douary(const Douary &d);//构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为 ~Douary(); //析构函数:用于释放动态数组所占用的存储空间 friend istream &operator>>(istream &input, Douary &d);//重载运算符“>>”输入二维数组,其中d为Dousry类对象; friend ostream &operator<<(ostream &output, const Douary &d);//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象。 friend Douary operator+(const Douary &d1,const Douary &d2);//两个矩阵相加,规则:对应位置上的元素相加 friend Douary operator-(const Douary &d1,const Douary &d2);//两个矩阵相减,规则:对应位置上的元素相减 bool operator==(const Douary &d);//判断两个矩阵是否相等,即对应位置上的所有元素是否相等 private: int *Array; //Array 为动态数组指针。 int row; //row 为二维数组的行数。 int col; //col 为二维数组的列数。 }; Douary::Douary(int m, int n) //构造函数:用于建立动态数组存放m行n列的二维数组(矩阵)元素,并将该数组元素初始化为 { row=m; col=n; Array = new int[m*n]; for(int i=0; i<row; ++i) for(int j=0; j<col; ++j) Array[i*col+j]=0; } Douary::Douary(const Douary &d) { row=d.row; col=d.col; if(Array!=NULL) delete []Array; Array = new int[row*col]; for(int i=0; i<row; ++i) for(int j=0; j<col; ++j) Array[i*col+j]=d.Array[i*col+j]; } Douary::~Douary() //析构函数:用于释放动态数组所占用的存储空间 { delete [] Array; } istream &operator>>(istream &input, Douary &d)//重载运算符“>>”输入二维数组,其中d为Dousry类对象 { cout<<d.row<<"行"<<d.col<<"列矩阵: "<<endl; for(int i=0; i<d.row; ++i) for(int j=0; j<d.col; ++j) cin>>d.Array[i*d.col+j]; return input; } ostream &operator<<(ostream &output, const Douary &d)//重载运算符“<<”以m行n列矩阵的形式输出二维数组,其中d为Douary类对象 { for(int i=0; i<d.row; ++i) { for(int j=0; j<d.col; ++j) cout<<d.Array[i*d.col+j]<<"\t"; cout<<endl; } cout<<endl; return output; } Douary operator+(const Douary &d1,const Douary &d2)//两个矩阵相加,规则:对应位置上的元素相加 { //在此可以先判断d1和d2的行列是否相同,如果不相同可以报错退出,不做运算。本参考解答忽略了这一前提 Douary d(d1.row,d1.col); for(int i=0; i<d1.row; ++i) { for(int j=0; j<d1.col; ++j) d.Array[i*d1.col+j]=d1.Array[i*d1.col+j]+d2.Array[i*d1.col+j]; } return d; } Douary operator-(const Douary &d1,const Douary &d2)//两个矩阵相减,规则:对应位置上的元素相减 { //在此可以先判断d1和d2的行列是否相同,如果不相同可以报错退出,不做运算。本参考解答忽略了这一前提 Douary d(d1.row,d1.col); for(int i=0; i<d1.row; ++i) { for(int j=0; j<d1.col; ++j) d.Array[i*d.col+j]=d1.Array[i*d1.col+j]-d2.Array[i*d2.col+j]; } return d; } bool Douary::operator ==(const Douary &d)//判断两个矩阵是否相等,即对应位置上的所有元素是否相等 { if(row!=d.row||col!=d.col) return false; bool eq = true; for(int i=0; i<row; ++i) { for(int j=0; j<col; ++j) if (Array[i*col+j]!=d.Array[i*col+j]) { eq=false; break; } if (!eq) break; } return eq; } int main() { Douary d1(2,3),d2(2,3); cout<<"输入d1——"; cin>>d1; cout<<"输入d2——"; cin>>d2; cout<<"d1="<<endl; cout<<d1; cout<<"d2="<<endl; cout<<d2; cout<<"d1+d2="<<endl<<d1+d2<<endl; cout<<"d1-d2="<<endl<<d1-d2<<endl; cout<<"d1"<<((d1==d2)?"==":"!=")<<"d2"<<endl; return 0; }