进阶PAT 2021-12-12 12:49 采纳率: 78.4%
浏览 124
已结题

使用C++完成圆类小程序

题目描述:
要求编写一个类,来代表二维坐标系上的圆
要求实现函数:
1.打印圆心坐标
2.求圆的面积
3.求圆的周长
4.判断两圆是否相切(包括内切和外切)

输入格式:
第一行输入三个整数 x,y,r,代表圆心的坐标(x,y),半径r
第二行输入一个正整数case,代表测试数据的组数
对于每一组case,第一行输入一个数,代表要调用哪个函数,1 2 3 4对应题干函数
对于函数1,无后续输入
对于函数2,无后续输入
对于函数3,无后续输入
对于函数4,输入两个整数 p ,q , r1, ( p , q)代表第二个圆圆心坐标,r1代表第二个圆的半径

输出格式:
对于函数1,打印圆心坐标,用空格隔开
对于函数2,打印圆的面积,误差不超过10^-5
对于函数3,打印圆的周长,误差不超过10^-5
对于函数4,若点在两圆相切输出“Yes”,否则输出“No”

输入样例:
0 0 1
4
1
2
3
4
2 0 1

输出样例:
0 0
3.14159265
6.2831853
Yes

  • 写回答

1条回答 默认 最新

  • 索利亚噶通 2021-12-12 13:25
    关注

    有用请采纳

    #include<iostream>
    #include<cmath>    // 使用sqrt 
    #define PI 3.14159265358
    using namespace std;
    
    class Circle{
    private:
        double x, y, r;
    public:
        Circle(double x, double y, double r);
        void printCenter();
        void printArea();
        void printPerimeter(); 
        void judgeTangent(double otherX, double otherY, double otherR);
    };
    
    Circle::Circle(double x, double y, double r){
        this->x = x;
        this->y = y;
        this->r = r;
    }
    
    
    void Circle::printCenter(){
        cout << this->x << " " << this->y << endl;
    }
    
    void Circle::printArea(){
        double area;
        area = PI * this->r * this->r;
        printf("%.8lf\n", area);
    }
    
    void Circle::printPerimeter(){
        double perimeter;
        perimeter = 2 * PI * this->r;
        printf("%.8lf\n", perimeter);  
    }
    
    void Circle::judgeTangent(double otherX, double otherY, double otherR){
        double distance;   // 圆心之间的距离
        distance = sqrt((otherX - this->x) * (otherX - this->x) + (otherY - this->y) * (otherY - this->y));
        double addR = otherR + this->r;   // 判断外切
        double subR = fabs(otherR - this->r);  // 判断内切
        double judgeRange = 0.00001;
        if(fabs(distance - addR) < judgeRange || fabs(distance - subR) < judgeRange)  
            cout << "Yes" << endl;
        
        else
            cout << "No" << endl; 
    }
    
    
    int main(){
        double x, y, r;
        int choose, num;
        cin >> x >> y >> r;
        Circle circle = Circle(x, y, r);
        double otherX, otherY, otherR;
        
        cin >> choose;
        for (int i = 0; i < choose; i++){
            cin >> num;
            switch(num){
                case 1: 
                    circle.printCenter(); 
                    break;
                case 2: 
                    circle.printArea(); 
                    break;
                case 3: 
                    circle.printPerimeter(); 
                    break;
                case 4: 
                    cin >> otherX >> otherY >> otherR;
                    circle.judgeTangent(otherX, otherY, otherR);
                    break;
                default:
                    break;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月12日
  • 已采纳回答 12月12日
  • 创建了问题 12月12日

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私