问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matnx和单位阵UnitMatrix的定义,还有mam函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include<iostream>
using namespace std;
//矩阵基础类,一个抽象类
class MatrixBase
int rows,cols;
public:
MatrixBase(int rows,int cols):rows(rows),cols(cols)
int getRows()constreturn rows;
//矩阵行数
int gatCols()constreturn cols;
//矩阵列数
virtual double getElement(int r,int c)const=0;//取第i个元素的值
void show() const
//分行显示矩阵中所有元素
for(int i=0;i<rows;i++)
cout<<endl;
for(int j=0;j<cols;j++)
//**********found**********
COUt<<______<<" ";


;
//矩阵类
class Matrix:public MatrixBase
double*val;
public:
//**********found**********
Matrix(int rows,int cols,double m[]=NuLL):______
//**********found**********
val=______;
for(int i=0;i<rows* cols;i++)
val[i]=(m==NuLL0.0:m[i]);

~Matrix()delete[]val;
double getElement(int r,int c)constreturn val[r* getCols()+c];
;
//单位阵(主对角线元素都是1,其余元素都是0的方阵)类
class UnitMatrix: public MatrixBase
public:
UnitMatrix(int rows):MatrixBase(rows,rows)
//单位阵行数列数相同
double getElement(int r,int c)
const
//**********found**********
if(______)return 1.0;
return 0.0;

;
int main()
MatrixBase*m;
double d[][5]=1,2,3,4,5,2,3,4,5,6,3,4,5,6,7;
m=new Matrix(3,5,(double*)d);
m->show();
delete m;
cout<<endl;
m=new UnitMatrix(6);
m->show();
delete m;
return 0;

【参考答案】

(A)getElement(i,j)
(B)MatrixBase(rows,cols)
(C)new......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。DataList的重载运算符函数operator+,其功能是求当前数据表与另一个相同长度的数据表之和;即它返回一个数据表,其每个元素等于相应两个数据表对应元素之和。请编写这个operator+函数。程序的正确输出应该是: 两个数据表: 1,2,3,4,5,6 3,4,5,6,7,8 两个数据表之和: 4,6,8,10,12,14 要求: 补充编制的内容写在“ ********333********”与“ ********666********”之间,不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 DataList.h #include <iostream> using namespace std; class Datalist 数据表类 int len; double*d; public: DataList(int len,double data[]= DataList(Datalist &data); int length () constreturn len; double getElement(int i)const return d[i]; Datalist operator+(const DataList&list)const; 两个数据表求和 void show () const; 显示数据表void writeToFile(char*,constDataList&); main.cpp #include Datalist.h DataList::DataList(int len,doubledata[]):len(len) d=new double[len]; for(int i=0;i<len;i++) d[i]=(data==NULL0.0:data[i]); DataList::Datalist(Datalist &data):len(data.len) d=new double[len]; for(int i=0;i<len;i++) d[i]=data.d[i]; DataList DataList::operator+(const Datalist& list)constt 两个数据表求和double*dd=new double [list.length()]; ********333******** ********666******** return DataList(list.length(),dd); void DataList::show()const( 显示数据表 for(int i=0;i<len-1;i++) cout<<d[i]<< , ; cout<<d[len-1]<<endl; int main() double s1[]=1,2,3,4,5,6; double s2[]=3,4,5,6,7,8; DataList list1(6,s1),list2(6,s2); 定义两个数据表对象 cout<< 两个数据表: <<endl; list1.show(); list2.show(); cout<<endl<< 两个数据表之和: <<endl; (list1+list2).show(); writeToFile( ,list1+list2); return 0;