问答题

请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1。其中有线段类Line的定义。程序中位于每个“//ERROR ****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
End point 1=(1,8),End point 2=(5,2),length=7.2111。
注意:只修改每个“//ERROR ****found****”下的那一行,不要改动程序中的其他内容。
#include <iostream>
#include <cmath>
using namespace std;
class Line;
double length(Line);
class Line //线段类
double x1,y1; //线段端点1
double x2,y2; //线段端点2
public:
//ERROR **********found**********
Linedouble x1,double y1,doublex2,double y2)const
this->x1=x1;
this->y1=y1;
this->x2=x2;
this->y2=y2;

double getX1()constreturn x1;
double getY1()constreturn y1;
double getX2()constreturn x2;
double getY2()constreturn y2;
void show () const
cout<<"End point 1=("<<x1<<","<<y1;
cout<<"),End point 2=("<<x2<<","<<y2;
//ERROR ********found********
cout<<"),length="<<length(this)
<<"。"<<endl;

;
double length (Line 1)
//ERROR ********found********
return sqrt((1.x1-1.x2)*(1.x1-1.x2)+(1.y1-1.y2)*(1.y1-1.y2));

int main ()
Line r1(1.0,8.0,5.0,2.0);
r1.show();
return 0;

【参考答案】

(A)Line(double xA,double yA,double xB,double yB){
(B)co......

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

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj3下的工程proj3,其中声明了SortedList类,是一个用于表示有序数据表的类。其成员函数insert的功能是将一个数据插入到一个有序表中,使得该数据表仍然保持有序。请编写这个insert函数。程序的正确输出应为: 插入前: 1,2,4,5,7,8,10 插入6和3后: 1,2,3,4,5,6,7,8,10 要求: 补充编制的内容写在“ ********333********”与“ ********666********”之间。不得修改程序的其他部分。 注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。 SortedList.h #include<iostream> using namespace std; class SortedList 有序数据表类 int len; double*d; public: SortedList(int len,double data[]=NULL); ~SortedList()delete[]d; int length()constreturn len; 有序数据表长度(即元素的个数) double getElement(int i)constreturn d[i]; void insert(double data); void show()const; 显示有序数据表 ; void writeToFile(char*,const Sort-edlist &); main.cpp #include SortedList.h SortedList::SortedList (int len,double data[]):len(len) d=new double[len]; for(int k=0;k<len;k++) d[k]=(data==NULL0.0:data[k]); for(int i=0;i<len-1;i++) int m=i; for(int j=i;j<len;j++) if(d[j]<d[m])m=j; 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); list.show(); writeToFile( ,list); return 0;
问答题
请使用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()constreturn 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 double getElement(int index)const______; double sum()constreturn 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;