进阶PAT 2021-12-12 13:14 采纳率: 78.4%
浏览 203
已结题

使用C++完成二维点类小程序

题目描述:
要求编写一个类,来代表二维坐标系上的点
要求实现函数:
1.打印该点坐标
2.求两点之间距离
3.返回一个点对象,为该点相对于原点的(0,0)的对称点
4.判断点是否在给定圆内

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

输出格式:
对于函数1,打印该点坐标,用空格隔开
对于函数2,打印两点间距离(误差不超过10^-5对于函数3,打印结果点坐标,用空格隔开
对于函数4,若点在圆内输出“Yes”,否则输出“No”

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

输出样例:
1 1
1
-1 -1
Yes

数据范围与提示:
输入的数均不超过1000

  • 写回答

1条回答 默认 最新

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

    有用请采纳

    #include<iostream>
    #include<cmath>    // 使用sqrt 
    
    using namespace std; 
    
    class Point{
    private:
        int x, y;
    
    public:
        Point(double x, double y);
        void printPoint();
        void calculateDistance(double otherX, double otherY);
        Point symmetryPoint();
        void judgeInCircle(double p, double q, double r);
    }; 
    
    Point::Point(double x, double y){
        this->x = x;
        this->y = y;
    }
    
    void Point::printPoint(){
        cout << this->x << " " << this->y << endl; 
    }
    
    void Point::calculateDistance(double otherX, double otherY){
        double distance;
        distance = sqrt((otherX - this->x) * (otherX - this->x) + (otherY - this->y) * (otherY - this->y));
        
        printf("%.8lf\n", distance);
    } 
    
    Point Point::symmetryPoint(){
        cout << -this->x << " " << -this->y << endl;
    }
    
    void Point::judgeInCircle(double p, double q, double r){
        double distance; //圆心到点的距离
        distance =  sqrt((p - this->x) * (p - this->x) + (q - this->y) * (q - this->y));
        double judgeRange = 0.00001;
    
        if(fabs(distance) < r)
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    
    
    int main(){
        double x, y;
        int choose, num;
        cin >> x >> y;
        Point point = Point(x, y);
        double otherX, otherY;
        double circleX, circleY, circleR;
        
        cin >> choose;
        for (int i = 0; i < choose; i++){
            cin >> num;
            switch(num){
                case 1: 
                    point.printPoint(); 
                    break;
                case 2: 
                    cin >> otherX >> otherY;
                    point.calculateDistance(otherX, otherY); 
                    break;
                case 3: 
                    point.symmetryPoint(); 
                    break;
                case 4: 
                    cin >> circleX >> circleY >> circleR;
                    point.judgeInCircle(circleX, circleY, circleR);
                    break;
                default:
                    break;
            }
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题