#include <iostream> #include <cmath> using namespace std; class Point { public: Point(double a,double b):x(a),y(b) {} double getx() { return x; } double gety() { return y; } friend ostream&operator << (ostream&,Point&); protected: double x,y; }; ostream&operator << (ostream &output,Point &p) { output<<"("<<p.getx()<<","<<p.gety()<<")"<<endl; return output; } class Circle:public Point { public: Circle(double a,double b,double c):Point(a,b),r(c) {} double getr() { return r; } friend ostream&operator << (ostream&,Circle&); friend double locate(Circle&,Point&); protected: double r; }; ostream&operator << (ostream &output,Circle &c) { output<<"("<<c.getx()<<","<<c.gety()<<")"<<" "; output<<"r="<<c.getr()<<endl; return output; } double locate(Point &po,Circle &ci) { double d,num; d=sqrt(pow((ci.getx()-po.getx()),2)+pow((ci.gety()-po.gety()),2)); if(d>ci.getr()) num=1; else if(d==ci.getr()) num=0; else num=-1; return num; } int main( ) { Circle c1(3,2,4),c2(4,5,5); //c2应该大于c1 Point p1(1,1),p2(3,-2),p3(7,3); //分别位于c1内、上、外 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; return 0; }
感悟:还没吃饭呢!
时间: 2024-10-13 17:09:48