3.动态存储管理和程序调试

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;


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值