问答题

请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2,其中有矩阵基类MatrixBase、矩阵类Matrix和单位阵UnitMatrix的定义,还有main函数的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 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 lows,int cols):rows(rows),cols(cols){} int getRows()const{return rows;} //矩阵行数 int getCols()const{return cols;}//矩阵列数 virtual double getElement(int r,int c)const=0;//取第i个元素的值 void show()const{//分行显示矩阵中所有元素 for(int i=0;i<lows;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<lows*cols;i++) val[i]=(m==NULL0.0:m[i]); } ~Matrix(){delete[]val;} double getElement(int r,int c)const{return 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; }

【参考答案】

正确答案:(1)getElement(i,j) (2)MatrixBase(rows,eols) (3)new dou......

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

问答题
请使用VC6或使用【答题】菜单打开考生文件夹proj1下的工程proj1,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: 书名:C++语句程序设计总页数:299 已把“C++语言程序设计”翻到第50页 已把“C++语言程序设计”翻到第51页 已把“C++语言程序设计”翻到第52页 已把“C++语言程序设计”翻到第51页 已把书合上。 当前页:0 注意:只修改每个“//ERROR****found****”下的那一行,不要改动程序中的其他内容。 #include<iostremn> using namespace std; class Book{ char*title; int num_pages;//页数 int cur_page;//当前打开页面的页码,0表示书未打开 public: //ERROR **********found********** Book(const char*theTitle,int pages)num_pages(pages) { tide=new char[strlen(theTitle)+1]; strcpy(title,theTitle); cout<<endl<< 书名: <<tide << 总页数: <<num_pages: } ~Book(){delete[]title;} bool isClosed()const{return cur_page==0;}//书合上时返回true,否则返回false bed isOpen()const{return ! isClosed();}//书打开时返回true,否则返回fasle int numOfPages()const{return num_pages;}//返回书的页数 int currentPage()const{return cur_page;}//返回打开页面的页码 //ERROR **********found********** void openAtPage(int page_no)const{ //把书翻到指定页 cout<<endl: if(page_no<1 || page_no>num_pages){ cout<< 无法翻到第 <<cur_page<< 页。 ; close(); } else{ cur_page=Page_no; cout<< 已把“ <<title<< ”翻到第 <<cur_page<< 页 ; } } void openAtPrevPage(){openAtPage(cur_page-1);}//把书翻到上一页 void openAtNextPage(){openAtPage(cur_page+1);}//把书翻到下一页 void close(){ //把书合上 cout<<endl: if(isClosed()) cout<< 书是合上的。 ; else{ //ERROR **********found********** Bum_pages=0; cout<< 已把书合上。 ; } cout<<endl: } }; int main(){ Book book( C++语言程序设计 ,299); book.openAtPage(50); book.openAtNextPage(); book.openAtNextPage(); book.openAtPrevPage(); book.close(); cout<< 当前页: <<book.currentPage()<<endl; return 0; }