第十二周项目四 点与圆的关系

问题及代码

/*    
ALL rights reserved.                           
*文件名称: 初学对象12                       
作者:李长鸿                        
*完成时间:2015.5.27                 
*问题描述: 点与圆的继承   
*/  
#include <iostream>
#include<Cmath>
using namespace std;
class Point
{
    double x,y;
public:
    Point(double a=0,double b=0):x(a),y(b) {}
    void setp()
    {
        cout<<"请输入点的坐标:";
        char a,b,c;
        cin>>a>>x>>b>>y>>c;
        while(a!='('||b!=','||c!=')')
        {
            cout<<"输入有误,请再次输入:";
            cin>>a>>x>>b>>y>>c;
        }
    }
    inline double getx()
    {
        return x;
    }
    inline double gety()
    {
        return y;
    }
    void printp()
    {
        cout<<"("<<x<<","<<y<<"); ";
    }
};
class Circle:public Point
{
    double r;
public:
    Circle(double a=0,double b=0,double c=1):Point(a,b),r(c) {}
    inline double getr()
    {
        return r;
    }
    friend istream &operator>>(istream&in,Circle&c);
    friend ostream &operator<<(ostream&out,Circle&c);
    double area();

};
istream &operator>>(istream &in,Circle &c)
{
    c.setp();
    cout<<"请输入半径:";
    in>>c.r;
    while(c.r<=0)
    {
        in>>c.r;
    }
    return in;
}
double Circle::area()
{
    return 3.14*r*r;
}
ostream& operator<<(ostream &out,Circle &c)
{
    cout<<"圆心坐标为:";
    c.printp();
    cout<<"半径为:"<<c.r<<"; ";
    cout<<"面积为:"<<c.area();
    return out;
}
int locate(Point p,Circle c)
{
    double h=sqrt((p.getx()-c.getx())*(p.getx()-c.getx())+(p.gety()-c.gety())*(p.gety()-c.gety()));
    if(h==c.getr())
        return 0;
    if(h<c.getr())
        return -1;
    if(h>c.gety())
        return 1;
}
int main()
{
    Circle c1(7,7,7);
    Point p1(7,7);
    cout<<"p1=";
    p1.printp();
    cout<<endl;
    cout<<"圆为:";
    cout<<c1;
    cout<<endl;
    p1.setp();
    cin>>c1;
    cout<<c1;
    cout<<endl;
    cout<<"点";
    p1.printp();
    cout<<"在";
    cout<<c1;
    cout<<"的圆";
    if(locate(p1,c1)==0)
    {
        cout<<"上"<<endl;
    }
    if(locate(p1,c1)>0)
    {
        cout<<"外"<<endl;
    }
    if(locate(p1,c1)<0)
    {
        cout<<"内"<<endl;
    }

    return 0;

}


总结:写<<   >>的重载时,格式的小细节未注意好,operator前的&忘了,out ,  in前的&也忘了,结果系统就给来个内存什么的问题。唉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值