(补)第十二周实验报告(4)

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:                              
* 作    者:     王      琦                                
* 完成日期:   2012      年   5    月   12   日
* 版 本 号:          
 
* 对任务及求解方法的描述部分 : 
* 输入描述:类的组合与继承
(1)先建立一个Point(点)类,包含数据成员x,y(坐标点);
(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员(半径),基类的成员表示圆心;
(3)编写上述两类中的构造、析构函数及必要的输入输出函数
(4)定义友元函数int locate,判断点p在圆c上、圆c内或圆c外,返回值<0圆内,==0圆上,>0 圆外;
(5)重载关系运算符(6种)运算符,使之能够按圆的面积比较两个圆的大小;
(6)给定一点p,求出该点与圆心相连成的直线与圆的两个交点并输出*/ 

 
* 问题描述:
* 程序输出: 
* 程序头部的注释结束


程序代码:

#include <iostream>   
#include <cmath>   
using namespace std;  
class Point  
{  
public:  
    Point(){x=0;y=0;}  
    Point(double x0,double y0) {x=x0;y=y0;}  
    ~Point(){};  
    double getX(){return x;}//公共接口   
    double getY(){return y;}  
    void setx(double x1){x = x1;}  
    void sety(double y1){y = y1;}  
    friend ostream &operator<<(ostream& output,Point &c);  
protected:  
    double x;  
    double y;  
};  
ostream &operator<<(ostream& output,Point &c)  
{   
    output<<"Point:("<<c.x <<","<<c.y<<")";  
    return output;  
}  
  
class Cricle:public Point  
{  
public:  
    Cricle(){r=0;}  
    Cricle(double x0,double y0,double r0):Point(x0,y0){r=r0;}  
    ~Cricle(){};  
    double getR(){return r;}  
    friend ostream &operator<<(ostream& output,Cricle &c);  
    friend double locate();  
    double area();  
  bool operator>(Cricle &c);  
    bool operator>=(Cricle &c);  
    bool operator<(Cricle &c);  
    bool operator<=(Cricle &c);  
    bool operator==(Cricle &c);  
    bool operator!=(Cricle &c);  
    friend void crossover_point1(Point &p1,Cricle &c1,Point &p4,Point &p5);  
protected:  
    double r;  
};  
ostream &operator<<(ostream& output,Cricle  &c)  
{  
    output<<"圆心为:"<<"("<<c.getX()<<","<<c.getY()<<"),"<<"半径为:"<<c.r<<endl;  
    return output;  
}  
double locate(Point p,Cricle c)  
{  
    double d;  
    d=(c.getX()-p.getX())*(c.getX()-p.getX())+(c.getY()-p.getY())*(c.getY()-p.getY());  
    return (c.getR()*c.getR()-d);  
}  
double Cricle::area()  
{  
    return r*r*3.1415926;       
}  
  
bool Cricle::operator>(Cricle &c)  
{  
    if(area()>c.area())  
        return true;  
    return false;  
}  
bool Cricle::operator>=(Cricle &c)  
{  
    if(area()<c.area())  
        return false;  
    return true;  
}  
bool Cricle::operator<(Cricle &c)  
{  
    if(area()<c.area())  
        return true;  
    return false;  
}  
bool Cricle::operator<=(Cricle &c)  
{  
    if(area()>c.area())  
        return false;  
    return true;  
}  
bool Cricle::operator==(Cricle &c)  
{  
    if(area()==c.area())  
        return true;  
    return false;  
}  
bool Cricle::operator!=(Cricle &c)  
{  
    if(area()==c.area())  
        return false;  
    return true;  
}  
void crossover_point1(Point &p1,Cricle &c1,Point &p4,Point &p5)   
{   
    double d;   
    d = c1.getX() + (p1.getX() - c1.getX()) * c1.getR() / sqrt((p1.getX() - c1.getX()) * (p1.getX() - c1.getX()) + (p1.getY() - c1.getY()) * (p1.getY() - c1.getY()));   
    p4.setx(d);   
   
    d = c1.getX() - (p1.getX() - c1.getX()) * c1.getR() / sqrt((p1.getX() - c1.getX()) * (p1.getX() - c1.getX()) + (p1.getY() - c1.getY()) * (p1.getY() - c1.getY()));   
    p5.setx(d);   
   
    d = c1.getY() + (p1.getY() - c1.getY()) * c1.getR() / sqrt((p1.getX() - c1.getX()) * (p1.getX() - c1.getX()) + (p1.getY() - c1.getY()) * (p1.getY() - c1.getY()));   
    p4.sety(d);   
   
    d = c1.getY() - (p1.getY() - c1.getY()) * c1.getR() / sqrt((p1.getX() - c1.getX()) * (p1.getX() - c1.getX()) + (p1.getY() - c1.getY()) * (p1.getY() - c1.getY()));   
    p5.sety(d);   
}  
int main( )  
{  
    Cricle 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<<endl;  
    cout<<"交点: "<<p5;  
    cout<<endl;   
  
    system("pause");  
    return 0;  
}  


运行显示:

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

圆c1: 圆心为:(3,2),半径为:4
小于
小于等于
不等于
圆c2: 圆心为:(3,2),半径为:4

点p1: Point:(1,1)与圆c1: 圆心为:(3,2),半径为:4
的圆心相连,与圆交于两点,分别是:
交点: Point:(-0.577709,0.211146)
交点: Point:(6.57771,3.78885)
请按任意键继续. . .
上机感想:

我认为难点还是在定义成员函数时的那些算法,不去看的话,自己很难想的出来。或许自己不大擅长计算。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值