第二学期第13周项目2--圆的比较

/*
 * Copyright (c) 2013, 烟台大学计算机学院
 * All rights reserved.
 * 作    者:  沈远宏
 * 完成日期:2014 年 05月20日
 * 版 本 号:v1.0
 * 问题描述:(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
             (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
             (3)编写上述两类中的构造、析构函数及必要运算符重载函数(本项目主要是输入输出);
             (4)定义友元函数int locate,判断点p与圆的位置关系(返回值<0圆内,==0圆上,>0 圆外);
              (5)在项目1的基础上,在圆类上重载关系运算符(6种),使之能够按圆的面积比较两个圆的大小。
              自编main函数完成测试。

 * 输出:圆的关系
*/
#include <iostream>
#include <cmath>
#define PI 3.14
using namespace std;
class Point
{
protected:
    double x;
    double y;
public:
    Point(double xx=0,double yy=0):x(xx),y(yy) {}
    ~Point()
    {
        cout<<"已释放Point\n";
    }
    friend istream& operator >>(istream& in,Point &p);
    friend ostream& operator <<(ostream& out,Point &p);
    double getx()
    {
        return x;
    }
    double gety()
    {
        return y;
    }
};
istream& operator >>(istream& in,Point &p)
{
    in>>p.x>>p.y;
    return  in;
}
ostream& operator <<(ostream& out,Point &p)
{
    out<<"("<<p.x<<","<<p.y<<")";
    return  out;
}
class Circle: public Point
{
protected:
    double r;
public:
    Circle(double xx,double yy,double rr):Point(xx,yy),r(rr) {}
    ~Circle()
    {
        cout<<"已释放Circle\n";
    }
    double area();
    friend istream& operator >>(istream& in,Circle &c);
    friend ostream& operator <<(ostream& out,Circle &c);
    friend double locate(Point p,Circle c);
    friend bool operator >(Circle &a,Circle &b);
    friend bool operator <(Circle &a,Circle &b);
    friend bool operator ==(Circle &a,Circle &b);
    friend bool operator >=(Circle &a,Circle &b);
    friend bool operator <=(Circle &a,Circle &b);
    friend bool operator !=(Circle &a,Circle &b);
};
double Circle:: area()
{
    return PI*r*r;
}
istream& operator >>(istream& in,Circle &c)
{
    in>>c.x>>c.y>>c.r;
    return in;
}
ostream& operator <<(ostream& out,Circle &c)
{
    out<<"该圆为以"<<"("<<c.x<<","<<c.y<<")"<<"为圆心,以"<<c.r<<"为半径的"<<endl;
    return out;
}
double locate(Point p,Circle c)
{
    double distance;
    distance=sqrt((p.getx()-c.x)*(p.getx()-c.x)+(p.gety()-c.y)*(p.gety()-c.y));
    return (distance-c.r);
}
 bool operator >(Circle &a,Circle &b)
{
    if(a.area()>b.area())
    {
        return true;
    }
    else
    {
        return false;
    }
}
 bool operator <(Circle &a,Circle &b)
{
    if(a.area()<b.area())
    {
        return true;
    }
    else
    {
        return false;
    }
}
 bool operator ==(Circle &a,Circle &b)
{
    if(a.area()==b.area())
    {
        return true;
    }
    else
    {
        return false;
    }
}
 bool operator >=(Circle &a,Circle &b)
{
   if(!(a<b))
   {
       return true;
   }
   else
   {
       return false;
   }
}
 bool operator <=(Circle &a,Circle &b)
{
   if(!(a>b))
   {
       return true;
   }
   else
   {
       return false;
   }
}
 bool operator !=(Circle &a,Circle &b)
{
   if(!(a==b))
   {
       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: "<<c2;
    cout<<"圆c1"<<(c1>c2?"大于":c1<c2?"小于":"等于")<<"c2"<<endl;
    if(c1>=c2){cout<<"圆c1大于等于c2"<<endl;}
    if(c1!=c2){cout<<"圆c1不等于c2"<<endl;}
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值