/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:王鲁峰
*完成日期:2014 年 5 月 24 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: -
*问题描述:(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
(4)定义友元函数,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);
(5)在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小。
*程序输出:点与圆的关系,圆与圆的大小关系
*问题分析:
*算法设计:
*/
#include<iostream>
using namespace std;
#include<cmath>
const double pi=3.14;
class Point
{
public:
Point(double X,double Y):x(X),y(Y){}
~Point(){}
double getX()
{
return x;
}
double getY()
{
return y;
}
friend ostream & operator << (ostream &out,Point &p);
protected:
double x,y;
};
class Circle:public Point
{
public:
Circle(double a,double b,double R):Point(a,b),r(R){}
~Circle(){}
double getR()
{
return r;
}
friend double locate(Point,Circle);
friend ostream & operator << (ostream &out,Circle &c);
double area();
bool operator > (Circle &c);
bool operator < (Circle &c);
bool operator >= (Circle &c);
bool operator <= (Circle &c);
bool operator == (Circle &c);
bool operator != (Circle &c);
private:
double r;
};
ostream & operator << (ostream &out,Point &p)
{
out<<"("<<p.getX()<<","<<p.getY()<<")"<<endl;
return out;
}
ostream & operator << (ostream &out,Circle &c)
{
out<<"("<<c.getX()<<","<<c.getY()<<")"<<" "<<"r="<<c.getR()<<endl;
return out;
}
double locate(Point p,Circle c)
{
double R,result;
R=sqrt(pow(p.getX()-c.getX(),2)+pow(p.getY()-c.getY(),2));
if(R<c.getR())
result=-1;
else if(R==c.getR())
result=0;
else
result=1;
return result;
}
double Circle::area()
{
return pi*getR()*getR();
}
bool Circle::operator > (Circle &c)
{
if(area()>c.area())
return true;
else
return false;
}
bool Circle::operator < (Circle &c)
{
if(area()<c.area())
return true;
else
return false;
}
bool Circle::operator >= (Circle &c)
{
if(!(area()<c.area()))
return true;
else
return false;
}
bool Circle::operator <= (Circle &c)
{
if(!(area()>c.area()))
return true;
else
return false;
}
bool Circle::operator == (Circle &c)
{
if(area()==c.area())
return true;
else
return false;
}
bool Circle::operator != (Circle &c)
{
if(!(area()==c.area()))
return true;
else
return false;
}
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;
cout<<"圆c2: "<<c2;
if(c1>c2)
cout<<"c1>c2"<<endl;
if(c1<c2)
cout<<"c1<c2"<<endl;
if(c1==c2)
cout<<"c1=c2"<<endl;
if(c1!=c2)
cout<<"c1!=c2"<<endl;
if(c1>=c2)
cout<<"c1>=c2"<<endl;
if(c1<=c2)
cout<<"c1<=c2"<<endl;
return 0;
}
运行结果: