c++模板计算折线长度

//point.h
struct st
{
double x, y;
};


template
class point
{
    private:
        T *data;
int MaxSize;
public:
point(int Size=10);
void Display();
void Length();
        void Set();
~point();
};


template
point::point(int Size)
{
MaxSize = Size;
data = new T[MaxSize];
}


template
void point::Display()
{
    for(int i=0; i cout< }


template
void point::Length()
{
double sum=0, a1, b1, a2, b2;
a1 = data[0].x;
b1 = data[0].y;
for(int i=1; i {
a2 = data[i].x;
b2 = data[i].y;
sum = sum + sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
a1 = a2;
b1 = b2;
}
cout< }


template
void point::Set()
{
for(int i=0; i {
cout<<"Input 第"< cin>>data[i].x>>data[i].y;
}
}


template
point::~point()
{
delete []data;

}



//point.cpp
#include
#include
#include "point.h"
using namespace std;


int main()
{
pointp(10);
p.Set();
cout<<"内存块的数据如下:"<p.Display();
cout<<"组成的折线长度为:";
p.Length();
return 0;
}

你可能感兴趣的:(数据结构C++)