问答题

请使用“答题”菜单或使用VC6打开考生文件夹proj3下的工程proj3,其中声明了SorteList类。这是一个用于表示有序数据表的类,其成员函数insert的功能是将一个数据插入到有序表中,使得该数据表仍保持有序。请编写lnsert函数。程序的正确输出应该是:
1,2,4,5,7,8,10
插入6和3后:
1,2,3,4,5,6,7,8,10
要求:补充编制的内容写在//********333********与//料料料料666********两行之间,不得修改程序的其他部分。
注意:程序最后已经将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//源程序
#include"SortedList.h"
SortedList:: SortedList(int len, double data[]):len(len)
d=new double[len+2];
for(int k=0; k<len; k++) d[k]=(data==NULL0.0:data[k]);
for(int i=0; i<len-l,i++)
int m=i;
for(int j=i; j<len; j++)
if(d[j]<d[m]) m=j;
if(m>i)
double t=d[m];
d[m]=d[i];
d[i]=t;



void SortedList::insert(double data)
//********333********
//********666********

void SortedList:: show () const //显示有序数据表
for(int i=0; i<len-1,i++) cout<<d[i]<<",";
cout<<d[len-1]<<endl;

int main()
double s[]=5,8,1,2,10,4,7;
SortedList list(7,s);
cout<<"插入前:"<<endl;
list.show();
list.insert (6.0);
list.insert(3.0);
cout<<"插入 6和 3后:"<<endl;
list.show();
// writeToFile ("K: \\ b10 \\61000101 \\ ",list) ;
return 0,

【参考答案】

//********333********
int n=len:
d[n]=data;
f......

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

问答题
请使用“答题”菜单或使用VC6打开考生文件夹proj2下的工程proj2。其中有向量基类VectorBase、向量类Vector和零向量类ZeroVector的定义。请在程序中的画线处填写适当代码,然后删除横线,以实现上述定义。此程序的正确输出结果应为: (1,2,3,4,5) (0,0,0,0,0,0) 注意:只能在画线处填写适当的代码,不要改动程序中的其他内容,也不能删除或移动 ************found************。 源程序 #include<iostream> using namespace std; class VectorBase 向量基类,一个抽象类 int len, public: VectorBase(int len): len(len) int length() const return len; 向量长度,即向量中元素的个数 virtual double getElement(int i) const=0; 取第i个元素的值 virtual double sum() const_0; 求所有元素的和 void show() const 显示向量中所有元素 cout<< ( ; for(int i=0;i<length()-1;i++) cout<<getElement(i)<< , ; ************found************ cout<<________<< ) <<endl; 显示最后一个元素 ; class Vector: public VectorBase 向量类 double*val, public: Vector(int len, double v[]=NULL): VectorBase(len) val=new double[len]; for(int i=0; i<len, i++) val[i]=(v==NULL0.0:v[i]); ************found************ ~Vector()______; double getElement(int index) const return val[index]; double sum() const double s=0.0: ************found************ for(int i=0; i<length();i++)______; return s: ; class ZeroVector: public VectorBase 零向量类 public: ZeroVector(int len): VectorBase(len) ************found************ double getElement(int index) const______; double sum()const return 0.0, ; int main() VectorBase*v: double d[]=1,2,3,4,5; v=new Vector(5,d); v->show(); delete v: v=new ZeroVector(6); v->show(); delete v: return 0: