阅读程序。阅读下列C++程序,对每条语句进行注释,说明其作用(请粘贴源代码或屏幕截图,不要上传附件
【说明】
①在类体中添加函数move(double ax,double ay)的定义,使得点的坐标x和y分别移动 ax和ay个单位。
②在类定义外完成重载的两个构造函数CPosition()和CPosition(double dx,double dy),其中前者为不带参数的构造函数,使CPosition对象的默认值为x=0,y=0,后者为带参数的构造函数,把数据成员x和y分别初始化为参数dx和dy的值。
③完成函数double distance(double bx,double by)的定义,该函数返回*this和点(bx, by)的距离。
注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。
源程序文件test5.cpp清单如下:
include<iostream.h>
include<math.h>
class CPosition
{
public:
CPosition();
CPosition(double dx,double dy);
double getx();
double gety();
(1)
double distance(double bx,double by);
private:
double x;
double y;
};
(2)
{
x=0;y=0;
}
CPosition::CPosition(doub,e dx,doub,e dy)
{
x=dx; y=dy;
}
double CPosition::getx()
{
return x;
}
double CPosition::gety()
{
return y;
}
double CPosition::distance(double bx,double by)
{
(3)
}
vold main()
{
double a,b;
cout<<"|nput x,y position of a point:";
cin >> a >> b;
CPosition psA(a,b);
cout<<"Input x,y position of another point:";
cin >>a >> b;
cout <<"The distance is" <<psA.distance(a,b) <<end1;
}
A、第5行:x <y 第8行:x="y;" 第9行:y="z;<br/"> B、第5行:x>y 第8行:x=y; 第9行:y=z;
C、第5行:x <y 第8行: y="z;" 第9行:x="y;<br/"> D、第5行:x>y 第8行: y=z; 第9行:x=y;
阅读下列程序说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【程序4.1说明】
"背包问题"的基本描述是:有一个背包,能盛放的物品总重量为S,设有N件物品,其重量分别为w1,w2,...,wn,希望从N件物品中选择若干件物品,所选物品的重量之和恰能放入该背包,即所选物品的重量之和等于S。
如下程序均能求得"背包问题"的一组解,其中程序4.1是"背包问题"的递归解法,而程序4.2是"背包问题"的非递归解法。
【程序4.1】
#include
#define N 7
#define S 15
int w[N+1]={0,1,4,3,4,5,2,7};
int knap(int s,int n)
{ if(s==0)return 1;
if (s<0||(s>0& &n<1))return 0;
if((1) )){
printf(″%4d″,w[n]);return 1;
}return (2) ;
}
main(){
if(knap(S,N))printf(″OK!\n″);
else printf(″N0!\n″);
}
【程序4.2】
#include
#define N 7
#define S 15
typedef struct {
int s;
int n:
int job;
} KNAPTP;
int w[N+1]={0,1,4,3,4,5,2,7};
int knap (int s,int n);
main() {
if (knap (S,N)) printf (″OK!\n″);
else printf (″NO!\n″);}
int knap (int s,int n)
{ KNAPTP stack[100],x;
int top,k,rep;
x.s=s;x.n=n;
x.job=0;
top=l;stack[top]=x;
k=0;
while((3) ) {
x=stack [ top ];
rep=1;
while (!k && rep ) {
if (x.s==0)k=1;/*已求得一组解*/
else if (x.s<0 || x.n <=0)rep=0;
else{x.s= (4) ;x.job=1;
(5) =x;
}
}
if(!k){
rep=1;
while(top>=1&&rep){
x=stack[top--];
if(x.job==1){
x.s+=w[x.n+1];
x.job=2;
stack[++top]=x;
(6) ;
}
}
}
}
if(k){/*输出一组解*/
while(top>=1){
x=stack[top--];
if(x.job==1)
printf(″%d\t″,w[x.n+1]);
}
}
return k;
}
A、obj->fun( );
B、obj->fun( &obj );
C、fun( obj );
D、fun( &obj );
A、void show( ) { cout << “圆面积= ” << Circle.GetArea() << endl; }
B、void show( Circle c ) { cout << “圆面积= ” << c.GetArea() << endl; }
C、void show( Circle &c ) { cout << “圆面积= ” << c.GetArea() << endl; }
D、void show( Circle *c ) { cout << “圆面积= ” << c->GetArea() << endl; }
A、Circle.SetR( 5 ); cout << Circle.GetArea();
B、Circle c; c.SetR( 5 ); cout << c.GetArea();
C、Circle c, *p = &c; p->SetR( 5 ); cout << p->GetArea();
D、Circle *p = new Circle( ); p->SetR( 5 ); cout << p->GetArea(); delete p;
A、for (int i = 0; i < 3; i++) { c[i].SetR(i*2.5); cout << c[i].GetArea() << endl; }
B、for (int i = 3; i >= 0; i++) { c[i].SetR(i*2.5); cout << c[i].GetArea() << endl; }
C、Circle *p = &c; for (int i = 0; i < 3; i++) { p->SetR(i*2.5); cout << p->GetArea() << endl; p++; }
D、Circle *p = &c[2]; for (int i = 2; i >= 0; i--) { p->SetR(i*2.5); cout << p->GetArea() << endl; p--; }
为了保护您的账号安全,请在“简答题”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!