C++圆类

要求:

编写一个圆类

成员变量

    int r;

    int x;

    int y;

成员函数

    返回周长

    返回面积

    返回直径

    返回圆心

    设定半径

    设定x

    设定y

    判定两个圆是否相

/*
编写一个圆类

成员变量

    int r;

    int x;

    int y;

成员函数

    返回周长

    返回面积

    返回直径

    返回圆心

    设定半径

    设定x

    设定y

    判定两个圆是否相切


*/
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

const double PI = 3.1415; //圆周率

class round_1
{
private:
    double r;//圆的半径

    int x;//圆的横坐标

    int y;//圆的纵坐标
public:
    round_1()
    {
    }
    round_1(int r,int x ,int y):r(r),x(x),y(y)
    {
    }
    //返回周长
    double get_girth()
    {
        return 2*PI*r;
    }
    // 返回面积
    double get_area()
    {
        return r*PI*r;
    }
    // 返回直径
    double get_diameter()
    {
        return 2*r;
    }
    // 返回圆心

    // 设定半径
    void set_r(double r)
    {
        this->r = r;
    }
    // 设定x
    void set_x(int x)
    {
        this->x = x;
    }
    // 设定y
    void set_y(int y)
    {
        this->y = y;
    }
    // 判定两个圆是否相切
    int round_center(round_1 other)
    {
        double distance = sqrt (pow((y-other.y),2)+pow((x-other.x),2));//通过两个圆的坐标求出两圆的圆心距离
        double r_distance_1 = abs(r-other.r);
        double r_distance_2 = r+other.r;
        if(distance == r_distance_1)
        {
            return 1;//内切
        }
        else if(distance == r_distance_2)
        {
            return 2;//外切
        }
        else
        {
            return 3;
        }
    }
};



int main(int argc, char const *argv[])
{
    
    round_1 r1(3,0,0);
    round_1 r2;
    r2.set_r(2);
    r2.set_x(4);
    r2.set_y(3);
    cout << "圆1的周长为" << r1.get_girth() << endl;
    cout << "圆1的直径为" << r1.get_diameter() << endl;
    cout << "圆1的面积为" << r1.get_area() << endl;
    
    cout << "圆2的周长为" << r2.get_girth() << endl;
    cout << "圆2的直径为" << r2.get_diameter() << endl;
    cout << "圆2的面积为" << r2.get_area() << endl;
    int flag = r1.round_center(r2);
    {
        if(flag == 1)
        {
            cout << "圆1和圆2内切" << endl;
        }
        else if(flag == 2)
        {
            cout << "圆1和圆2外切" << endl;
        }
        else
        {
            cout << "圆1和圆2不相切" << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值