point.h
----------------------------------------------------------------------------
#include<iostream>
#include<cmath>
using namespace std;
class point{
private:
double x,y;
public:
point(double =0,double =0);
double Getx(); //获取x
double Gety(); //获取y
void setxy(double,double); //设置x,y
~point(); //析构的同时显示删去的点
};
void set(point *); //设置x,y
void Display(point *); //显示x,y
double lenth(point *); //计算折现长度
const int num=10; //设置开辟内存的大小
--------------------------------------------------------------------------
#include"point.h"
point::point(double a,double b){
x=a;
y=b;
}
double point::Getx(){
return x;
}
double point::Gety(){
return y;
}
void point::setxy(double a,double b){
x=a;
y=b;
}
point::~point(){
cout<<x<<","<<y<<"is delete."<<endl;
}
void set(point *p){
double a,b;
int i;
for(i=0;i<num;i++)
{
cout<<"请输入第"<<i<<"个点坐标:";
cin>>a>>b;
(p+i)->setxy(a,b);
}
}
void Display(point *p){
for(int i=0;i<num;i++)
{
cout<<"第"<<i<<"个点的坐标是:"<<(p+i)->Getx()<<","<<(p+i)->Gety()<<endl;
}
}
double lenth(point *p){
double sum=0.0,a1,a2,b1,b2;
a1=p->Getx();
b1=p->Gety();
for(int i=1;i<num;i++)
{
a2=(p+i)->Getx();
b2=(p+i)->Gety();
sum+=sqrt((a2-a1)*(a2-a1)+(b2-b1)*(b2-b1));
a1=a2;
b1=b2;
}
return sum;
}
int main()
{
point *p=new point[10];
if(p==NULL)
{
cout<<"分配内存失败。"<<endl;
return 0;
}
set(p);
Display(p);
cout<<"lenth="<<lenth(p)<<endl;
delete []p;
return 0;
}