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日

悬赏问题

  • ¥40 matlab调用ansys联合
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证