m0_59064448 2021-12-05 18:50 采纳率: 55.6%
浏览 26
已结题

能不能写一个接口?急!

#include <iostream>
#include <math.h>
using namespace std;
class Point
{
public:
    double x,y;
    Point(){x=0;y=0;}
    Point(int xx,int yy){x=xx;y=yy;}
};
class Circle
{
public:
    Point center;
    double radius;
    Circle(){}
    Circle(Point p,double r)
    {
        center = p;
        radius = r;
    }
    void fun(Point p)
    {
        double dis = sqrt( (center.x-p.x)*(center.x-p.x) + (center.y-p.y)*(center.y-p.y) );
        if(dis < radius)
            cout << "在圆内"<< endl;
        else if(dis == radius)
            cout << "在圆上"<< endl;
        else
            cout << "在圆外" << endl;
    }
};
class Triangle
{
public:
    Point a,b,c;
    Triangle(){}
    Triangle(Point x,Point y,Point z)
    {
        a =x;b=y;c=z;
        double l1 = sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
        double l2 = sqrt( (a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y) );
        double l3 = sqrt( (b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y) );
        if(l1 +l2 <= l3 || l2+l3 <= l1 || l1+l3<= l2)
            cout << "不能组成三角形";
    }
    double getzc()
    {
        double l1 = sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
        double l2 = sqrt( (a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y) );
        double l3 = sqrt( (b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y) );
        if(l1 +l2 <= l3 || l2+l3 <= l1 || l1+l3<= l2)
        {
            return -1;
        }
        else
            return (l1 + l2 +l3);
    }
};
int main()
{
    Circle c(Point(2,2),1);
    c.fun(Point(1,1));
    Triangle tr(Point(0,0),Point(1,0),Point(0,1));
    if (tr.getzc() < 0)
    {
        cout <<"不构成三角形"<<endl;
    }else
        cout <<"周长="<<tr.getzc()<<endl;
    return 0;
}


  • 写回答

2条回答 默认 最新

  • 关注

    参考如下:

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    //抽象类
    class MyObject
    {
    public:
        virtual double getzc()=0;  
    };
    
    
    class Point
    {
    public:
        double x,y;
        Point(){x=0;y=0;}
        Point(int xx,int yy){x=xx;y=yy;}
    };
    class Circle :public MyObject
    {
    public:
        Point center;
        double radius;
        Circle(){}
        Circle(Point p,double r)
        {
            center = p;
            radius = r;
        }
        double getzc()
        {
            return 2*3.1415926*radius;
        }
        void fun(Point p)
        {
            double dis = sqrt( (center.x-p.x)*(center.x-p.x) + (center.y-p.y)*(center.y-p.y) );
            if(dis < radius)
                cout << "在圆内"<< endl;
            else if(dis == radius)
                cout << "在圆上"<< endl;
            else
                cout << "在圆外" << endl;
        }
    };
    class Triangle :public MyObject
    {
    public:
        Point a,b,c;
        Triangle(){}
        Triangle(Point x,Point y,Point z)
        {
            a =x;b=y;c=z;
            double l1 = sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
            double l2 = sqrt( (a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y) );
            double l3 = sqrt( (b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y) );
            if(l1 +l2 <= l3 || l2+l3 <= l1 || l1+l3<= l2)
                cout << "不能组成三角形";
        }
        double getzc()
        {
            double l1 = sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
            double l2 = sqrt( (a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y) );
            double l3 = sqrt( (b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y) );
            if(l1 +l2 <= l3 || l2+l3 <= l1 || l1+l3<= l2)
            {
                return -1;
            }
            else
                return (l1 + l2 +l3);
        }
    };
    int main()
    {
        Circle c(Point(2,2),1);
        c.fun(Point(1,1));
        Triangle tr(Point(0,0),Point(1,0),Point(0,1));
        if (tr.getzc() < 0)
        {
            cout <<"不构成三角形"<<endl;
        }else
            cout <<"周长="<<tr.getzc()<<endl;
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月14日
  • 已采纳回答 12月6日
  • 创建了问题 12月5日

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?