创建一个CPoint 类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。
要求:
1。输入两个点的坐标,输出两个点之间的距离
2。重载运算符为“-”
创建一个CPoint 类,代表平面直角坐标系中的点,创建构造函数和运算符重载函数,运算符重载为类重载(非友元重载),可以实现计算两个点之间的距离。
要求:
1。输入两个点的坐标,输出两个点之间的距离
2。重载运算符为“-”
输入第一行为样例数m,接下来m行每行4个整数分别表示两个点的横纵坐标。
输出m行,通过重载“-”运算输出两点的距离,保留小数点后两位。
1 0 0 2 0
2.00
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <vector> #include <map> #include <cmath> #include <set> #include <queue> using namespace std; class CPoint{ public : int x,y; CPoint() { } CPoint(int x,int y) { this->x = x; this->y = y; } void operator - (const CPoint A) { double ans = 0.0; ans = sqrt(1.0*((A.x - x) * (A.x - x)) + 1.0 * ((A.y - y) *(A.y - y))); printf("%.2lf\n",ans); } }; int main() { int n; cin >> n; for(int i = 0 ;i<n;i++) { int a,b,c,d; cin >> a >> b >> c >> d; CPoint p(a,b); CPoint q(c,d); p - q; } }
Python实现 重载 ‘-’ class CPoint(object): def __init__(self, coordinateX, coordinateY): self.x = coordinateX self.y = coordinateY def __sub__(self, other): return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5 for _ in range(int(input())): firstX, firstY, secondX, secondY = map(int, input().split()) first = CPoint(firstX, firstY) second = CPoint(secondX, secondY) print('%.2f' % (first - second))
#include<iostream> #include<cmath> #include<iomanip> using namespace std; int main(){ int m; cin>>m; while(m>0){ int x0,y0,x1,y1; cin>>x0>>y0>>x1>>y1; cout<<fixed<<setprecision(2)<<sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1))<<endl; m--; } }
#include <iostream> #include <cmath> using namespace std; class CPoint { public: int x, y; CPoint(int _x, int _y): x(_x), y(_y) {} double operator-(CPoint& p) { return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); } }; int main() { int m; cin >> m; while (m--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; CPoint p1(x1, y1), p2(x2, y2); printf("%.2lf\n", p1 - p2); } }
#include<iostream> (720)#include<cmath> using namespace std; class CPoint{ private: int x; int y; public: CPoint(int x,int y){ this->x=x; this->y=y; } void operator-(CPoint c){ x=x-c.get_x(); y=y-c.get_y(); } int get_x(){ return x; } int get_y(){ return y; } }; int main(){ int k,a,b,c,d; cin>>k; while(k-- >0){ cin>>a>>b>>c>>d; CPoint c1(a,b); CPoint c2(c,d); c1-c2; double res=sqrt(pow(c1.get_x(),2)+pow(c1.get_y(),2)); printf("%.2f\n",res); } return 0; }
#include <cstdio> #include <cmath> class CPoint { public: int x, y; CPoint(int xx, int yy): x(xx), y(yy) {} float operator-(const CPoint &r) { return hypot(x - r.x, y - r.y); } }; int main () { int N, x1, y1, x2, y2; scanf("%d", &N); while (N--) { scanf("%d %d %d %d", &x1, &y1, &x2, &y2); printf("%.2f\n", CPoint(x1, y1) - CPoint(x2, y2)); } }
#include <iostream> #include <cmath> class CPoint { private: int x; int y; double ans; public: CPoint(int a, int b):x(a),y(b){} CPoint & operator-(const CPoint c) { this->ans = sqrt(1.0*(x - c.x)*(x - c.x) + 1.0*(y - c.y)*(y - c.y)); return *this; } double getAns() { return ans; } }; int main() { int m; std::cin>>m; while(m--) { int x1,y1,x2,y2; std::cin>>x1>>y1>>x2>>y2; CPoint t(x1,y1),c(x2,y2); CPoint a = t-c; printf("%.2f\n",a.getAns()); } return 0; }
# ** 函数的创建&运算符重载 ** # class CPoint(object): def __init__(self, x, y): self.x = x self.y = y def __sub__(self, dot): return ((self.x - dot.x)**2 + (self.y - dot.y)**2)**0.5 # ** 数据输入 ** # num = int(input()) for i in range(num): d0 = [int(x) for x in input().split()] dot0 = CPoint(d0[0],d0[1]) dot1 = CPoint(d0[2],d0[3]) print('%0.2f'%(dot0-dot1))
import java.util.Scanner;
import java.lang.Math;
public class Main{
static class Cpoint{
int x;
int y;
char ch;
public Cpoint(int x, int y){
super();
this.x=x;
this.y=y;
}
public Cpoint() {
// TODO Auto-generated constructor stub
}
public double Distance(int x,int y,char ch){
if(ch=='-'){
double d=(this.x-x)*(this.x-x)+(this.y-y)*(this.y-y);
return Math.sqrt(d);
}else{
return 0;
}
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int m=sc.nextInt();
for(int i=0;i<m;i++){
Cpoint p1=new Cpoint();
Cpoint p2=new Cpoint();
p1.setX(sc.nextInt());
p1.setY(sc.nextInt());
p2.setX(sc.nextInt());
p2.setY(sc.nextInt());
double b=p1.Distance(p2.getX(),p2.getY(),'-');
System.out.println(String.format("%.2f", b));
}
}
}
我都不想在这发解题思路了实在是没啥意思,哎,,,,,,#include <iostream> #include <iomanip> #include <math.h> using namespace std; class CPoint{ public: int x,y; CPoint(int x,int y) { this->x = x;this->y = y; } double operator-(const CPoint &p) { int temp = pow(this->x-p.x,2)+pow(this->y-p.y,2); return pow(temp,0.5); } }; int main() { int m; cin >> m; while(m--) { int a,b,c,d; cin >> a >> b >> c >> d; CPoint p1(a,b),p2(c,d); cout << fixed << setprecision(2) << p1-p2 << endl; } }