第12周实验报告4

实验目的:类的组合和继承

* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:         圆的关系

* 作    者:          胡斌                
* 完成日期:  2012 年 5月8日
* 版本号:     v1.0    

#include <iostream>   
#include <string.h>   
#include <cmath>   
#define PI 3.14   
using namespace std;  
class Point  
{  
public:  
    double x,y;  
public:  
    Point(double x0,double y0){x=x0;y=y0;}  
    Point()  
    {  
        x=0;  
        y=0;  
    }  
    ~ Point(){}  
    double Getx(){return x;}  
    double Gety(){return y;}  
    friend ostream&operator << (ostream&,Point&);  
  
};  
ostream&operator << (ostream&output,Point&p)  
{  
    output<<"点的坐标为:"<<"("<<p.Getx()<<","<<p.Gety()<<")";  
    return output;  
}  
  
class Circle:public Point  
{  
protected:  
    double r;  
  
public:  
    Circle(double x0,double y0,double r1):Point(x0,y0){r=r1;}  
    ~Circle(){}  
    double Getr(){return r;}  
    friend int locate(Point&, Circle&);  
    friend bool operator>(Circle& c1,Circle& c2);  
    friend bool operator<(Circle& c1,Circle& c2);  
    friend bool operator>=(Circle& c1,Circle& c2);  
    friend bool operator<=(Circle& c1,Circle& c2);  
    friend bool operator==(Circle& c1,Circle& c2);  
    friend bool operator!=(Circle& c1,Circle& c2);  
    friend void crossover_point1(Point&,Circle&,Point&,Point&);  
    friend ostream&operator << (ostream&,Circle&);  
};  
ostream&operator<<(ostream&output,Circle&C)  
{  
    output<<"圆心坐标为:"<<"("<<C.Getx()<<","<<C.Gety()<<")"<<"半径为:"<<C.Getr();  
    return output;  
}  
int locate(Point& p, Circle& c)  
{  
    double t;  
    t=sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));  
    if(t>c.Getr())  
    {  
        return 1;  
    }  
    if(t==c.Getr())  
    {  
        return 0;  
    }  
    if(t<c.Getr())  
    {  
        return -1;  
    }  
  
}  
bool operator>(Circle& c1,Circle& c2)  
{  
    double ares1,ares2;  
    ares1=PI*c1.Getr()*c1.Getr();  
    ares2=PI*c2.Getr()*c2.Getr();  
    if(ares1>ares2)  
    {  
        return true;  
    }  
    else  
        return false;  
}  
  
  
bool operator<(Circle& c1,Circle& c2)  
{  
    double ares1,ares2;  
    ares1=PI*c1.Getr()*c1.Getr();  
    ares2=PI*c2.Getr()*c2.Getr();  
    if(ares1<ares2)  
    {  
        return true;  
    }  
    else  
        return false;  
}  
bool operator==(Circle& c1,Circle& c2)  
{  
    double ares1,ares2;  
    ares1=PI*c1.Getr()*c1.Getr();  
    ares2=PI*c2.Getr()*c2.Getr();  
    if(ares1==ares2)  
    {  
        return true;  
    }  
    else  
        return false;  
}  
bool operator>=(Circle& c1,Circle& c2)  
{  
    if(c1>c2&&c1==c2)  
    {  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
  
}  
  
bool operator<=(Circle& c1,Circle& c2)  
{  
    if(c1<c2&&c1==c2)  
    {  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
}  
  
  
  
bool operator!=(Circle& c1,Circle& c2)  
{  
    if(c1==c2)  
    {  
        return false;  
    }  
    else  
    {  
        return true;  
    }  
  
}  
void crossover_point1(Point&p,Circle&c,Point&p4,Point&p5)  
{  
    p4.x=c.Getx()+(p.Getx()-c.Getx()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));  
    p5.x=c.Getx()-(p.Getx()-c.Getx()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));  
    p4.y=c.Gety()+(p.Gety()-c.Gety()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));  
    p5.y=c.Gety()-(p.Gety()-c.Gety()*c.Getr())/sqrt((p.Getx()-c.Getx())*(p.Getx()-c.Getx())+(p.Gety()-c.Gety())*(p.Gety()-c.Gety()));  
  
  
}  
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<<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_point1(p1,c1, p4, p5);  
  
    cout<<"点p1: "<<p1;  
    cout<<"与圆c1: "<<c1;  
    cout<<"的圆心相连,与圆交于两点,分别是:"<<endl;  
    cout<<"交点: "<<p4;  
    cout<<"交点: "<<p5;  
    cout<<endl;   
  
    system("pause");  
    return 0;  
}  


截图:

圆c1: 圆心坐标为:(3,2)半径为:4点p1: 点的坐标为:(1,1)点p1在圆c1之内
点p2: 点的坐标为:(3,-2)点p2在圆c1之上
点p3: 点的坐标为:(7,3)点p3在圆c1之外

圆c1: 圆心坐标为:(3,2)半径为:4小于
不等于
圆c2: 圆心坐标为:(3,2)半径为:4
点p1: 点的坐标为:(1,1)与圆c1: 圆心坐标为:(3,2)半径为:4的圆心相连,与圆交于两点
,分别是:
交点: 点的坐标为:(-1.91935,-1.1305)交点: 点的坐标为:(7.91935,5.1305)
请按任意键继续. . .


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值