第十二周C++实验报告(四)
来源:互联网 发布:淘宝香水正品店 编辑:程序博客网 时间:2024/06/01 13:29
- #include<iostream>
- #include<Cmath>
- using namespace std;
- class Point
- {
- public:
- Point(double x=0,double y=0);
- double distance(const Point &p) const;
- double getx() const {return x;}
- double gety() const {return y;}
- void setx(double x1){x=x1;}
- void sety(double y1){y=y1;}
- friend ostream & operator<<(ostream &,const Point &);
- protected:
- double x,y;
- };
-
-
-
- Point::Point(double a,double b):x(a),y(b){}
-
- double Point::distance(const Point &p) const
- {
- double dx = x-p.x;
- double dy = y-p.y;
- return sqrt(dx*dx+dy*dy);
- }
-
- ostream & operator<<(ostream &output,const Point &p)
- {
- output<<"["<<p.x<<","<<p.y<<"]"<<endl;
- return output;
- }
-
- class Circle:public Point
- {
- public:
- Circle(double x=0,double y=0,double r=0);
- double area ( ) const;
- friend ostream &operator<<(ostream &,const Circle &);
- friend int locate(const Point &p, const Circle &c);
-
- bool operator>(const Circle &);
- bool operator<(const Circle &);
- bool operator>=(const Circle &);
- bool operator<=(const Circle &);
- bool operator==(const Circle &);
- bool operator!=(const Circle &);
-
- friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 ) ;
- protected:
- double radius;
- };
-
-
- Circle::Circle(double a,double b,double r):Point(a,b),radius(r){ }
-
-
- double Circle::area( ) const
- {
- return 3.14159*radius*radius;
- }
-
-
- ostream &operator<<(ostream &output,const Circle &c)
- {
- output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<endl;
- return output;
- }
-
-
- int locate(const Point &p, const Circle &c)
- {
- const Point cp(c.x,c.y);
- double d = cp.distance(p);
- if (abs(d - c.radius) < 1e-7)
- return 0;
- else if (d < c.radius)
- return -1;
- else
- return 1;
- }
-
-
- bool Circle::operator>(const Circle &c)
- {
- return (this->radius - c.radius) > 1e-7;
- }
-
- bool Circle::operator<(const Circle &c)
- {
- return (c.radius - this->radius) > 1e-7;
- }
-
- bool Circle::operator>=(const Circle &c)
- {
- return !(*this < c);
- }
-
- bool Circle::operator<=(const Circle &c)
- {
- return !(*this > c);
- }
-
- bool Circle::operator==(const Circle &c)
- {
- return abs(this->radius - c.radius) < 1e-7;
- }
-
- bool Circle::operator!=(const Circle &c)
- {
- return abs(this->radius - c.radius) > 1e-7;
- }
-
-
- void crossover_point(Point &p, Circle &c, Point &p1,Point &p2 )
- {
- p1.setx (c.getx() + sqrt(c.radius*c.radius/(1+((c.gety()-p.gety())/(c.getx()-p.getx()))*((c.gety()-p.gety())/(c.getx()-p.getx())))));
- p2.setx (c.getx() - sqrt(c.radius*c.radius/(1+((c.gety()-p.gety())/(c.getx()-p.getx()))*((c.gety()-p.gety())/(c.getx()-p.getx())))));
- p1.sety (p.gety() + (p1.getx() -p.getx())*(c.gety()-p.gety())/(c.getx()-p.getx()));
- p2.sety (p.gety() + (p2.getx() -p.getx())*(c.gety()-p.gety())/(c.getx()-p.getx()));
- }
-
- int main( )
- {
- Circle c1(3,2,4),c2(4,5,5);
- Point p1(1,1),p2(3,-2),p3(7,3);
-
- cout<<"圆c1: "<<c1;
- cout<<"点p1: "<<p1;
- cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)?"外":((locate(p1, c1)<0)?"内":"上"))<<endl;
- cout<<"点p2: "<<p2;
- cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl;
- cout<<"点p3: "<<p3;
- cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;
- cout<<endl;
-
- cout<<"圆c1: "<<c1;
- if(c1>c2) cout<<"大于"<<endl;
- if(c1<c2) cout<<"小于"<<endl;
- if(c1>=c2) cout<<"大于等于"<<endl;
- if(c1<=c2) cout<<"小于等于"<<endl;
- if(c1==c2) cout<<"等于"<<endl;
- if(c1!=c2) cout<<"不等于"<<endl;
- cout<<"圆c2: "<<c1;
- cout<<endl;
-
- Point p4,p5;
- crossover_point(p1,c1, p4, p5);
-
- cout<<"点p1: "<<p1;
- cout<<"与圆c1: "<<c1;
- cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;
- cout<<"交点: "<<p4;
- cout<<"交点: "<<p5;
- cout<<endl;
- system("pause");
- return 0;
- }