要求:
编写一个圆类
成员变量
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;
}