CPP 两点间的距离 (函数模板)

    xiaoxiao2022-07-03  122

    题目

    代码

    #include <iostream> #include <cmath> using namespace std; template <class T> class d { private: T x1; T y1; T x2; T y2; public: d(int i_x1,int i_y1, int i_x2, int i_y2) { x1=i_x1; x2=i_x2; y1=i_y1; y2=i_y2; } d(double i_x1,double i_y1, double i_x2, double i_y2) { x1=i_x1; x2=i_x2; y1=i_y1; y2=i_y2; } T distance( T x1, T y1, T x2, T y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } }; int main( ) { int i_x1, i_y1, i_x2, i_y2; double d_x1, d_y1, d_x2, d_y2; cin>>i_x1>>i_y1>>i_x2>>i_y2; cin>>d_x1>>d_y1>>d_x2>>d_y2; d<int> d1(i_x1, i_y1, i_x2, i_y2); d<double> d2( d_x1, d_y1, d_x2, d_y2); cout<<d1.distance( i_x1, i_y1, i_x2, i_y2 )<<endl; cout<<d2.distance(d_x1, d_y1, d_x2, d_y2 )<<endl; return 0; }
    最新回复(0)