2020-03-10 21:46

# 6-3 函数模板 (10分) 不知如何运用函数模板

template < class T >
double dist(T a, T b)

#include
#include
using namespace std;
double dist(int a)
{
if(a==1){
int x,y,d;
cin>>x>>y;
d=x-y;
if(d return d;
}
if(a==2){
double x,y,d;
cin>>x>>y;
d=x-y;
if(d return d;
}
if(a==3){
double x1,y2,x2,y1,d;
cin>>x1>>y1>>x2>>y2;
d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
return d;
}
}
int main(){
int i;
cin>>i;
while(i!=0){
cout< cout cin>>i;
}
return 0;
}

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

• 回答这么多问题就耍赖把我的积分一笔勾销了 1年前
``````#include <iostream>
#include <math.h>
using namespace std;

class point
{
public:
double x;
double y;
point() : x(0), y(0) {}
point(double _x, double _y) : x(_x), y(_y) {}
double operator-(point p) { return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); }
};

template <class T>
double dist(T a, T b)
{
return a - b;
}

int main()
{
int n;
while (1)
{
cin >> n;
if (n == 0) break;
if (n == 1)
{
int a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
else if (n == 2)
{
double a, b;
cin >> a >> b;
cout << dist(a, b) << endl;
}
else if (n == 3)
{
point a, b;
cin >> a.x >> a.y >> b.x >> b.y;
cout << dist(a, b) << endl;
}
}
return 0;
}

``````
点赞 评论 复制链接分享