要求:设计一个圆形类和一个点类,计算点和圆的关系
个人设计如下:
class circle{
public:
void setx(int x)
{
X = x;
}
int getx()
{
return X;
}
void sety(int y)
{
Y = y;
}
int gety()
{
return Y;
}
void setr(int r)
{
R = r;
}
int getr()
{
return R;
}
private:
int X;
int Y;
int R;
};
class point {
public:
void setpx(int x)
{
X = x;
}
int getpx()
{
return X;
}
void setpy(int y)
{
Y = y;
}
int getpy()
{
return Y;
}
private:
int X;
int Y;
};
int main()
{
circle c;
point p;
int a, b = 0; int r = 0; int a1, b1 = 0;
cout << "输入圆的坐标" << endl;
cin >> a >> b; c.setx(a); c.sety(b);
cout << "输入圆的半径" << endl;
cin >> r; c.setr(r);
cout << "输入点的坐标" << endl;
cin >> a1 >> b1; p.setpx(a1); p.setpy(b1);
int x1 = c.getx(); int y1 = c.gety(); int x2 = p.getpx(); int y2 = p.getpy();
double d = sqrt(1.00 * (x1 - x2) * (x1 - x2) + 1.00 * (y1 - y2) * (y1 - y2));
if (d < r)
cout << "相交" << endl;
if (d == r)
cout << "相切" << endl;
if (d > r)
cout << "相离" << endl;
}
把点当成线了呜呜呜呜呜呜呜呜
运行图:
注意避免函数重名
标准答案:
分文件:circle.h,point.h,circle.cpp,point.cpp--->可以重复利用
1,头文件声明函数和类
circle.h:
#pragma once
#include<iostream>
#include"point.h"
class circle {
public:
void setr(int r);
int getR();
void setcenter(point center);
point getcenter();
private:
int R;
//在类中可以让另一个类作为本来中的成员
point Center;
};
point.h:
#pragma once
#include<iostream>
using namespace std;
class point {
public:
void setX(int x);
int getx();
void setY(int y);
int gety();
private:
int X;
int Y;
};
源文件:
point.cpp
#include"point.h"
void point::setX(int x)//告知是point作用域下的成员函数
{
X = x;
}
int point::getx()
{
return X;
}
void point::setY(int y)
{
Y = y;
}
int point::gety()
{
return Y;
}
circle.cpp:
#include"circle.h"
void circle::setr(int r)
{
R = r;
}
int circle::getR()
{
return R;
}
void circle::setcenter(point center)
{
Center = center;
}
point circle::getcenter()
{
return Center;
}
主cpp:
#include<iostream>
using namespace std;
#include<math.h>
#include"circle.h"
#include"point.h"
void isincircle(circle &c, point &p)
{
int distance = (c.getcenter().getx() - p.getx()) * (c.getcenter().getx() - p.getx()) +
(c.getcenter().gety() - p.gety()) * (c.getcenter().gety() - p.gety());
int rr = c.getR() * c.getR();
if (distance == rr)
cout << "点在圆上" << endl;
if (distance <rr)
cout << "点在圆内" << endl;
if (distance > rr)
cout << "点在圆外" << endl;
}
int main()
{//创建圆
circle c;
c.setr(10);
point center;//只是定义了一个名为center的点,还没有放入圆c中!!!!!!!!
center.setX(10);
center.setY(0);
c.setcenter(center);//注意要放入圆心
point p;
p.setX(10);
p.setY(9);
isincircle(c, p);
//创建点
}