第十二周C++实验报告(四)

来源:互联网 发布:淘宝香水正品店 编辑:程序博客网 时间:2024/06/01 13:29
[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<Cmath>  
  3. using namespace std;  
  4. class Point  
  5. {  
  6. public:  
  7.     Point(double x=0,double y=0);               //构造函数  
  8.     double distance(const Point &p) const;  //求距离  
  9.     double getx() const {return x;}   
  10.     double gety() const {return y;}   
  11.     void setx(double x1){x=x1;}  
  12.     void sety(double y1){y=y1;}  
  13.     friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”  
  14. protected:                                       //受保护成员  
  15.     double x,y;  
  16. };  
  17.   
  18.   
  19. //Point的构造函数  
  20. Point::Point(double a,double b):x(a),y(b){}  
  21.   
  22. double Point::distance(const Point &p) const    //求距离  
  23. {  
  24.     double dx = x-p.x;  
  25.     double dy = y-p.y;  
  26.     return sqrt(dx*dx+dy*dy);  
  27. }  
  28.   
  29. ostream & operator<<(ostream &output,const Point &p)  
  30. {  
  31.     output<<"["<<p.x<<","<<p.y<<"]"<<endl;  
  32.     return output;  
  33. }  
  34.   
  35. class Circle:public Point //circle是Point类的公用派生类  
  36. {  
  37. public:  
  38.     Circle(double x=0,double y=0,double r=0); //构造函数  
  39.     double area ( ) const//计算圆面积  
  40.     friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”  
  41.     friend int locate(const Point &p, const Circle &c); //判断点p在圆上、圆内或圆外,返回值:<0圆内,==0圆上,>0 圆外  
  42.     //重载关系运算符(种)运算符,使之能够按圆的面积比较两个圆的大小;  
  43.     bool operator>(const Circle &);  
  44.     bool operator<(const Circle &);  
  45.     bool operator>=(const Circle &);  
  46.     bool operator<=(const Circle &);  
  47.     bool operator==(const Circle &);  
  48.     bool operator!=(const Circle &);  
  49.     //给定一点p,求出该点与圆c的圆心相连成的直线与圆的两个交点p1和p2(为了带回计算结果,p1和p2需要声明为引用)  
  50.     friend void crossover_point(Point &p,Circle &c, Point &p1,Point &p2 ) ;  
  51. protected:  
  52.     double radius;  
  53. };  
  54.   
  55. //定义构造函数,对圆心坐标和半径初始化  
  56. Circle::Circle(double a,double b,double r):Point(a,b),radius(r){ }  
  57.   
  58. //计算圆面积  
  59. double Circle::area( ) const  
  60. {  
  61.     return 3.14159*radius*radius;  
  62. }  
  63.   
  64. //重载运算符“<<”,使之按规定的形式输出圆的信息  
  65. ostream &operator<<(ostream &output,const Circle &c)  
  66. {  
  67.     output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<endl;  
  68.     return output;  
  69. }  
  70.   
  71. //判断点p在圆内、圆c内或圆c外  
  72. int locate(const Point &p, const Circle &c)  
  73. {  
  74.     const Point cp(c.x,c.y); //圆心  
  75.     double d = cp.distance(p);  
  76.     if (abs(d - c.radius) < 1e-7)  
  77.         return 0;  //相等  
  78.     else if (d < c.radius)  
  79.         return -1;  //圆内  
  80.     else   
  81.         return 1;  //圆外  
  82. }  
  83.   
  84. //重载关系运算符(种)运算符,使之能够按圆的面积比较两个圆的大小;  
  85. bool Circle::operator>(const Circle &c)  
  86. {  
  87.     return (this->radius - c.radius) > 1e-7;  
  88. }  
  89.   
  90. bool Circle::operator<(const Circle &c)  
  91. {  
  92.     return (c.radius - this->radius) > 1e-7;  
  93. }  
  94.   
  95. bool Circle::operator>=(const Circle &c)  
  96. {  
  97.     return !(*this < c);  
  98. }  
  99.   
  100. bool Circle::operator<=(const Circle &c)  
  101. {  
  102.     return !(*this > c);  
  103. }  
  104.   
  105. bool Circle::operator==(const Circle &c)  
  106. {  
  107.     return abs(this->radius - c.radius) < 1e-7;  
  108. }  
  109.   
  110. bool Circle::operator!=(const Circle &c)  
  111. {  
  112.     return abs(this->radius - c.radius) > 1e-7;  
  113. }  
  114.   
  115. //给定一点p,求出该点与圆c的圆心相连成的直线与圆的两个交点p1和p2(为了带回计算结果,p1和p2需要声明为引用)  
  116. void crossover_point(Point &p, Circle &c, Point &p1,Point &p2 )  
  117. {  
  118.     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())))));  
  119.     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())))));  
  120.     p1.sety (p.gety() + (p1.getx() -p.getx())*(c.gety()-p.gety())/(c.getx()-p.getx()));  
  121.     p2.sety (p.gety() + (p2.getx() -p.getx())*(c.gety()-p.gety())/(c.getx()-p.getx()));  
  122. }  
  123.   
  124. int main( )  
  125. {  
  126.     Circle c1(3,2,4),c2(4,5,5);      //c2应该大于c1  
  127.     Point p1(1,1),p2(3,-2),p3(7,3);  //分别位于c1内、上、外  
  128.   
  129.     cout<<"圆c1: "<<c1;  
  130.     cout<<"点p1: "<<p1;  
  131.     cout<<"点p1在圆c1之"<<((locate(p1, c1)>0)?"外":((locate(p1, c1)<0)?"内":"上"))<<endl;  
  132.     cout<<"点p2: "<<p2;  
  133.     cout<<"点p2在圆c1之"<<((locate(p2, c1)>0)?"外":((locate(p2, c1)<0)?"内":"上"))<<endl;    
  134.     cout<<"点p3: "<<p3;  
  135.     cout<<"点p3在圆c1之"<<((locate(p3, c1)>0)?"外":((locate(p3, c1)<0)?"内":"上"))<<endl;  
  136.     cout<<endl;   
  137.   
  138.     cout<<"圆c1: "<<c1;  
  139.     if(c1>c2) cout<<"大于"<<endl;  
  140.     if(c1<c2) cout<<"小于"<<endl;   
  141.     if(c1>=c2) cout<<"大于等于"<<endl;  
  142.     if(c1<=c2) cout<<"小于等于"<<endl;   
  143.     if(c1==c2) cout<<"等于"<<endl;   
  144.     if(c1!=c2) cout<<"不等于"<<endl;   
  145.     cout<<"圆c2: "<<c1;  
  146.     cout<<endl;   
  147.   
  148.     Point p4,p5;  
  149.     crossover_point(p1,c1, p4, p5);  
  150.   
  151.     cout<<"点p1: "<<p1;  
  152.     cout<<"与圆c1: "<<c1;  
  153.     cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;  
  154.     cout<<"交点: "<<p4;  
  155.     cout<<"交点: "<<p5;  
  156.     cout<<endl;  
  157.     system("pause");  
  158.     return 0;  


原创粉丝点击