问答题

请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程proj3,其中声明的DataList类,是一个用于表示数据表的类。sort成员函数的功能是将当前数据表中的元素升序排列。请编写这个sort函数。程序的正确输出应为: 排序前:7,1,3,11,6,9,12,10,8,4,5,2 排序后:1,2,3,4,5,6,7,8,9,10,11,12 要求: 补充编制的内容写在“//**********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[]=NULL); ~DataList(){delete[]d;} int length()const{return len;}//数据表长度(即数据元素的个数) double getElement(int i)const{return d[i];} void sort();//数据表排序 void show()const;//显示数据表};void writeToFile(char*,const DataList&);//main.cpp#include"DataList.h"DataList::DataList(int len,double data[]):len(1en){ d=new double[1en]; for(int i=0;i<len;i++) d[i]=(data==NULL0.0:data[i]);}void DataList::sort(){//数据表排序//**********333**********//**********666**********}void DataList::show()const{//显示数据表 for(int i=0;i<len—1;i++)cout<<d[i]<<","; cout<<d[len—1]<<endl;}int main(){ double s[]={7,1,3,11,6,9,12,10,8,4,5,2}; DataList list(12,s); cout<<"排序前:"; list.show(); list.sort(); cout<<endl<<"排序后:"; list.show(); writeToFile("",list); return 0;}

【参考答案】

正确答案:for(int i=0;i<len;++i) //从头遍历数组d for(int j=i;j<len;++j)......

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

问答题
请使用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; }
问答题
请使用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; }