64,648
社区成员
发帖
与我相关
我的任务
分享
#ifndef _POINT_H
#define _POINT_H
#include <cmath>
class Point
{
private:
float x;
float y;
public:
explicit Point( const float& _x = 0, const float& _y = 0 )
{
x = _x;
y = _y;
}
~Point(){}
Point& operator = ( const Point& other )
{
x = other.x;
y = other.y;
return *this;
}
friend bool operator == ( const Point& lhs, const Point& rhs )
{
return ( fabs( lhs.x - rhs.x ) < 1E-10) && ( fabs( lhs.y - rhs.y ) < 1E-10 );
}
};
class Edge
{
private:
bool flag;
Point p1;
Point p2;
public:
explicit Edge( const Point& _p1, const Point& _p2 )
{
flag = true;
p1 = _p1;
p2 = _p2;
}
~Edge(){}
friend bool operator == ( const Edge& lhs, const Edge& rhs )
{
return ( lhs.flag == rhs.flag ) &&
( lhs.p1 == rhs.p1 ) &&
( lhs.p2 == rhs.p2 );
}
};
#endif
#include "point.h"
#include <iostream>
using namespace std;
int main()
{
if (
Edge( Point(1,15), Point(0,16) ) ==
Edge( Point(1,15), Point(3,16))
)
cout << "true";
else
cout << "false";
return 0;
}
class Point
{
public:
float x;
float y;
public:
Point (void);
~Point ();
Point (float x, float y);
friend bool operator == (const Point & lhs, const Point& rhs )
{
//return ( ( lhs.x == rhs.x ) && ( lhs.y == rhs.y ) );
//推荐这种写法
return( ( fabs( lhs.x - rhs.x ) < 1E-10 ) && ( fabs( lhs.x - rhs.x ) < 1E-10 ) );
}
};
class Point
{
public:
float x;
float y;
public:
Point (void);
public:
//~Point(void);
~Point ();
Point (float x, float y);
// 两点是否相等
//bool operator==(const Point p)const;
bool operator== (const Point & p) const;
};
//两点是否相等
/*
bool Point::operator==(const Point p)const
{
if(this->x==p.x && this->y==p.y)
return true;
else
return false;
}
*/
bool
Point::operator == (const Point & p) const
{
return ( x == p.x && y == p.y);
}
class Edge
{
public:
Edge (void);
Edge (Point p1, Point p2);
//bool operator==(const Edge e)const;
bool operator== (const Edge & e) const;
public:
//~Edge(void);
~Edge ();
public:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
//两条边相等
bool
Edge::operator == ( const Edge& e) const
{
return (
( flag == e.flag ) &&
( ( ( p1 == e.p1 ) && ( p2 == e.p2 ) ) ||
( ( p2 == e.p1 ) && ( p1 == e.p2 ) )
)
);
//return ((this->p1 == e.p1 && this->p2 == e.p2) ? ?¦(this->p1 == e.p2
// && this->p2 == e.p1))
//return true;
//else
//return false;
}
// 请使用初始化列表,不要这样赋值
Edge::Edge(Point p1,Point p2)
{
this->p1=p1;
this->p2=p2;
this->flag=false;
}
#include <iostream>
using namespace std;
class Point
{
public:
float x;
float y;
public:
Point(void) {}
public:
~Point(void) {}
Point(float x,float y) : x(x), y(y) {}
// 两点是否相等
bool operator==(const Point& p)const;
};
//两点是否相等
bool Point::operator==(const Point& p)const
{
if(this->x==p.x && this->y==p.y)
return true;
else
return false;
}
class Edge
{
public:
Edge(void) {}
Edge(Point p1,Point p2) : p1(p1), p2(p2) {}
bool operator==(const Edge& e)const;
public:
~Edge(void) {}
public:
// 标识该边是否为边界
bool flag;
Point p1;
Point p2;
};
//两条边相等
bool Edge::operator==(const Edge& e)const
{
if((this->p1==e.p1 && this->p2==e.p2) || (this->p1==e.p2 && this->p2==e.p1))
return true;
else
return false;
}
int main(void)
{
cout << (Edge(Point(1, 2), Point(3, 4)) == Edge(Point(1, 2), Point(3, 5))) << endl;
return 0;
}