#include<iostream>#include<cmath>using namespace std;const double PI=3.14159265;class Circle;class Point{ private: int x; int y; public: friend class Circle; Point(); Point(int a, int b); Point(Point &p);};class Circle{ private: double radius; Point center; public: Circle(int m, int n, double j); Circle(Circle &q); double getArea(); double getPerimeter(); int isInTheCircle(Point &i); };int Circle::isInTheCircle(Point &i){ double lenth; lenth=sqrt(i.x*i.x+i.y*i.y); if(lenth<radius) return 1; else if(lenth>=radius) return 0; }double Circle::getArea(){ return PI*radius*radius;}double Circle::getPerimeter(){ return 2*PI*radius;}Circle::Circle(int m, int n, double j){ center.x=m; center.y=n; radius=j;}Circle::Circle(Circle &q){ center.x=q.center.x; center.y=q.center.y; radius=q.radius;}Point::Point():x(0),y(0){}Point::Point(int a, int b):x(a),y(b){}Point::Point(Point &p){ x=p.x; y=p.y;}int main(){ double x,y,radius; cin>>x>>y; cin>>radius; Circle c(x,y,radius); cin>>x>>y; Point p(x,y); cout<<c.getArea()<<endl; cout<<c.getPerimeter()<<endl; if(c.isInTheCircle(p)) cout<<"该点在圆内部!"<<endl; else cout<<"该点不在圆内部!"<<endl; return 0; }输入0 0 3 2.5 2.5为什么输出不是28.2743 18.8496 该点不在园内部!
时间: 2023-06-26 22:05:45 浏览: 80
您的代码有一些问题,主要是在 `isInTheCircle` 函数中计算距离的时候没有用圆心作为参考点,而是直接使用了点的坐标。修改后的代码如下:
```
#include<iostream>
#include<cmath>
using namespace std;
const double PI=3.14159265;
class Circle;
class Point{
private:
int x;
int y;
public:
friend class Circle;
Point();
Point(int a, int b);
Point(Point &p);
};
class Circle{
private:
double radius;
Point center;
public:
Circle(int m, int n, double j);
Circle(Circle &q);
double getArea();
double getPerimeter();
int isInTheCircle(Point &i);
};
int Circle::isInTheCircle(Point &i){
double length;
length = sqrt((i.x - center.x) * (i.x - center.x) + (i.y - center.y) * (i.y - center.y));
if (length < radius) {
return 1;
} else if (length >= radius) {
return 0;
}
}
double Circle::getArea(){
return PI*radius*radius;
}
double Circle::getPerimeter(){
return 2*PI*radius;
}
Circle::Circle(int m, int n, double j){
center.x=m;
center.y=n;
radius=j;
}
Circle::Circle(Circle &q){
center.x=q.center.x;
center.y=q.center.y;
radius=q.radius;
}
Point::Point():x(0),y(0){}
Point::Point(int a, int b):x(a),y(b){}
Point::Point(Point &p){
x=p.x;
y=p.y;
}
int main(){
double x,y,radius;
cin>>x>>y;
cin>>radius;
Circle c(x,y,radius);
cin>>x>>y;
Point p(x,y);
cout<<c.getArea()<<endl;
cout<<c.getPerimeter()<<endl;
if(c.isInTheCircle(p))
cout<<"该点在圆内部!"<<endl;
else
cout<<"该点不在圆内部!"<<endl;
return 0;
}
```
修改后您的程序就能正确输出28.2743和18.8496了,同时也能正确判断点是否在圆内部。
阅读全文