再求一道c++题目 跪求答案

2024-11-06 19:02:33
推荐回答(1个)
回答1:

# include
# include
using namespace std;
class point
{
private:
double x,y;
public:
point(double a=0,double b=0);
double getx()
{return x;};
double gety()
{return y;};
void setxy(double,double);
~point();
};
point::point(double a,double b)
{x=a;y=b;}
void point::setxy(double a,double b)
{x=a;y=b;}
point::~point()
{cout<<"delete it:"<const int num=10;
void set(point *);
void display(point *);
double lenth(point *);
int main()
{
point *p=new point[10];
if(p==NULL)
{
cout<<"地址申请失败,结束程序运行。\n";
return 0;
}
set(p);
cout<<"内存块的数据如下:"< display(p);
cout<<"组成的折现长度为:";
cout< delete []p;
system("pause");
return 0;

}
void set(point *p)
{
double a,b;
for(int i=0;i {
cout<<"输入第"< cin>>a>>b;
(p+i)->setxy(a,b);
}
}
void display(point *p)
{
for(int i=0;i cout<<(p+i)->getx()<<","<<(p+i)->gety()<}
double lenth(point *p)
{
double sum=0,a1,a2,b1,b2;
a1=p->getx();
b1=p->gety();
for(int i=1;i {
a2=(p+i)->getx();
b2=(p+i)->gety();
sum+=sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
a1=a2;b1=b2;
}
return sum;
}